~sebzur/os.correspondence/main

« back to all changes in this revision

Viewing changes to Korespondencja/KorespondencjaModel.py

  • Committer: Sebastian Żurek
  • Date: 2008-08-21 08:23:30 UTC
  • Revision ID: sebzur@gmail.com-20080821082330-zbn0sksrkgleunmn
Mail data modificaton added. Model changed to use Mail class

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
import string
14
14
import types
15
15
 
16
 
# Do ogarnięcia i poprawienia, znalazłem na:
17
 
# http://kbyanc.blogspot.com/2007/07/python-aggregating-function-arguments.html
18
 
# chodzi o słownik z argumentami lokalnych zmiennych, funkcja ma mieć możliwość w dowolnym miejscy...
19
 
def arguments():
20
 
        """Returns tuple containing dictionary of calling function's
21
 
           named arguments and a list of calling function's unnamed
22
 
           positional arguments.
23
 
        """
24
 
        from inspect import getargvalues, stack
25
 
        posname, kwname, args = getargvalues(stack()[1][0])[-3:]
26
 
        posargs = args.pop(posname)
27
 
        args.update(args.pop(kwname))
28
 
        return args, posargs
29
 
 
30
16
    
31
17
class KorespondencjaModel(Model.Model):
32
18
    def __init__(self,*args,**kwargs):
38
24
        """
39
25
        super(KorespondencjaModel,self).__init__(*args,**kwargs)
40
26
        
41
 
        self = self._models['Korespondencja']
 
27
        #self = self._models['Korespondencja']
42
28
 
43
29
# - Table Creation Fields ----------------------------------------------------------
44
30
 
75
61
  #      self._cursor.execute(query, (incoming,))
76
62
   #     return self._cursor.fetchall()
77
63
 
78
 
    def GetMailColumnHeaders(self):
79
 
        return ('Nr', 'Data', 'Korespondent', u'Treść',)
 
64
    def GetMailColumnHeaders(self,db_columns = False):
 
65
            if db_columns:
 
66
                    return ('nr','m_date','from_to','topic')
 
67
            else:
 
68
                    return ('Nr', 'Data', 'Korespondent', u'Treść',)
80
69
 
81
70
    def GetLastMailNumber(self):
82
71
        query="SELECT max(nr)+1 FROM mail"
108
97
        #print args
109
98
        myregexp= '.*'+search+'.*'
110
99
        queries=("SELECT COUNT(*) FROM mail ",
111
 
                 "SELECT nr, m_date, from_to, topic, signature, address, polecony, ZPO, priorytetowy FROM mail ")
 
100
                 "SELECT mid, incoming,nr, m_date, from_to, topic, signature, address, polecony, ZPO, priorytetowy FROM mail ")
112
101
 
 
102
        column_names = ['mid','incoming','nr','m_date','from_to','topic','signature','address','polecony','ZPO','priorytetowy']
113
103
        if item==None:
114
104
                query=queries[0]
115
105
        else:
156
146
        result=self._cursor.fetchone()
157
147
 
158
148
        if item==None:
 
149
                # Zwracam ilość poczty
159
150
                return result[0]
160
151
        else:
161
152
                # Musze przerobic na unicode dane dla Windowsa...
162
 
                unicoded_result=list(result)
163
 
                for i,each in enumerate(unicoded_result):
164
 
                        if type(each)==types.StringType:
165
 
                                unicoded_result[i]=each.decode('utf-8')
166
 
                return unicoded_result
167
 
 
168
 
 
169
 
        
 
153
                unicoded_result={}
 
154
                for i,each in enumerate(result):
 
155
                        unicoded_result[column_names[i]]= each.decode('utf-8') if type(each)==types.StringType else each
 
156
 
 
157
                return Mail(data = unicoded_result)
 
158
 
170
159
# - Setters ------------------------------------------------------------
171
160
 
172
 
    def InsertMail(self, data):
 
161
    def InsertMail(self, mail):
 
162
 
 
163
        data = mail.GetData()
 
164
        data.pop('mid')
173
165
        try:
174
166
            self._cursor.execute(self.ParseInsertQuery(data,'mail'),data.values())
175
167
            self._db.commit()         
177
169
        except Exception, error:            
178
170
            self._db.rollback()            
179
171
            raise Exceptions.DatabaseError(error.message)
 
172
 
 
173
 
 
174
    def UpdateMail(self,mail):
 
175
        """Update Mail data
 
176
        
 
177
        """
 
178
        data = mail.GetData()
 
179
        where={'mid':data.pop('mid')}
 
180
        try:
 
181
                
 
182
            values=data.values()
 
183
            values.append(where.values()[0])
 
184
            self._cursor.execute(self.ParseUpdateQuery(data,'mail',where),values)
 
185
            self._db.commit()
 
186
            pubsub.Publisher().sendMessage("PostBook.Model.Events", data=('MailUpdated',mail))        
 
187
        except Exception, error:            
 
188
            self._db.rollback()            
 
189
            raise Exceptions.DatabaseError(error.message)
 
190
 
 
191
 
 
192
class Mail(object):
 
193
 
 
194
        _model = KorespondencjaModel()
 
195
        
 
196
        def __init__(self, data = None,*args,**kwargs):
 
197
                super(Mail,self).__init__(*args,**kwargs)
 
198
                self._data = {
 
199
                        'mid': None,
 
200
                        'nr': None,
 
201
                        'm_date': None,
 
202
                        'from_to': None,
 
203
                        'address': None,
 
204
                        'signature': None,
 
205
                        'topic':None,
 
206
                        'incoming':None,
 
207
                        'polecony':None,
 
208
                        'ZPO':None,
 
209
                        'priorytetowy':None}
 
210
 
 
211
                if data != None:
 
212
                        for key in self._data.keys():
 
213
                                if data.has_key(key):
 
214
                                        self._data[key] = data[key]
 
215
        def GetData(self):
 
216
                return self._data
 
217
 
 
218
        def SetIncoming(self, incoming):
 
219
                self._data['incoming'] = incoming
 
220
 
 
221
        def Save(self):
 
222
                """Save the Mail in database.
 
223
 
 
224
                """             
 
225
                if self._data['mid'] !=None:
 
226
                        print 'Robie update!!'                  
 
227
                        self._model.UpdateMail(self)
 
228
                elif self._data['mid'] == None:                 
 
229
                        self._model.InsertMail(self)
 
230
 
 
231
 
 
232
 
 
233