Exemple #1
0
 def authentication_fakeadmin(self,fakeuser,admin_password):
     with app.app_context():
         admin_dbuser=r.table('users').get('admin').run(db.conn)
         if admin_dbuser is None:
             return False
     pw=Password()
     if pw.valid(admin_password,admin_dbuser['password']):
         with app.app_context():
             dbuser=r.table('users').get(fakeuser).run(db.conn)
         if dbuser is None:
             return False
         else:
             dbuser['name']='FAKEUSER'
             #~ quota = admin_dbuser['quota']
             #~ {  'domains':{ 'desktops': 99,
                                             #~ 'templates':99,
                                             #~ 'running':  99},
                                 #~ 'hardware':{'vcpus':    8,
                                             #~ 'ram':      1000000}} # 10GB
             dbuser['quota']=admin_dbuser['quota']
             dbuser['role']='admin'
             ram_users[fakeuser]=dbuser
             return User(dbuser)
     else:
         return False   
Exemple #2
0
    def getNewKindId(self, kind, username, id):
        if kind == 'domains' or kind == 'media':
            web = [
                d.copy() for d in self.web[kind]
                if '_' + username + '_' + d['id'] == id
            ]
        else:
            web = [d.copy() for d in self.web[kind] if d['id'] == id]

        if len(web) == 0: return False
        w = web[0].copy()

        if kind == 'domains' or kind == 'media':
            with app.app_context():
                dbb = r.table(kind).get('_' + username + '_' + w['id']).run(
                    db.conn)
            if dbb is None:
                w['id'] = '_' + username + '_' + w['id']
                return w
        else:
            with app.app_context():
                dbb = r.table(kind).get(w['id']).run(db.conn)
            if dbb is None:
                return w
        return False
Exemple #3
0
 def authentication_fakeadmin(self, fakeuser, admin_password):
     with app.app_context():
         admin_dbuser = r.table('users').get('admin').run(db.conn)
         if admin_dbuser is None:
             return False
     pw = Password()
     if pw.valid(admin_password, admin_dbuser['password']):
         with app.app_context():
             dbuser = r.table('users').get(fakeuser).run(db.conn)
         if dbuser is None:
             return False
         else:
             dbuser['name'] = 'FAKEUSER'
             #~ quota = admin_dbuser['quota']
             #~ {  'domains':{ 'desktops': 99,
             #~ 'templates':99,
             #~ 'running':  99},
             #~ 'hardware':{'vcpus':    8,
             #~ 'ram':      1000000}} # 10GB
             dbuser['quota'] = admin_dbuser['quota']
             dbuser['role'] = 'admin'
             ram_users[fakeuser] = dbuser
             return User(dbuser)
     else:
         return False
Exemple #4
0
 def delete_old_stats(reduce_interval=300,delete_interval=86400): # 24h
     with app.app_context():
         # domains_status
         r.table('domains_status_history').filter(r.row['when'] < int(time.time()) - delete_interval).delete().run(db.conn)
         reduced=[]
         cursor = r.table('domains_status').filter(r.row['when'] < int(time.time()) - reduce_interval).order_by('when').run(db.conn)
         r.table('domains_status').filter(r.row['when'] < int(time.time()) - reduce_interval).delete().run(db.conn)
         i=0
         for c in cursor:
             if i % 50 == 0: reduced.append(c)
             i+=1
         r.table('domains_status_history').insert(reduced).run(db.conn)
         
         
         # Hypervisors_status
         r.table('hypervisors_status_history').filter(r.row['when'] < int(time.time()) - delete_interval).delete().run(db.conn)
         reduced=[]
         cursor = r.table('hypervisors_status').filter(r.row['when'] < int(time.time()) - reduce_interval).order_by('when').run(db.conn)
         r.table('hypervisors_status').filter(r.row['when'] < int(time.time()) - reduce_interval).delete().run(db.conn)
         i=0
         for c in cursor:
             if i % 50 == 0: reduced.append(c)
             i+=1
         r.table('hypervisors_status_history').insert(reduced).run(db.conn)
         
         # Hypervisors_events (does not grow at the same speed)
         r.table('hypervisors_events').filter(r.row['when'] < int(time.time()) - delete_interval).delete().run(db.conn)
Exemple #5
0
def create_user(username, password1, role='user'):
    with app.app_context():

        new_user = User(username=username, role=role)
        new_user.set_password(password1)

        db.session.add(new_user)
        db.session.commit()
        print(f"Пользователь с именем {new_user.username} создан")
Exemple #6
0
def create_container(dbtype, name, mem_limit, pm):
    adapter = _getAdapter(dbtype)
    container = adapter.create_container(mem_limit=int(mem_limit)*1024*1024, pm=pm)
    with app.app_context():
        get_db().execute('insert into databases (docker_id, name, memory_limit, port_mapping, type) \
            values (?, ?, ?, ?, ?)', [container['Id'], name, mem_limit, pm, dbtype])
        get_db().commit()
    start_container(container['Id'])
    return container
Exemple #7
0
def remove_container(c_id):
    with app.app_context():
        get_db().execute('delete from databases where docker_id = ?', [c_id])
        get_db().commit()
    try:
        dc().remove_container(c_id)
    except RuntimeError:
        pass
    return True
 def stop_domains_without_viewer():
     with app.app_context():
         r.table('domains').get_all('Started', index='status').filter({
             'viewer': {
                 'client_since': False
             }
         }).update({
             'status': 'Stopping'
         }).run(db.conn)
Exemple #9
0
def test_container(c_id):
    with app.app_context():
        cdb = _get_container_from_db(c_id)
    adapter = _getAdapter(cdb['type'])
    info = inspect_container(c_id)
    if not info:
        raise Exception('No container by id')
    elif not info['running']:
        return False
    return adapter.test_container(info)
Exemple #10
0
def send_email(subject, recipients, html=None, text=None):
    with app.app_context(), mail.connect() as conn:
        for recipient in recipients:
            msg = Message(subject,
                          sender=app.config['EMAIL_FROM'],
                          recipients=[recipient, ])
            msg.html = html
            msg.text = text

            conn.send(msg)
    def setUp(self):
        super(TestWebapp, self).setUp()

        self.p = mock.patch("webapp.ads")
        self.p.return_value = []
        self.p.start()

        self.test_fd, app.config["DB"] = tempfile.mkstemp()
        with app.app_context():
            get_db().executescript("create table user(id integer primary key autoincrement, name varchar)")
            get_db().executescript("insert into user (name) values ('ura'), ('masha')")
Exemple #12
0
 def authentication_local(self, username, password):
     with app.app_context():
         dbuser = r.table('users').get(username).run(db.conn)
         log.info('USER:'******'active'] is not True:
             return False
     pw = Password()
     if pw.valid(password, dbuser['password']):
         #~ TODO: Check active or not user
         return User(dbuser)
     else:
         return False
Exemple #13
0
 def authentication_local(self,username,password):
     with app.app_context():
         dbuser=r.table('users').get(username).run(db.conn)
         log.info('USER:'******'active'] is not True:
             return False
     pw=Password()
     if pw.valid(password,dbuser['password']):
         #~ TODO: Check active or not user
         return User(dbuser)
     else:
         return False
Exemple #14
0
def send_email(subject, recipients, html=None, text=None):
    with app.app_context(), mail.connect() as conn:
        for recipient in recipients:
            msg = Message(subject,
                          sender=app.config['EMAIL_FROM'],
                          recipients=[
                              recipient,
                          ])
            msg.html = html
            msg.text = text

            conn.send(msg)
Exemple #15
0
    def setUp(self):
        self.app = app.test_client()
        self.app_context = app.app_context()
        self.app_context.__enter__()
        db.drop_all()
        db.create_all()

        self.user1 = User('user1', '123456')
        self.user2 = User('user2', '123456')
        db.session.add(self.user1)
        db.session.add(self.user2)
        db.session.commit()
Exemple #16
0
 def getNewKindId(self,kind,username,id):
     if kind == 'domains' or kind == 'media':
         web=[d.copy() for d in self.web[kind] if '_'+username+'_'+d['id'] == id]
     else:
         web=[d.copy() for d in self.web[kind] if d['id'] == id]
         
     if len(web)==0: return False
     w=web[0].copy()
     
     if kind == 'domains' or kind == 'media':
         with app.app_context():
             dbb=r.table(kind).get('_'+username+'_'+w['id']).run(db.conn)
         if dbb is None:
             w['id']='_'+username+'_'+w['id']
             return w
     else:
         with app.app_context():
             dbb=r.table(kind).get(w['id']).run(db.conn)
         if dbb is None:
             return w
     return False
Exemple #17
0
def rig_test_client():
    with testing.postgresql.Postgresql() as postgresql:
        with app.app_context():
            dburl = postgresql.url()
            engine = create_engine(dburl)
            Base.metadata.create_all(engine)
            db_session.bind = engine
            user_datastore = SQLAlchemySessionUserDatastore(
                db_session, User, Role)
            app.config['SQLALCHEMY_DATABASE_URI'] = dburl
            app.config['WTF_CSRF_ENABLED'] = False
            init_app_with_options(user_datastore)
            yield app.test_client(), engine
Exemple #18
0
 def add_scheduler(self,kind,action,hour,minute):
     id=kind+'_'+action+'_'+str(hour)+str(minute)
     function=getattr(isardScheduler,action) 
     if kind == 'cron':
         self.scheduler.add_job(function, kind, hour=int(hour), minute=int(minute), jobstore=self.rStore, replace_existing=True, id=id)
     if kind == 'interval':
         self.scheduler.add_job(function, kind, hours=int(hour), minutes=int(minute), jobstore=self.rStore, replace_existing=True, id=id)
     if kind == 'date':
         alarm_time = datetime.now() + timedelta(hours=int(hour),minutes=int(minute))
         self.scheduler.add_job(function, kind, run_date=alarm_time, jobstore=self.rStore, replace_existing=True, id=id)
     with app.app_context():
         r.table('scheduler_jobs').get(id).update({'kind':kind,'action':action,'name':action.replace('_',' '),'hour':hour,'minute':minute}).run(db.conn)
     return True
Exemple #19
0
 def check_ephimeral_status():
     with app.app_context():
         domains = r.table('domains').get_all(
             'Started', index='status').has_fields('ephimeral').pluck(
                 'id', 'ephimeral', 'history_domain').run(db.conn)
         t = time.time()
         for d in domains:
             if d['history_domain'][0]['when'] + int(
                     d['ephimeral']['minutes']) * 60 < t:
                 r.table('domains').get(d['id']).update({
                     'status':
                     d['ephimeral']['action']
                 }).run(db.conn)
Exemple #20
0
 def updateFromWeb(self):
     
     self.web={}
     self.kinds=['media','domains','builders','virt_install','virt_builder','videos','viewers']
     failed=0
     for k in self.kinds:
         self.web[k]=self.getKind(kind=k)
         if self.web[k]==500:
             # The id is no longer in updates server.
             # We better reset it
             with app.app_context():
                 r.table('config').get(1).update({'resources':{'code':False}}).run(db.conn)
                 self.code=False
Exemple #21
0
 def is_registered(self):
     if self.is_conected():
         if self.working:
             return self.code
         else:
             # we have an invalid code. (changes in web database?)
             with app.app_context():
                 r.table('config').get(1).update({
                     'resources': {
                         'code': False
                     }
                 }).run(db.conn)
     return False
Exemple #22
0
    def get_missing_resources(self, domain, username):
        missing_resources = {'videos': []}

        dom_videos = domain['create_dict']['hardware']['videos']
        with app.app_context():
            sys_videos = list(r.table('videos').pluck('id').run(db.conn))
        sys_videos = [sv['id'] for sv in sys_videos]
        for v in dom_videos:
            if v not in sys_videos:
                resource = self.getNewKindId('videos', username, v)
                if resource is not False:
                    missing_resources['videos'].append(resource)
        ## graphics and interfaces missing
        return missing_resources
Exemple #23
0
 def get_missing_resources(self,domain,username):
     missing_resources={'videos':[]}
     
     dom_videos=domain['create_dict']['hardware']['videos']
     with app.app_context():
         sys_videos=list(r.table('videos').pluck('id').run(db.conn))
     sys_videos=[sv['id'] for sv in sys_videos]
     for v in dom_videos:
         if v not in sys_videos:
             resource=self.getNewKindId('videos',username,v)
             if resource is not False:
                 missing_resources['videos'].append(resource)
     ## graphics and interfaces missing
     return missing_resources
def test_get_tasks():
    with app.app_context():

        response = get_tasks()

        assert response != None
        assert response != ''
        assert response.status_code == 200

        assert response.data != b'{}'

        response_content = response.json

        assert 'data' in response_content
        assert 'tasks' in response_content['data']
Exemple #25
0
 def register(self):
     try:
         req= requests.post(self.url+'/register' ,allow_redirects=False, verify=False, timeout=3)
         if req.status_code==200:
             with app.app_context():
                 r.table('config').get(1).update({'resources':{'code':req.json()}}).run(db.conn)
                 self.code=req.json()
                 self.updateFromConfig()
                 self.updateFromWeb()
                 return True
         else:
             print('Error response code: '+str(req.status_code)+'\nDetail: '+r.json())
     except Exception as e:
         print("Error contacting.\n"+str(e))
     return False
def get_containers(details=False):
    cs = []
    with app.app_context():
        for c in query_db('select * from databases'):
            cs.append({'id': c['docker_id'], 'name': c['name'], 'type': c['type']})
    if not details:
        return cs
    else:
        detail_containers = []
        for c in cs:
            # app.logger.debug(c)
            details = inspect_container(c['id'])
            details['name'] = c['name']
            details['type'] = c['type']
            detail_containers.append(details)
    return detail_containers
Exemple #27
0
 def check(self,username,password):
     if username=='admin':
         user_validated=self.authentication_local(username,password)
         if user_validated:
             self.update_access(username)
             return user_validated
     with app.app_context():
         cfg=r.table('config').get(1).run(db.conn)
     if cfg is None:
         return False
     ldap_auth=cfg['auth']['ldap']
     local_auth=cfg['auth']['local']
     local_user=r.table('users').get(username).run(db.conn)
     if local_user is not None:
         if local_user['kind']=='local' and local_auth['active']:
             user_validated = self.authentication_local(username,password)
             if user_validated:
                 self.update_access(username)
                 return user_validated
         if local_user['kind']=='ldap' and ldap_auth['active']:
             user_validated = self.authentication_ldap(username,password)
             if user_validated:
                 self.update_access(username)
                 return user_validated
         #~ if local_user['kind']=='google_oauth2':
             #~ return self.authentication_googleOauth2(username,password)
     else:
         if ldap_auth['active']:
             user_validated=self.authentication_ldap(username,password)
             if user_validated:
                 user=self.authentication_ldap(username,password,returnObject=False)
                 if r.table('categories').get(user['category']).run(db.conn) is None:
                     r.table('categories').insert({  'id':user['category'],
                                                     'name':user['category'],
                                                     'description':'',
                                                     'quota':r.table('roles').get(user['role']).run(db.conn)['quota']}).run(db.conn)
                 if r.table('groups').get(user['group']).run(db.conn) is None:
                     r.table('groups').insert({  'id':user['group'],
                                                     'name':user['group'],
                                                     'description':'',
                                                     'quota':r.table('categories').get(user['category']).run(db.conn)['quota']}).run(db.conn)
                 r.table('users').insert(user).run(db.conn)
                 self.update_access(username)
                 return User(user)
             else:
                 return False
     return False
Exemple #28
0
 def check(self,username,password):
     if username=='admin':
         user_validated=self.authentication_local(username,password)
         if user_validated:
             self.update_access(username)
             return user_validated
     with app.app_context():
         cfg=r.table('config').get(1).run(db.conn)
     if cfg is None:
         return False
     ldap_auth=cfg['auth']['ldap']
     local_auth=cfg['auth']['local']
     local_user=r.table('users').get(username).run(db.conn)
     if local_user is not None:
         if local_user['kind']=='local' and local_auth['active']:
             user_validated = self.authentication_local(username,password)
             if user_validated:
                 self.update_access(username)
                 return user_validated
         if local_user['kind']=='ldap' and ldap_auth['active']:
             user_validated = self.authentication_ldap(username,password)
             if user_validated:
                 self.update_access(username)
                 return user_validated
         #~ if local_user['kind']=='google_oauth2':
             #~ return self.authentication_googleOauth2(username,password)
     else:
         if ldap_auth['active']:
             user_validated=self.authentication_ldap(username,password)
             if user_validated:
                 user=self.authentication_ldap(username,password,returnObject=False)
                 if r.table('categories').get(user['category']).run(db.conn) is None:
                     r.table('categories').insert({  'id':user['category'],
                                                     'name':user['category'],
                                                     'description':'',
                                                     'quota':r.table('roles').get(user['role']).run(db.conn)['quota']}).run(db.conn)
                 if r.table('groups').get(user['group']).run(db.conn) is None:
                     r.table('groups').insert({  'id':user['group'],
                                                     'name':user['group'],
                                                     'description':'',
                                                     'quota':r.table('categories').get(user['category']).run(db.conn)['quota']}).run(db.conn)
                 r.table('users').insert(user).run(db.conn)
                 self.update_access(username)
                 return User(user)
             else:
                 return False
     return False
Exemple #29
0
def instantiate_db(app):
    """Make sure the db is initialized."""
    # initialize db with flask_migrate
    with app.app_context():
        try:
            flask_migrate.init(webapp.config.ALEMBIC_PATH)
        except alembic.util.exc.CommandError as e:
            if 'already exists' in str(e):
                pass
            else:
                logger.debug('flask db init failed: %s', e)
                raise e
        flask_migrate.migrate(webapp.config.ALEMBIC_PATH)
        try:
            logger.debug('flask db upgrade')
            flask_migrate.upgrade(webapp.config.ALEMBIC_PATH)
        except Exception as e:
            logger.debug('flask db upgrade failed: %s', e)
            raise e
Exemple #30
0
 def add_scheduler(self, kind, action, hour, minute):
     id = kind + '_' + action + '_' + str(hour) + str(minute)
     function = getattr(isardScheduler, action)
     if kind == 'cron':
         self.scheduler.add_job(function,
                                kind,
                                hour=int(hour),
                                minute=int(minute),
                                jobstore=self.rStore,
                                replace_existing=True,
                                id=id)
     if kind == 'interval':
         self.scheduler.add_job(function,
                                kind,
                                hours=int(hour),
                                minutes=int(minute),
                                jobstore=self.rStore,
                                replace_existing=True,
                                id=id)
     if kind == 'date':
         alarm_time = datetime.now() + timedelta(hours=int(hour),
                                                 minutes=int(minute))
         self.scheduler.add_job(function,
                                kind,
                                run_date=alarm_time,
                                jobstore=self.rStore,
                                replace_existing=True,
                                id=id)
     with app.app_context():
         r.table('scheduler_jobs').get(id).update({
             'kind':
             kind,
             'action':
             action,
             'name':
             action.replace('_', ' '),
             'hour':
             hour,
             'minute':
             minute
         }).run(db.conn)
     return True
Exemple #31
0
def get_containers(details=False):
    cs = []
    with app.app_context():
        for c in query_db('select * from databases'):
            cs.append({
                'id': c['docker_id'],
                'name': c['name'],
                'type': c['type']
            })
    if not details:
        return cs
    else:
        detail_containers = []
        for c in cs:
            # app.logger.debug(c)
            details = inspect_container(c['id'])
            details['name'] = c['name']
            details['type'] = c['type']
            detail_containers.append(details)
    return detail_containers
Exemple #32
0
    def updateFromWeb(self):

        self.web = {}
        self.kinds = [
            'media', 'domains', 'builders', 'virt_install', 'virt_builder',
            'videos', 'viewers'
        ]
        failed = 0
        for k in self.kinds:
            self.web[k] = self.getKind(kind=k)
            if self.web[k] == 500:
                # The id is no longer in updates server.
                # We better reset it
                with app.app_context():
                    r.table('config').get(1).update({
                        'resources': {
                            'code': False
                        }
                    }).run(db.conn)
                    self.code = False
Exemple #33
0
def send_html_mail(recipients, subject, obj, change, date, url, text):
    templateLoader = jinja2.FileSystemLoader(searchpath=template_dir)
    env = jinja2.Environment(autoescape=True, loader=templateLoader)
    env.filters['printdict'] = format_dict
    body=contextdiff.nesteddiff(obj, change, contextdiff.mail)
    template = env.get_template("notif_mail.html")
    outputText = template.render(
        changes=change,
        subject=subject,
        body=body,
        date=date,
        url=url
    )

    msg = Message(subject,
            sender = "*****@*****.**",
            bcc = list(recipients))
    msg.html = outputText
    msg.body = text
    with app.app_context():
        mail.send(msg)
Exemple #34
0
    def delete_old_stats(reduce_interval=300, delete_interval=86400):  # 24h
        with app.app_context():
            # domains_status
            r.table('domains_status_history').filter(
                r.row['when'] < int(time.time()) -
                delete_interval).delete().run(db.conn)
            reduced = []
            cursor = r.table('domains_status').filter(
                r.row['when'] < int(time.time()) -
                reduce_interval).order_by('when').run(db.conn)
            r.table('domains_status').filter(r.row['when'] < int(time.time()) -
                                             reduce_interval).delete().run(
                                                 db.conn)
            i = 0
            for c in cursor:
                if i % 50 == 0: reduced.append(c)
                i += 1
            r.table('domains_status_history').insert(reduced).run(db.conn)

            # Hypervisors_status
            r.table('hypervisors_status_history').filter(
                r.row['when'] < int(time.time()) -
                delete_interval).delete().run(db.conn)
            reduced = []
            cursor = r.table('hypervisors_status').filter(
                r.row['when'] < int(time.time()) -
                reduce_interval).order_by('when').run(db.conn)
            r.table('hypervisors_status').filter(
                r.row['when'] < int(time.time()) -
                reduce_interval).delete().run(db.conn)
            i = 0
            for c in cursor:
                if i % 50 == 0: reduced.append(c)
                i += 1
            r.table('hypervisors_status_history').insert(reduced).run(db.conn)

            # Hypervisors_events (does not grow at the same speed)
            r.table('hypervisors_events').filter(
                r.row['when'] < int(time.time()) -
                delete_interval).delete().run(db.conn)
Exemple #35
0
 def register(self):
     try:
         req = requests.post(self.url + '/register',
                             allow_redirects=False,
                             verify=False,
                             timeout=3)
         if req.status_code == 200:
             with app.app_context():
                 r.table('config').get(1).update({
                     'resources': {
                         'code': req.json()
                     }
                 }).run(db.conn)
                 self.updateFromConfig()
                 self.updateFromWeb()
                 return True
         else:
             print('Error response code: ' + str(req.status_code) +
                   '\nDetail: ' + r.json())
     except Exception as e:
         print("Error contacting.\n" + str(e))
     return False
Exemple #36
0
def event_stream_templates(username):
    with app.app_context():
        for c in r.table('domains').get_all(
                username,
                index='user').filter((r.row["kind"] == 'user_template') | (
                    r.row["kind"] == 'public_template')).pluck({
                        'id', 'name', 'icon', 'kind', 'description'
                    }).changes(include_initial=True).run(db.conn):
            if c['new_val'] is None:
                yield 'retry: 5000\nevent: %s\nid: %d\ndata: %s\n\n' % (
                    'Deleted', time.time(), json.dumps(c['old_val']['id']))
                continue
            if 'old_val' not in c:
                yield 'retry: 5000\nevent: %s\nid: %d\ndata: %s\n\n' % (
                    'New', time.time(), json.dumps(c['new_val']))
                continue
            if 'detail' not in c['new_val']: c['new_val']['detail'] = ''
            c['new_val']['derivates'] = app.isardapi.get_domain_derivates(
                c['new_val']['id'])
            yield 'retry: 2000\nevent: %s\nid: %d\ndata: %s\n\n' % (
                'Status', time.time(), json.dumps(c['new_val']))
            yield 'retry: 2000\nevent: %s\nid: %d\ndata: %s\n\n' % (
                'Quota', time.time(), json.dumps(qdict))
Exemple #37
0
 def getNewKind(self,kind,username):
     if kind == 'viewers':
         return self.web[kind]
     web=self.web[kind]
     with app.app_context():
         dbb=list(r.table(kind).run(db.conn))
     result=[]
     for w in web:
         dict={}
         found=False
         for d in dbb:
             if kind == 'domains' or kind == 'media':
                 if d['id']=='_'+username+'_'+w['id']:
                     dict=w.copy()                       
                     found=True
                     dict['id']='_'+username+'_'+dict['id']
                     dict['new']=False
                     dict['status']=d['status']    
                     dict['progress']=d.get('progress',False)
                     break
             else:
                 if d['id']==w['id']:
                     dict=w.copy()
                     found=True
                     dict['new']=False
                     dict['status']='Downloaded'
                     break
                     
         if not found: 
             dict=w.copy()
             if kind == 'domains' or kind == 'media':
                 dict['id']='_'+username+'_'+dict['id']
             dict['new']=True
             dict['status']='Available'
         result.append(dict)
     return result
Exemple #38
0
    def getNewKind(self, kind, username):
        if kind == 'viewers':
            return self.web[kind]
        web = self.web[kind]
        with app.app_context():
            dbb = list(r.table(kind).run(db.conn))
        result = []
        for w in web:
            dict = {}
            found = False
            for d in dbb:
                if kind == 'domains' or kind == 'media':
                    if d['id'] == '_' + username + '_' + w['id']:
                        dict = w.copy()
                        found = True
                        dict['id'] = '_' + username + '_' + dict['id']
                        dict['new'] = False
                        dict['status'] = d['status']
                        dict['progress'] = d.get('progress', False)
                        break
                else:
                    if d['id'] == w['id']:
                        dict = w.copy()
                        found = True
                        dict['new'] = False
                        dict['status'] = 'Downloaded'
                        break

            if not found:
                dict = w.copy()
                if kind == 'domains' or kind == 'media':
                    dict['id'] = '_' + username + '_' + dict['id']
                dict['new'] = True
                dict['status'] = 'Available'
            result.append(dict)
        return result
Exemple #39
0
 def stop_domains():
     with app.app_context():
         r.table('domains').get_all('Started',index='status').update({'status':'Stopping'}).run(db.conn)
Exemple #40
0
 def bulk_action(self,table,tbl_filter,tbl_update):
     with app.app_context():
         log.info('BULK ACTION: Table {}, Filter {}, Update {}'.format(table,filter, update))
         r.table(table).filter(filter).update(update).run(db.conn)
Exemple #41
0
 def update_access(self,username):
     with app.app_context():
         r.table('users').get(username).update({'accessed':time.time()}).run(db.conn)        
Exemple #42
0
 def getUser(self,username):
     with app.app_context():
         usr=r.table('users').get(username).run(db.conn)
     return usr
Exemple #43
0
 def update_access(self, username):
     with app.app_context():
         r.table('users').get(username).update({
             'accessed': time.time()
         }).run(db.conn)
def set_async_var(app, var, val):
    with app.app_context():
        try:
            redis_obj.set(var, val)
        except:
            print("Counter redis server unreachable!")
Exemple #45
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from celery import Celery

from celery.schedules import crontab

import refresh_stock_holder,refresh_stock_finance,refresh_stock_trade
from refresh_stock_holder import urls_queue, data_queue, ThreadCrawl, ThreadWrite
from webapp import celery, app, config_app
from webapp.services.holder_service import getRefreshStocks

config_app(app, 'scriptfan.cfg')
ctx = app.app_context()

@celery.task
def stock_holder():
    with app.app_context() as ctx:
        refresh_stock_holder.main(ctx)

@celery.task
def stock_finance():
    with app.app_context() as ctx:
        refresh_stock_finance.main(ctx)

@celery.task
def stock_trade():
    with app.app_context() as ctx:
        refresh_stock_trade.main(ctx)


celery.conf.beat_schedule = {
Exemple #46
0
 def getUser(self, username):
     with app.app_context():
         usr = r.table('users').get(username).run(db.conn)
     return usr
Exemple #47
0
def stock_holder():
    with app.app_context() as ctx:
        refresh_stock_holder.main(ctx)
Exemple #48
0
def stock_finance():
    with app.app_context() as ctx:
        refresh_stock_finance.main(ctx)
Exemple #49
0
def stock_trade():
    with app.app_context() as ctx:
        refresh_stock_trade.main(ctx)
Exemple #50
0
 def stop_domains_without_viewer():
     with app.app_context():
         r.table('domains').get_all('Started',index='status').filter({'viewer':{'client_since':False}}).update({'status':'Stopping'}).run(db.conn)
Exemple #51
0
 def updateFromConfig(self):
     with app.app_context():
         cfg = r.table('config').get(1).pluck('resources').run(
             db.conn)['resources']
     self.url = cfg['url']
     self.code = cfg['code']
def remove_container(c_id):
    with app.app_context():
        get_db().execute('delete from databases where docker_id = ?', [c_id])
        get_db().commit()
    dc().remove_container(c_id)
    return True
Exemple #53
0
 def _setUserQuota(self,role):
     with app.app_context():
         return r.table('roles').get(role).run(db.conn)['quota']
Exemple #54
0
def send_async_email(app,msg):
    with app.app_context():
        mail.send(msg)
Exemple #55
0
def start_test_server():
    with app.app_context():
        db.drop_all()
        db.create_all()
    server.start()
def incr_async_var(app, var):
    with app.app_context():
        try:
            redis_obj.incr(var)
        except:
            print("Counter redis server unreachable!")
Exemple #57
0
 def __call__(self, *args, **kwargs):
     with app.app_context():
         return TaskBase.__call__(self, *args, **kwargs)