Exemple #1
0
 def update(self):
     '''
     Запускает обновление
     '''
     
     logging.info("Update top for group %s", self.gid)
     
     sql_script = open(p('app/topfuns/sql/processing.mysql.sql'), 'U')
     sql = sql_script.read().decode('utf-8') % {'gid': self.gid}
     run_sql(sql) 
     sql_script.close()
     
     logging.info("Updating for group %s completed", self.gid)
Exemple #2
0
    def gen_report(beginWith=None, endWith=None):
        '''
        Процедура непосредственного формирования отчета
        '''
        if beginWith is None or endWith is None:
            beginWith = date.today() - timedelta(8)
            endWith = date.today() - timedelta(1)
        
        gid = 2611
        
        sql = open(p('app/topfuns/sql/gen_report.mysql.sql')).read().decode('utf8') % {
            'gid': gid,
            'start_with': beginWith.strftime('%Y-%m-%d'),
            'end_with':   endWith.strftime('%Y-%m-%d')
        }
        variables = re.findall(r'/\*(.*?)\*/.*?SELECT @([a-z_]+)', sql.replace('\r', '').replace('\n', ''))

        data = []
        if len(variables) > 0 and run_sql(sql):
            cursor = connection.cursor()
            cursor.execute("SELECT " + ",".join(["@" + var[1] + " as " + var[1] for var in variables]) + ";")

            row = cursor.fetchall()

            for i in xrange(len(row[0])):
                data.append({
                    'description':  variables[i][0], 
                    'value': row[0][i]
                })
            content = ''
        
        content = ''
        for entry in data:
            content += '"%(description)s";"%(value)s"\n' %  {
                'description': entry['description'],
                'value': entry['value']
            }
            
        
        group = VkGroup.objects.get(gid=2611)    
        Report.objects.create(group=group, start=beginWith, end=endWith, stuff=content)