Example #1
0
    def save(record, oldRecord=None):
        """ Convert the record to Xml code, and Write
        the code to the disk, record id is the basename
        of the record file.

        If the oldRecord is provided, this is to change
        an existing record. When to change an existing
        log, the new log may be saved to a new directory
        if its timestamp been changed, in such case the
        old log will be deleted.
        """
        paths = []
        if not getattr(record, 'id', None):
            record.id = applib.genId(record.time)
        if not oldRecord:   # add new record
            commitMsg = 'Add log\n\n%s' % record.id
        else:
            commitMsg = 'Change log\n\n%s' % record.id
            if record != oldRecord:
                path = XmlStorage.idToPath(oldRecord.id)
                paths.append(path)
                XmlStorage.__delete(None, path=path)
            else:
                return
        path = XmlStorage.saveRecord(record.elements())
        paths.append(path)

        # create a git commit
        XmlStorage.git.commit(paths, commitMsg)

        return record
Example #2
0
    def save(record, oldRecord=None):
        """ Convert the record to Xml code, and Write
        the code to the disk, record id is the basename
        of the record file.

        If the oldRecord is provided, this is to change
        an existing record. When to change an existing
        log, the new log may be saved to a new directory
        if its timestamp been changed, in such case the
        old log will be deleted.
        """
        paths = []
        if not getattr(record, 'id', None):
            record.id = applib.genId(record.time)
        if not oldRecord:  # add new record
            commitMsg = 'Add log\n\n%s' % record.id
        else:
            commitMsg = 'Change log\n\n%s' % record.id
            if record != oldRecord:
                path = XmlStorage.idToPath(oldRecord.id)
                paths.append(path)
                XmlStorage.__delete(None, path=path)
            else:
                return
        path = XmlStorage.saveRecord(record.elements())
        paths.append(path)

        # create a git commit
        XmlStorage.git.commit(paths, commitMsg)

        return record
Example #3
0
 def save(record, oldRecord=None, commit=True):
     """ For add and change a record.
     If the oldRecord is provided, this is to change
     an existing record, else it's to add a new one.
     if 'commit' is True, do a commit to the db.
     """
     tbl = E.recordTbl
     data = dict(record.elements()).items()
     data = Record.convertFields(data, False)
     if not oldRecord:  # add new record
         record.id = applib.genId(record.time)
         data['id'] = record.id
         # insert
         flds = ','.join(E.fields)
         hlds = ','.join(['?'] * len(E.fields))
         vals = [data[k] for k in E.fields]
         sql = 'INSERT INTO %s (%s) VALUES (%s)' % (tbl, flds, hlds)
     else:
         if record == oldRecord:
             return
         # update
         keys = []
         for k in E.fields:
             vnew = getattr(record, k)
             vold = getattr(oldRecord, k)
             if vnew != vold:
                 keys.append(k)
         vals = [data[k] for k in keys]
         pairs = ','.join(['%s=?'] * len(keys)) % tuple(keys)
         sql = 'UPDATE %s SET %s WHERE id = ?' % (tbl, pairs)
         vals.append(record.id)
     try:
         cur = E.conn.cursor()
         cur.execute('begin')
         cur.execute(sql, vals)
         if commit:
             E.commit()
         return record
     except:
         return None
Example #4
0
 def save(record, oldRecord=None, commit=True):
     """ For add and change a record.
     If the oldRecord is provided, this is to change
     an existing record, else it's to add a new one.
     if 'commit' is True, do a commit to the db.
     """
     tbl  = E.recordTbl
     data = dict(record.elements()).items()
     data = Record.convertFields(data, False)
     if not oldRecord:   # add new record
         record.id  = applib.genId(record.time)
         data['id'] = record.id
         # insert
         flds = ','.join(E.fields)
         hlds = ','.join(['?'] * len(E.fields))
         vals = [data[k] for k in E.fields]
         sql  = 'INSERT INTO %s (%s) VALUES (%s)' % (tbl, flds, hlds)
     else:
         if record == oldRecord:
             return
         # update
         keys = []
         for k in E.fields:
             vnew = getattr(record, k)
             vold = getattr(oldRecord, k)
             if vnew != vold:
                 keys.append(k)
         vals = [data[k] for k in keys]
         pairs = ','.join(['%s=?'] * len(keys)) % tuple(keys)
         sql = 'UPDATE %s SET %s WHERE id = ?' % (tbl, pairs)
         vals.append(record.id)
     try:
         cur = E.conn.cursor()
         cur.execute('begin')
         cur.execute(sql, vals)
         if commit:
             E.commit()
         return record
     except:
         return None