Example #1
0
    def synch(self):
        """
        Synchronize objects in the object cache with the database.

        Objects which have been deleted in the database are removed
        from the object cache.
        
        Objects which are out of synch with their database record
        have their attribute set to match the data in the database.
        """
        #print 'Synchronizing ' + self.table.name + ' with database'
        last_synched = self.last_synched  # Remember this, because we're about to overwrite it.
        self.last_synched = now_string()

        # Delete any newly deleted objects.
        sql = 'SELECT identifier FROM deleted WHERE table_name=' + wsq(
            self.table.name) + ' AND deleted >= ' + wsq(last_synched)
        cursor = db.select(sql)
        while (1):
            row = cursor.fetchone()
            if row == None: break

            # Load keys for the deleted object
            object = self.new_object()
            if len(self.table.key_list) == 1:
                field = self.table.fields[self.table.key_list[0]]
                value = field.field_to_attr(row[0])
                setattr(object, field.attribute, value)
            else:
                values = row[0].split()
                for key in self.table.key_list:
                    field = self.table.fields[key]
                    value = field.field_to_attr(values[field.index])
                    setattr(object, field.attribute, value)
            object.key = self.table.get_key(object)

            #print 'Deleting from ' + self.table.name + ' cache: ' + str(value)
            self.cache.delete(object)

            # FIXME: Delete the object from all data sets which contain it!

        # Update any newly updated objects.
        sql = self.table.select + ' WHERE updated >= ' + wsq(last_synched)
        cursor = db.select(sql)
        while (1):
            row = cursor.fetchone()
            if row == None: break
            key = self.table.get_row_key(row)
            if self.cache.has_key(key):
                object = self.cache[key]
                self.table.load_row(object, row)
                #print 'Updating in ' + self.table.name + ' cache: ' + str(object.key)
            else:
                object = self.row_to_object(row)
                self.cache.add(object)
Example #2
0
File: base.py Project: Fat-Zer/LDP
    def synch(self):
        """
        Synchronize objects in the object cache with the database.

        Objects which have been deleted in the database are removed
        from the object cache.
        
        Objects which are out of synch with their database record
        have their attribute set to match the data in the database.
        """
        #print 'Synchronizing ' + self.table.name + ' with database'
        last_synched = self.last_synched        # Remember this, because we're about to overwrite it.
        self.last_synched = now_string()

        # Delete any newly deleted objects.
        sql = 'SELECT identifier FROM deleted WHERE table_name=' + wsq(self.table.name) + ' AND deleted >= ' + wsq(last_synched)
        cursor = db.select(sql)
        while (1):
            row = cursor.fetchone()
            if row==None: break
            
            # Load keys for the deleted object
            object = self.new_object()
            if len(self.table.key_list)==1:
                field = self.table.fields[self.table.key_list[0]]
                value = field.field_to_attr(row[0])
                setattr(object, field.attribute, value)
            else:
                values = row[0].split()
                for key in self.table.key_list:
                    field = self.table.fields[key]
                    value = field.field_to_attr(values[field.index])
                    setattr(object, field.attribute, value)
            object.key = self.table.get_key(object)

            #print 'Deleting from ' + self.table.name + ' cache: ' + str(value)
            self.cache.delete(object)

            # FIXME: Delete the object from all data sets which contain it!

        # Update any newly updated objects.
        sql = self.table.select + ' WHERE updated >= ' + wsq(last_synched)
        cursor = db.select(sql)
        while (1):
            row = cursor.fetchone()
            if row==None: break
            key = self.table.get_row_key(row)
            if self.cache.has_key(key):
                object = self.cache[key]
                self.table.load_row(object, row)
                #print 'Updating in ' + self.table.name + ' cache: ' + str(object.key)
            else:
                object = self.row_to_object(row)
                self.cache.add(object)
Example #3
0
 def load(self):
     sql = 'SELECT report_code, only_cvs, command FROM file_report WHERE report_code=' + wsq(self.code)
     cursor = db.select(sql)
     row = cursor.fetchone()
     if row==None: return
     self.load_row(row)
     sql = 'SELECT report_code, lang, report_name, report_desc FROM file_report_i18n WHERE report_code=' + wsq(self.code)
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         lang = trim(row[1])
         self.name[lang] = trim(row[2])
         self.description[lang] = trim(row[3])
Example #4
0
 def __init__(self):
     super(Templates, self).__init__()
     sql = "SELECT template_code, template FROM template"
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         newTemplate = Template()
         newTemplate.load_row(row)
         self[newTemplate.code] = newTemplate
Example #5
0
 def load_row(self, row):
     self.code = trim(row[0])
     sql = "SELECT lang, string, version FROM string_i18n WHERE string_code=" + wsq(self.code)
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         lang               = row[0]
         self.string[lang]  = trim(row[1])
         self.version[lang] = trim(row[2])
Example #6
0
 def __init__(self):
     super(Strings, self).__init__()
     sql = "SELECT string_code FROM string"
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         newString = String()
         newString.load_row(row)
         self[newString.code] = newString
Example #7
0
 def __init__(self):
     super(Sessions, self).__init__()
     sql = "SELECT section_code, sort_order FROM section"
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         newSection = Section()
         newSection.load_row(row)
         self[newSection.code] = newSection
Example #8
0
 def __init__(self):
     super(Pages, self).__init__()
     sql = "SELECT page_code, section_code, sort_order, template_code, data, only_dynamic, only_registered, only_admin, only_sysadmin FROM page"
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         page = Page()
         page.load_row(row)
         self[page.code] = page
Example #9
0
 def load_row(self, row):
     self.code		      = trim(row[0])
     self.sort_order       = safeint(row[1])
     sql = "SELECT lang, section_name FROM section_i18n WHERE section_code=" + wsq(self.code)
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         lang = row[0]
         self.name[lang] = trim(row[1])
Example #10
0
 def __init__(self):
     Super(Blocks, self).__init__()
     sql = "SELECT block_code, block FROM block"
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         newBlock = Block()
         newBlock.load_row(row)
         self[newBlock.code] = newBlock
Example #11
0
 def __init__(self):
     super(NewsItems, self).__init__()
     sql = "SELECT news_id, pub_date FROM news"
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         news = NewsItem()
         news.load_row(row)
         self[news.id] = news
Example #12
0
 def load(self):
     self.clear()
     sql = 'SELECT report_code, only_cvs, command FROM file_report'
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         report = FileReport()
         report.load_row(row)
         self[report.code] = report
     sql = 'SELECT report_code, lang, report_name, report_desc FROM file_report_i18n'
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         report_code = trim(row[0])
         lang = trim(row[1])
         report = self[report_code]
         report.name[lang] = trim(row[2])
         report.description[lang] = trim(row[3])
Example #13
0
 def __init__(self, username=None):
     self.user = None
     if username:
         self.user = dms.username.get_by_id(username)
         sql = 'SELECT username, ip_address, uri, created FROM session WHERE username=' + wsq(username)
         cursor = db.select(sql)
         row = cursor.fetchone()
         if row:
             self.username   = trim(row[0])
             self.ip_address = trim(row[1])
             self.uri        = trim(row[2])
             self.created    = time2str(row[3])
Example #14
0
 def load(self):
     sql = 'SELECT username, ip_address, uri, created FROM session'
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row == None: break
         session = Session()
         session.username = trim(row[0])
         session.ip_address = trim(row[1])
         session.uri = trim(row[2])
         session.created = time2str(row[3])
         self[session.username] = session
Example #15
0
 def load(self):
     sql = 'SELECT username, ip_address, uri, created FROM session'
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         session = Session()
         session.username   = trim(row[0])
         session.ip_address = trim(row[1])
         session.uri        = trim(row[2])
         session.created    = time2str(row[3])
         self[session.username] = session
Example #16
0
 def load_row(self, row):
     self.id       = row[0]
     self.pub_date = time2str(row[1])
     sql = "SELECT lang, headline, news, version FROM news_i18n WHERE news_id=" + str(self.id)
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         lang                = row[0]
         self.headline[lang] = trim(row[1])
         self.news[lang]     = trim(row[2])
         self.version[lang]  = trim(row[3])
Example #17
0
 def __init__(self, username=None):
     self.user = None
     if username:
         self.user = dms.username.get_by_id(username)
         sql = 'SELECT username, ip_address, uri, created FROM session WHERE username=' + wsq(
             username)
         cursor = db.select(sql)
         row = cursor.fetchone()
         if row:
             self.username = trim(row[0])
             self.ip_address = trim(row[1])
             self.uri = trim(row[2])
             self.created = time2str(row[3])
Example #18
0
File: base.py Project: Fat-Zer/LDP
 def get_sql(self, sql):
     """
     Accepts a SQL statement, instantiates the corresponding objects from the
     database, and stores those objects in the data cache if possible.
     """
     #print 'Cache miss, loading: ' + self.table.name
     dataset = self.new_dataset()
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row==None: break
         object = self.row_to_object(row)
         dataset[object.key] = object
         self.cache.add(object)
     return dataset
Example #19
0
 def get_sql(self, sql):
     """
     Accepts a SQL statement, instantiates the corresponding objects from the
     database, and stores those objects in the data cache if possible.
     """
     #print 'Cache miss, loading: ' + self.table.name
     dataset = self.new_dataset()
     cursor = db.select(sql)
     while (1):
         row = cursor.fetchone()
         if row == None: break
         object = self.row_to_object(row)
         dataset[object.key] = object
         self.cache.add(object)
     return dataset
Example #20
0
 def get_session(self, req):
     state.session = None
     state.user = None
     cookie = self.get_cookie(req.headers_in, 'lampadas')
     if cookie:
         session_id = str(cookie)
         sql = 'SELECT username FROM username WHERE session_id=' + wsq(session_id)
         cursor = db.select(sql)
         row = cursor.fetchone()
         if not row==None:
             username = trim(row[0])
             self.load()
             if self.has_key(username):
                 state.session = self[username]
                 state.session.refresh(req.connection.remote_addr[0], req.uri)
                 state.user = state.session.user
             else:
                 self.add(username, req.connection.remote_addr[0], req.uri)
             state.user = dms.username.get_by_id(username)
Example #21
0
File: base.py Project: Fat-Zer/LDP
    def get_by_id(self, id):
        """
        Returns an individual object whose primary key field matches
        the specified id.

        If the object is in the cache, the cached object is returned.
        Objects which have more than one primary key field cannot be
        reliably retrieved using this function. In this event, only the
        first matching object will be returned.
        """
        
        object = self.cache.get_by_key(id)
        if object==None:
            data_field = self.table.id_field()
            sql = self.table.select + ' WHERE ' + data_field.field_name + '=' + data_field.attr_to_field(id)
            cursor = db.select(sql)
            row = cursor.fetchone()
            if row==None: return
            object = self.row_to_object(row)
        return object
Example #22
0
 def get_session(self, req):
     state.session = None
     state.user = None
     cookie = self.get_cookie(req.headers_in, 'lampadas')
     if cookie:
         session_id = str(cookie)
         sql = 'SELECT username FROM username WHERE session_id=' + wsq(
             session_id)
         cursor = db.select(sql)
         row = cursor.fetchone()
         if not row == None:
             username = trim(row[0])
             self.load()
             if self.has_key(username):
                 state.session = self[username]
                 state.session.refresh(req.connection.remote_addr[0],
                                       req.uri)
                 state.user = state.session.user
             else:
                 self.add(username, req.connection.remote_addr[0], req.uri)
             state.user = dms.username.get_by_id(username)
Example #23
0
    def get_by_id(self, id):
        """
        Returns an individual object whose primary key field matches
        the specified id.

        If the object is in the cache, the cached object is returned.
        Objects which have more than one primary key field cannot be
        reliably retrieved using this function. In this event, only the
        first matching object will be returned.
        """

        object = self.cache.get_by_key(id)
        if object == None:
            data_field = self.table.id_field()
            sql = self.table.select + ' WHERE ' + data_field.field_name + '=' + data_field.attr_to_field(
                id)
            cursor = db.select(sql)
            row = cursor.fetchone()
            if row == None: return
            object = self.row_to_object(row)
        return object
Example #24
0
    def load_row(self, row):
        self.code            = trim(row[0])
        self.section_code	 = trim(row[1])
        self.sort_order      = safeint(row[2])
        self.template_code	 = trim(row[3])
        self.data            = trim(row[4]).split()
        self.only_dynamic    = tf2bool(row[5])
        self.only_registered = tf2bool(row[6])
        self.only_admin      = tf2bool(row[7])
        self.only_sysadmin   = tf2bool(row[8])
        sql = "SELECT lang, title, menu_name, page, version FROM page_i18n WHERE page_code=" + wsq(self.code)
        cursor = db.select(sql)
        while (1):
            row = cursor.fetchone()
            if row==None: break
            lang            = row[0]
            self.title[lang] = trim(row[1])
            self.menu_name[lang] = trim(row[2])
            self.page[lang] = trim(row[3])
            self.version[lang] = trim(row[4])

            # if there's no menu_name, use the title by default
            if self.menu_name[lang]=='':
                self.menu_name[lang] = self.title[lang]
Example #25
0
 def load(self):
     sql = 'SELECT page_code, section_code, sort_order, template_code, data, only_dynamic, only_registered, only_admin, only_sysadmin FROM page WHERE page_code=' + wsq(self.code)
     cursor = db.select(sql)
     row = cursor.fetchone()
     if row==None: return
     self.load_row(row)
Example #26
0
 def load(self):
     sql = 'SELECT news_id, pub_date FROM news WHERE news_id=' + str(self.id)
     cursor = db.select(sql)
     row = cursor.fetchone()
     if row==None: return
     self.load_row(row)