示例#1
0
文件: views.py 项目: vishakh/metamkt
def user_delete(request):
    dbsession = DBSession()
    name = clean_matchdict_value(request, 'user')
    user = dbsession.query(User).filter(User.name == name).one()
    dbsession.delete(user)
    transaction.commit()
    return {'status': 'success'}
示例#2
0
    def filemonitoring(self):
        log = logging.getLogger(__name__)
        if self._request.params:
            # TODO: add this information to the file
            md5_enabled = True if 'withmd5' in self._request.params and self._request.params['withmd5'] == '0' else False
            all_files = self._request.params.getall('file')

            complete_file, filenames, folders = self._get_monitored_files(self._request.params['folder'] + '/')

            with transaction.manager:
                for f in all_files:
                    if f in complete_file:
                        log.debug('Skipping file {0}, because it is already monitored'.format(f))
                        continue
                    (path, filename) = os.path.split(f)
                    dbobj = MonitoredFile(path, filename, f)
                    DBSession.add(dbobj)
                DBSession.commit()
            files_not_mentioned = [c for c in complete_file if c not in all_files]
            # TODO: decide on this
            log.info('TODO: Still have to decide whether files which are not selected should be deleted or not.'
                     'Affected files would be: {0}'.format(files_not_mentioned))
        else:
            log.info('Got an empty request, going to redirect to start page')
            subreq = Request.blank('/')
            return self._request.invoke_subrequest(subreq)

        subreq = Request.blank(self._request.route_path('filebrowser'),
                               POST=dict(folder=self._request.params['folder'],
                                         currentfolder=self._request.params['currentfolder'],
                                         pathdescription='abs'))
        return self._request.invoke_subrequest(subreq)
示例#3
0
def login(request):
    """Returns a tuple of (authenticated) uid, and a message string.
    If the user could not be authenticated (or they didn't try), the uid
    will be None. If the user successfully logged in, sets a session
    cookie for this session."""
    
    session = DBSession()

    uid = authenticated_userid(request)
    if uid:
        return session.query(User).get(uid), None

    if 'user' in request.params and 'passwd' in request.params:
        uid = request.params['user']
        passwd = request.params['passwd']
        user = session.query(User).get(uid)
        if not user:
            return None, "Invalid user or password."

        if user.check_pw(passwd):
            headers = remember(request, uid)
            request.response.headerlist.extend(headers)
            return user, "Logged in Successfully"
        else:
            return None, "Invalid user or password."
    else:
        return None, None
示例#4
0
def post_entry(request):
	entry = m.Entry()
	entry.start_datetime = datetime.datetime.now()
	entry.updated_datetime = datetime.datetime.now()
	DBSession.add(entry)
	DBSession.flush()
	return entry.entry_id
示例#5
0
def make_admin(config_path):
    from intranet3.models import User
    user_login = sys.argv[-1]
    if len(sys.argv) < 4:
        print u"Provide user login"
        return

    session = DBSession()

    user = session.query(User).filter(User.email==user_login).first()

    if not user:
        print u"No such user: %s" % user_login
        return

    if 'admin' in user.groups:
        print u'Removing %s from group admin' % user.name
        groups = list(user.groups)
        groups.remove('admin')
        user.groups = groups
    else:
        print u'Adding %s to group admin' % user.name
        groups = list(user.groups)
        groups.append('admin')
        user.groups = groups

    session.add(user)
    transaction.commit()
示例#6
0
文件: views.py 项目: vishakh/metamkt
def user_get(request):
    dbsession = DBSession()
    name = clean_matchdict_value(request, 'user')
    user = dbsession.query(User).filter(User.name == name).one()
    user_data = dbsession.query(UserData).filter(UserData.user_id == user.id).one()
    user_json = convert.decodeUser(request, dbsession, user, user_data)
    return {'status': 'success', 'user': user_json}
示例#7
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """

    load_project_settings()

    session_factory = UnencryptedCookieSessionFactoryConfig(settings.get('session.secret', 'hello'))

    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    config = Configurator(session_factory=session_factory, settings=settings)
    config.add_tween('giteverywhere.auth.authenticator')
    config.include('pyramid_handlers')
    config.add_view('pyramid.view.append_slash_notfound_view',
                context='pyramid.httpexceptions.HTTPNotFound')
    config.add_static_view('static', 'static', cache_max_age=3600)

    config.add_route('home', '/')
    config.add_route('contact', '/contact')
    config.add_route('pyckauth_login', '/login')
    config.add_route('pyckauth_logout', '/logout')
    config.add_route('pyckauth_manager', '/auth')
    config.add_route('pyckauth_users', '/auth/users')
    config.add_route('pyckauth_permissions', '/auth/permissions')
    config.add_route('pyckauth_routes', '/auth/routes')
   


    add_admin_handler(config, DBSession, get_models(giteverywhere), 'admin.', '/admin', AdminController)  
    configure_app_routes(config)

    config.scan()

    return config.make_wsgi_app()
示例#8
0
文件: Parser.py 项目: P-Laf/ofxtools
    def instantiate(self):
        """ """
        if not hasattr(self, '_root'):
            raise ValueError('Must first call parse() to have data to instantiate')
        # SECLIST - list of description of securities referenced by
        # INVSTMT (investment account statement)
        seclist = self.find('SECLISTMSGSRSV1/SECLIST')
        if seclist is not None:
            self.securities = [
                sec.instantiate()
                for sec in seclist
            ]
            DBSession.add_all(self.securities)

        # TRNRS - transaction response, which is the main section
        # containing account statements
        #
        # N.B. This iteration method doesn't preserve the original
        # ordering of the statements within the OFX response
        for stmtClass in (BankStatement, CreditCardStatement, InvestmentStatement):
            tagname = stmtClass._tagName
            for trnrs in self.findall('*/%sTRNRS' % tagname):
                # *STMTTRNRS may have no *STMTRS (in case of error).
                # Don't blow up; skip silently.
                stmtrs = trnrs.find('%sRS' % tagname)
                if stmtrs is not None:
                    stmt = stmtClass(stmtrs)
                    self.statements.append(stmt)
示例#9
0
文件: auth.py 项目: AnamAtiq/CDSR
def is_allowed(request, routename, method='ALL', check_route=True):
    """
    Given a request_object, routename and method; returns True if current user has access to that route,
    otherwise returns False.

    If check_route if False, does not check the DB to see if the route is in the list of protected routes
    """

    if check_route:
        route = DBSession.query(RoutePermission).filter_by(route_name=routename).first()
        if not route:
            return True

    if not isinstance(method, list):
        method = [method, ]

    user_permissions = request.session.get('auth_user_permissions', [])
    if request.session.get('auth_static_permission', None):
        user_permissions.append(request.session.get('auth_static_permission', None))

    has_permission = DBSession.query(func.count(RoutePermission.permission)).filter(
                                            RoutePermission.route_name == routename).filter(
                                            RoutePermission.method.in_(method)).filter(
                                            RoutePermission.permission.in_(user_permissions)).scalar()

    if has_permission > 0:
        return True
    else:
        return False
示例#10
0
文件: Parser.py 项目: P-Laf/ofxtools
 def __init__(self, stmtrs):
     """ Initialize with *STMTRS Element """
     self.currency = stmtrs.find('CURDEF').text
     acctfrom = stmtrs.find(self._acctTag)
     self.account = acctfrom.instantiate()
     DBSession.add(self.account)
     self._init(stmtrs)
示例#11
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    config = Configurator(settings=settings)
    config.include('pyramid_mako')
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('index', '/')
    config.add_route('accueil', '/accueil')
    config.add_route('map', '/map')
    config.add_route('map_stop', '/map/{stop_id}')
    config.add_route('form_stop', '/form/{stop_id}')
    config.add_route('trip', '/trip/{trip_id}')
    config.add_route('test', '/test')
    config.add_route('test_map', '/test_map')
    config.add_route('stop_stop', '/stop/{stop_id}')
    config.add_route('savoir', '/savoir')
    config.add_route('json_stop', '/json_stop')
    config.add_route('json_stops', '/json_stops')
    config.add_route('json_hor', '/json_hor')
    config.add_route('json_map', '/json_map')
    config.add_route('json_form', '/json_form')
    config.add_route('json_trip', '/json_trip')
    config.scan()
    return config.make_wsgi_app()
示例#12
0
def oauth2callback(request):
    log.info('oauth2callback called at %s', request.url)

    # Get the auth code from the GET params
    try:
        code = request.GET['code']
        log.info('Successfully got auth code')
    except KeyError:
        log.error('Could not find auth code in GET params')
        return error_response(500, 'Sorry, but Google authorization failed.')

    # Exchange the auth code for a bearer/access token
    flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE,
                               redirect_uri=request.route_url('oauth2callback'))
    credentials = flow.step2_exchange(code)
    try:
        access_token = credentials.access_token
        refresh_token = credentials.refresh_token
        token_expiry = credentials.token_expiry
        if access_token is None or refresh_token is None or token_expiry is None:
            raise ValueError
    except (AttributeError, ValueError):
        log.error('Could not get access token, refresh token, and/or token expiry from exchanged credentials')
        return error_response(500, 'Sorry, but Google authorization failed.')
    DBSession.add(GDriveAccount(access_token, refresh_token, token_expiry))

    return Response('Successfully authorized with Google Drive!')
示例#13
0
文件: __init__.py 项目: ayesha928/fyp
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    
    load_project_settings()
    
    session_factory = UnencryptedCookieSessionFactoryConfig(settings.get('session.secret', 'hello'))
    
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    config = Configurator(session_factory=session_factory, settings=settings)
    config.include('pyramid_handlers')
    config.add_view('pyramid.view.append_slash_notfound_view',
                context='pyramid.httpexceptions.HTTPNotFound')
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.add_route('status', '/status')
    config.add_route('settings', '/settings')
    
    
    configure_app_routes(config)
    
    config.scan()
    
    return config.make_wsgi_app()
示例#14
0
文件: views.py 项目: m6394g/pyramid
def eventAdd(request):
    
    eventName = request.POST['name']
    eventType = request.POST['type']
    eventDescription = request.POST['description']
  
    image = request.POST.getall('file')
    for i in image:
	imageId = uuid.uuid1()
	imageUrl = str(imageId)
	open('/home/mohit/intern/hallwala/HallWala/hallwala/images/%d.jpg' % (imageId), 'wb').write(i.file.read())

    for infile in glob.glob("/home/mohit/intern/hallwala/HallWala/hallwala/images/*.jpg"):
        im = Image.open(infile)
	# don't save if thumbnail already exists
	if infile[0:2] != "T_":
	# convert to thumbnail image
		im.thumbnail((128, 128), Image.ANTIALIAS)
	        # prefix thumbnail file with T_
	        im.save("T_" + infile, "JPEG")



    newEvent = Event(eventName,eventType,eventDescription)
    DBSession.add(newEvent)
    DBSession.flush()
   
    event = newEvent.getJSON()
    return {'event' : event}
 def write_jobs_to_database(jobs):
     """
     Writes the retrieved jobs to the database
     :param jobs: list of all jobs
     :return:
     """
     log = logging.getLogger(__name__)
     with transaction.manager:
         for (classname, classmembers) in jobs.items():
             if len(classmembers) == 0:
                 continue
             for element in classmembers:
                 if 'error' in element:
                     continue
                 if 'JobID' not in element:
                     log.debug('Row didn''t match specified criteria {0}'.format(element))
                     continue
                 if not re.search('[0-9]*', element['JobID']):
                     log.debug('Row didn''t match specified criteria {0}'.format(element))
                     continue
                 dbrow = DBSession.query(Job).filter(Job.id == element['JobID']).all()
                 json_str = jsonpickle.encode(element)
                 if len(dbrow) == 0:
                     j = Job(element['JobID'], json_str)
                     DBSession.add(j)
                 elif len(dbrow) == 1:
                     dbrow[0].jobinfo = json_str
                 else:
                     log.error('More than one entry for jobid: {0}'.format(json_str))
         DBSession.commit()
    def job_archive():
        """
        Returns the job archive from the database
        :return:
        """
        jobs_stmt = DBSession.query(Job).order_by(Job.updatetime.desc()).all()
        jobs_dict = []
        for jobs in jobs_stmt:
            job_outputs = DBSession.query(JobOutput).filter(JobOutput.jobid == jobs.id).all()
            jobs = jobs.__dict__

            if 'jobinfo' in jobs and jobs['jobinfo'] is not None:
                obj = jsonpickle.decode(jobs['jobinfo'])
                try:
                    jobs.update(obj)
                except BaseException as e:
                    print(str(e))

            if 'jobdetails' in jobs and jobs['jobdetails'] is not None:
                obj = jsonpickle.decode(jobs['jobdetails'])
                try:
                    jobs.update(obj)
                except BaseException as e:
                    print(str(e))
            jobs['number_of_joboutputs'] = len(job_outputs)
            jobs['joboutputs'] = []
            for outputs in job_outputs:
                jobs['joboutputs'].append(outputs.__dict__)
            jobs_dict.append(jobs)
        return jobs_dict
示例#17
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    session_factory = UnencryptedCookieSessionFactoryConfig("lolololseekret")
    config = Configurator(settings=settings, session_factory=session_factory)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_static_view('graphs', 'graphs')
    config.add_route('login', '/login')
    config.add_route('logout', '/logout')
    config.add_route('meal', '/meals/{date}')
    config.add_route('meal_edit', '/meals/edit/{date}')
    config.add_route('weight', '/weight')
    config.add_route('weight_add', '/weight/add/')
    config.add_route('home', '/')
    config.add_route('chat_add', '/chat/add')
    config.add_route('note_add', '/notes/add')
    config.add_route('workout', '/workouts/{date}')
    config.add_route('workout_edit', 'workouts/edit/{date}')
    config.add_route('recipe', '/recipes')
    config.add_route('recipe_view', '/recipes/{id}')
    config.add_route('recipe_add', '/recipes/add')
    config.scan()
    return config.make_wsgi_app()
示例#18
0
文件: Parser.py 项目: P-Laf/ofxtools
    def instantiate(self, **extra_attrs):
        """ 
        Create an instance of a SQLAlchemy model class corresponding to
        my OFX tag, with attributes given by my contained OFX elements.

        If an instance that matches the given primary key signature has
        already been given, return that instead of creating a new one.
        """
        self.extra_attributes = extra_attrs
        # SECID needs to instantiate as SECINFO
        if self.tag == 'SECID':
            SubClass = models.SECINFO
        else:
            SubClass = getattr(models, self.tag)
        self._preflatten()
        self.attributes = self._flatten()
        self._postflatten()
        self.attributes.update(self.extra_attributes)
        self.extra_attributes = {}

        try:
            fingerprint = SubClass._fingerprint(**self.attributes)
            instance = DBSession.query(SubClass).filter_by(**fingerprint).one()
        except NoResultFound:
            instance = SubClass(**self.attributes)
            DBSession.add(instance)

        return instance
示例#19
0
    def job_output(self):
        log = logging.getLogger(__name__)

        jobid = self._request.matchdict['jobid']
        log.info('Request job output for id:{0}'.format(jobid))

        ssh_holder = self._request.registry.settings['ssh_holder']
        ssh_jobmanager = SSHBasedJobManager(ssh_holder)

        jobs = self._get_current_jobs(ssh_jobmanager, self._coltitles)
        joboutput = ssh_jobmanager.get_job_output(jobid)

        if 'error' in joboutput and joboutput['error'] is None:
            db_joboutput = JobOutput(id=jobid, output=jsonpickle.encode(joboutput))
            DBSession.add(db_joboutput)
            DBSession.commit()

        if 'error' in joboutput and joboutput['error'] is not None:
            jobresult = joboutput['error']
        else:
            jobresult = joboutput['content']
            jobresult = ''.join(jobresult)
        if 'type' not in joboutput:
            joboutput['type'] = 'type field missing'

        return {'project': self._projectname,
                'jobs': jobs,
                'output': dict(jobid=jobid, output=jobresult, type=joboutput['type'])}
示例#20
0
def init_db(config_path):
    setup_logging(config_path)
    settings = get_appsettings(config_path)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    print 'Done'
示例#21
0
文件: views.py 项目: vishakh/metamkt
def action_delete(request):
    dbsession = DBSession()
    name = clean_matchdict_value(request, 'action')
    action = dbsession.query(Action).filter(Action.name == name).one()
    dbsession.delete(action)
    transaction.commit()
    return {'status': 'success'}
示例#22
0
文件: views.py 项目: vishakh/metamkt
def team_delete(request):
    dbsession = DBSession()
    name = clean_matchdict_value(request, 'team')
    entity = dbsession.query(Entity).filter(Entity.name == name).filter(Entity.parent_id == None).one()
    dbsession.delete(entity)
    transaction.commit()
    return {'status': 'success'}
示例#23
0
def upload(request):
    if request.content_length/1000000 > 20:
        return error_response(400, 'Sorry, but the file must be under 20MB.')

    # Create photo object in database
    photo = Photo(datetime.today(), request.POST['file'].filename, request.client_addr, request.content_type, request.content_length)
    DBSession.add(photo)
    DBSession.flush()

    # Save uploaded file
    input_file = request.POST['file'].file
    input_file.seek(0)
    if not os.path.exists('data'):
        os.makedirs('data')
    if not os.path.exists('data/uploads'):
        os.makedirs('data/uploads')
    upload_path = os.path.join('data', 'uploads', str(photo.id))
    with open(upload_path, 'w') as f:
        shutil.copyfileobj(input_file, f)

    # Check the content type and rename as appropriate
    mime = magic.from_file(upload_path, mime=True)
    if mime not in ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/tiff', 'image/x-tiff']:
        resp = Response('Sorry, but we can only accept jpg, gif, or png files.')
        resp.status_code = 400
        resp.status_string = '400 Bad Request'
        return resp
    extension = {'image/jpeg': '.jpg', 'image/pjpeg': '.jpg',
                 'image/gif': '.gif', 'image/png': '.png',
                 'image/tiff': '.tiff', 'image/x-tiff': '.tiff'}[mime]
    os.rename(upload_path, upload_path + extension)
    photo.content_type = mime

    return Response('OK')
示例#24
0
文件: views.py 项目: vishakh/metamkt
def order_delete(request):
    dbsession = DBSession()
    name = clean_matchdict_value(request, 'order')
    order = dbsession.query(Order).filter(Order.id == id).one()
    dbsession.delete(order)
    transaction.commit()
    return {'status': 'success'}
示例#25
0
文件: views.py 项目: vishakh/metamkt
def group_delete(request):
    dbsession = DBSession()
    name = clean_matchdict_value(request, 'group')
    group = dbsession.query(Group).filter(Group.name == name).one()
    dbsession.delete(group)
    transaction.commit()
    return {'status': 'success'}
示例#26
0
文件: __init__.py 项目: lseek/empdb
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    LOG.debug("settings:{}".format(settings))
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(settings=settings)
    config.include('pyramid_chameleon')
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_static_view('jasny-css', 'static/jasny-bootstrap/css', cache_max_age=3600)
    config.add_static_view('jasny-js', 'static/jasny-bootstrap/js', cache_max_age=3600)
    config.add_route('login', '/')
    config.add_route('logout', '/logout')
    config.add_route('show_empl', '/employee/{emp_no}',
                     factory=EmployeeRootFactory)
    config.add_route('show_dept', '/department/{dept_no}',
                     factory=DeptRootFactory)

    auth_engine = AuthEngine()
    config.add_settings(auth_engine=auth_engine)
    authn_policy = AuthTktAuthenticationPolicy(settings['secret_key'],
                                               callback=auth_engine.get_groups,
                                               hashalg='sha512')
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(ACLAuthorizationPolicy())

    query_parser = FormQueryParser()
    config.add_settings(query_parser=query_parser)

    config.scan()

    return config.make_wsgi_app()
示例#27
0
文件: gcalendar.py 项目: tinawen/menu
def get_menu_desc(menu, for_calendar):
    desc = ''
    json_result = []
    if len(menu.menus) < 1:
        return ''
    for menu_item_id in menu.menus.split(' '):
        menu_item = DBSession.query(MenuItem).filter(MenuItem.id==menu_item_id).one()
        allergens = DBSession.query(Allergen).filter(Allergen.menu_item_id==menu_item.id).all()
        allergen_string = ', '.join([a.allergen for a in allergens])
        desc = '\n' + desc
        menu_item_name = menu_item.name.decode('utf8')
        desc = desc + menu_item_name
        if  menu_item.healthy:
            if for_calendar:
                desc = desc + '\n' + health_icon[menu_item.healthy-1].decode('utf8') + ' ' + HEALTHY_FACTOR[menu_item.healthy-1] + ' '
            else:
                desc = desc + '\n' + '(' + HEALTHY_FACTOR[menu_item.healthy-1] + ') '

        desc = desc + '\n'
        menu_item_desc = ''
        if len(menu_item.description.decode('utf8')):
            menu_item_desc = menu_item.description.decode('utf8')
            desc = desc + menu_item_desc + '\n'
        if len(allergen_string):
            desc = desc + '(' + allergen_string + ')\n\n'
        json_result.append((menu_item_name, menu_item_desc))
    return desc.strip('\n')
示例#28
0
文件: views.py 项目: vishakh/metamkt
def entity_type_delete(request):
    dbsession = DBSession()
    name = clean_matchdict_value(request, 'entity_type')
    entity_type = dbsession.query(EntityType).filter(EntityType.name == name).one()
    dbsession.delete(entity_type)
    transaction.commit()
    return {'status': 'success'}
示例#29
0
文件: views.py 项目: vishakh/metamkt
def event_delete(request):
    dbsession = DBSession()
    id = clean_matchdict_value(request, 'event')
    event = dbsession.query(Event).filter(Event.id == id).one()
    dbsession.delete(event)
    transaction.commit()
    return {'status': 'success'}
示例#30
0
文件: views.py 项目: tinawen/menu
def update_menu_item_desc(request):
    menu_item_id = request.matchdict['menu_item_id']
    menuItem = DBSession.query(MenuItem).filter(MenuItem.id==menu_item_id)
    new_desc = urllib.unquote(request.json_body).encode('utf8')
    menuItem.update({"description":new_desc})
    menu = DBSession.query(Menu).filter(Menu.id==menuItem.one().menu_id).one()
    update_gcalendar(menu)
    return 'ok'
示例#31
0
    def insert_data(self, data):
        """
        Will handle inserting data into the database
        """
        try:
            db_session = DBSession()
            # Check if comic is in database, if so update else create
            try:
                comic = db_session.query(Comic).filter(
                    Comic.comic_id == data.get('comic_id')).one()
            except NoResultFound:
                comic = Comic()

            comic.title = data.get('title')
            comic.comic_id = data.get('id')
            comic.alt = data.get('alt')
            comic.file_path = data.get('file_path')
            comic.posted_at = data.get('posted_at')
            comic.time_collected = data.get('time_collected')

            db_session.add(comic)
            db_session.commit()

        except Exception:
            db_session.rollback()
            logger.exception("Error adding to db {data}".format(data=data))
示例#32
0
 def get(self):
     items = DBSession.query(WordUsage).order_by(WordUsage.count.desc())
     self.render(
         'admin.html', **{
             'items': items,
             'title': 'Word Frequency',
             'page': 'words'
         })
示例#33
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(settings=settings)
    config.include('pyramid_chameleon')
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('tweets', '/tweets')
    config.add_view(root_view, 'tweets')
    config.add_route('topic', '/topic')
    config.add_view(enqueue_topic, 'topic')
    config.add_route('raw_topic', '/raw_topic')
    config.add_view(raw_topic_enqueue, 'raw_topic')
    # config.scan() significantly improves the runtime
    return config.make_wsgi_app()
示例#34
0
 def get(self):
     items = DBSession.query(UrlSentiment).order_by(UrlSentiment.url)
     self.render(
         'admin.html', **{
             'items': items,
             'title': 'Url Sentiments',
             'page': 'sentiment'
         })
示例#35
0
def my_view(request):
    try:
        one = DBSession.query(Note).filter(Note.note_id == 1).first()
    except DBAPIError:
        return Response(conn_err_msg,
                        content_type='text/plain',
                        status_int=500)
    return {'one': one, 'project': 'PyramidNotes'}
示例#36
0
def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    init_model()

    session_factory = session_factory_from_settings(settings)
    if 'localization' not in settings:
        settings['localization'] = 'id_ID.UTF-8'
    locale.setlocale(locale.LC_ALL, settings['localization'])
    if 'timezone' not in settings:
        settings['timezone'] = DefaultTimeZone
    config = Configurator(settings=settings,
                          root_factory='inventory.models.RootFactory',
                          session_factory=session_factory)
    config.include('pyramid_beaker')
    config.include('pyramid_chameleon')

    authn_policy = AuthTktAuthenticationPolicy('sosecret',
                                               callback=group_finder,
                                               hashalg='sha512')
    authz_policy = ACLAuthorizationPolicy()
    config.set_authentication_policy(authn_policy)
    config.set_authorization_policy(authz_policy)
    config.add_request_method(get_user, 'user', reify=True)
    config.add_request_method(get_title, 'title', reify=True)
    config.add_notfound_view(RemoveSlashNotFoundViewFactory())

    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_static_view('deform_static', 'deform:static')
    config.add_static_view('files', settings['static_files'])

    routes = DBSession.query(Route.kode, Route.path, Route.nama).all()
    for route in routes:
        config.add_route(route.kode, route.path)
        if route.nama:
            titles[route.kode] = ' - '.join([main_title, route.nama])

    config.scan()

    app = config.make_wsgi_app()
    from paste.translogger import TransLogger
    app = TransLogger(app, setup_console_handler=False)
    return app
示例#37
0
def crawl_problem_plats():
    """
    问题平台 https://shuju.wdzj.com/problem-1.html
    """
    url = "https://shuju.wdzj.com/problem-list-all.html"
    params = {"year": ""}
    response = requests.get(url, params=params, headers=HEADERS)
    json_data = response.json()
    problem_plats = json_data.get('problemList')

    for problem_plat in problem_plats:
        session = DBSession()
        plat_id = problem_plat.get('platId')
        wdzj_id = problem_plat.get('wdzjPlatId')
        plat_name = problem_plat.get('platName')
        if wdzj_id != 0:
            session.execute(
                """
                INSERT INTO products
                    (plat_id, wdzj_id, name)
                    select
                    '{plat_id}', '{wdzj_id}', '{plat_name}'
                WHERE not EXISTS (SELECT *
                    FROM products
                    WHERE plat_id = '{plat_id}');
                """.format(
                    plat_id=plat_id, wdzj_id=wdzj_id, plat_name=plat_name
                )
            )
        new_problem_plat = ProblemPlat(
            plat_id=problem_plat.get('platId'),  # plat_id
            wdzj_id=problem_plat.get('wdzjPlatId'),  # wdzj_id
            plat_name=problem_plat.get('platName'),  # plat_name
            area=problem_plat.get('area'),  # 地区
            oneline_time=problem_plat.get('onlineTime'),  # 上线时间
            problem_date=problem_plat.get('problemTime'),  # 问题时间
            event_type=problem_plat.get('type'),  # 事件类型
            people_num=problem_plat.get('peopleNumber'),
            status1=problem_plat.get('status1'),  # 保留字段status1
            status2=problem_plat.get('status2')  # 保留字段status2
        )


        session.add(new_problem_plat)
        session.commit()
        session.close()
示例#38
0
 def get_files_by_status(self, ids_list, flag):
     if (ids_list):
         if (flag):
             result = DBSession.query(Filedbentity).filter(
                 Filedbentity.dbentity_id.in_(ids_list),
                 Filedbentity.is_public == True,
                 Filedbentity.s3_url != None,
                 Filedbentity.readme_file_id != None,
                 Filedbentity.dbentity_status == "Active").all()
             return result
         else:
             result = DBSession.query(Filedbentity).filter(
                 Filedbentity.dbentity_id.in_(ids_list),
                 Filedbentity.is_public == True,
                 Filedbentity.s3_url != None,
                 Filedbentity.readme_file_id != None).all()
             return result
示例#39
0
文件: views.py 项目: Eugerome/BP_app
 def get_record(self):
     """Retrieve record based on record_id."""
     record_id = self.request.matchdict["record_id"]
     # shouldn't be any duplicate record_id since primary key
     record = DBSession.query(Record).filter_by(record_id=record_id).first()
     if record:
         return record.to_dict()
     return Response(status=404)
示例#40
0
def update_patient(id):
    content = request.get_json(silent=True)
    session = DBSession()
    patient = session.query(Patient).filter_by(id=id).one()
    for key, value in content.iteritems():
        setattr(patient, key, value)
    stmt = session.add(patient)
    session.commit()
    session.close()
    return jsonify()
示例#41
0
class LagouPipeline(object):
    def __init__(self):
        self.session = DBSession()
        self.pool = redis.Redis(host='raspberrypi', port=6379, db=2)

    def process_item(self, item, spider):
        if isinstance(item, LagouItem):

            if self.session.query(Jobs).filter(
                    Jobs.positionId == item['positionId'],
                    Jobs.companyId == item['companyId']).first():
                pass
            else:
                obj = Jobs(
                    companyId=item['companyId'],
                    positionId=item['positionId'],
                    jobNature=item['jobNature'],
                    companyName=item['companyName'],
                    financeStage=item['financeStage'],
                    companyFullName=item['companyFullName'],
                    companySize=item['companySize'],
                    industryField=item['industryField'],
                    positionName=item['positionName'],
                    city=item['city'],
                    createTime=item['createTime'],
                    salary_low=item['salary_low'],
                    salary_high=item['salary_high'],
                    workYear=item['workYear'],
                    education=item['education'],
                    positionAdvantage=item['positionAdvantage'],
                    district=item['district'],
                    # uid=item['uid'],
                    companyLabelList=item['companyLabelList'],
                )
                self.session.add(obj)
                try:
                    self.session.commit()
                except Exception, e:
                    print e
                    self.session.rollback()

        # self.session.close()

        elif isinstance(item, CompanyItem):
            # 公司信息存入mysql数据库
            '''
            obj=Company(
                companyId=item['companyId'],
                companyName=item['companyFullName']
            )
            self.session.add(obj)
            try:
                self.session.commit()
            except Exception, e:
                print e
                self.session.rollback()
            '''

            # 公司的数据存入redis
            self.pool.set(item['companyId'], item['companyFullName'])
示例#42
0
def crawl_plat_first_letter(shuju_date="2020-01-062020-01-12"):
    """
    平台成交数据 https://shuju.wdzj.com/platdata-1.html
    """
    url = "https://shuju.wdzj.com/plat-data-custom.html"
    form_data = {
        "type": 0,
        "shujuDate": shuju_date
    }
    response = requests.post(url, data=form_data)
    status = response.status_code
    if status != 200:
        print("crawl failed. (status is not 200)")
        raise CrawlFailed('crawl failed')
    plats_data = response.json()
    for plat_data in plats_data:
        plat_id = plat_data.get('platId')
        wdzj_id = plat_data.get('wdzjPlatId')
        first_letter = plat_data.get('firstLetter')
        session = DBSession()
        if wdzj_id != 0:
            product = session.query(Product).filter_by(plat_id=plat_id).first()
            product.first_letter = first_letter

        session.commit()
        session.close()
示例#43
0
def getUser(request):

    username = request.headers["X-Webauth-User"]
    #now get the uuid
    uuid = ""
    print "UserName:"******"UUID:", uuid

    if uuid == "":
        user = DBSession.query(Owners).filter_by(username=username).all()
    else:
        user = DBSession.query(Owners).filter_by(uuid=uuid).all()

    if len(user) == 0:
        cur = Owners(username, uuid)
        DBSession.add(cur)
        DBSession.commit()
        user = DBSession.query(Owners).filter_by(username=username).all()

        print "Made"

    if len(user) == 1:
        print "Found"
        cur = user[0]

    # if user == "zemon1":
    #     user = "******"

    return cur
示例#44
0
def main(argv=sys.argv):  # pylint: disable=W0102
    """Initial database setup."""
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, "sqlalchemy.")
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    with transaction.manager:
        current_time = datetime.utcnow()
        for i in range(14):
            record = Record()
            record.timestamp = current_time - timedelta(days=i)
            record.bp_upper = random.randint(100, 160)
            record.bp_lower = random.randint(50, 80)
            DBSession.add(record)  # pylint: disable=E1101
示例#45
0
def keywords (request):
    mysql = DBSession ()

    form = KeywordForm (request.POST)

    if form.submit.data:
        pass

    return {'form': form}
示例#46
0
 def test_delete_success(self):
     user_data = {'email': '*****@*****.**', 'password': '******'}
     user = create_object(User, **user_data)
     note = create_object(Note, name='note', text='text', user=user)
     self.signin(**user_data)
     self.testapp.post('/notes/{}/delete/'.format(note.id),
                       {'name': 'new name'},
                       status=302)
     self.assertEquals(0, DBSession.query(Note).count())
示例#47
0
 def test_delete_success(self):
     user_data = {'email': '*****@*****.**', 'password': '******'}
     user = create_object(User, **user_data)
     pad = create_object(Pad, name='pad', user=user)
     self.signin(**user_data)
     self.testapp.post('/pads/{}/delete/'.format(pad.id),
                       {'name': 'new name'},
                       status=302)
     self.assertEquals(0, DBSession.query(Pad).count())
示例#48
0
def main(global_config, **settings):
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    authentication_policy = AuthTktAuthenticationPolicy('somesecret')
    authorization_policy = ACLAuthorizationPolicy()
    config = Configurator(settings=settings,
                          authentication_policy=authentication_policy,
                          authorization_policy=authorization_policy)
    config.include('pyramid_mako')
    config.include('.models')
    config.add_route('index', '/')
    config.add_route('admin','/admin')
    config.add_route('blog_action', '/blog/{action}',
                     factory='.models.Root')
    config.add_route('blog_view','/blogview')
    config.scan()
    return config.make_wsgi_app()
示例#49
0
def _response_dict(request, *args, **kwargs):
    return dict(
        logged_in=authenticated_userid(request),
        pads=DBSession.query(Pad).filter(
          Pad.user == get_current_user(request)
        ).all(),
        snippets=get_renderer('templates/snippets.pt').implementation(),
        **kwargs
    )
示例#50
0
 def get(self):
     uid = self.request.matchdict['uid']
     server = DBSession.query(Server).filter_by(uid=uid).one()
     return {
         'uid': server.uid,
         'address': server.address,
         'port': server.port,
         'useSSL': server.useSSL
     }
示例#51
0
def groupfinder(userid, request):
    roles = DBSession.query(UserRole).filter_by(user_id=userid).all()

    if roles is not None and len(roles) != 0:
        principals = []
        for role in roles:
            if role.role_name == "Admin":
                principals.append('g:admin')
        return principals
示例#52
0
文件: company.py 项目: blockmov/BMTF
 def on_set_admin_user(cls, company_id, user_id):
     """ 设置公司管理用户 """
     session = DBSession()
     com = session.query(cls).filter(cls.company_id == company_id).first()
     if com:
         com.admin_user_id = user_id
         try:
             session.add(com)
             session.commit()
         except Exception as why:
             session.rollback()
             message = "companies.on_set_admin_user: {0}".format(why)
             logging.info(message)
             return -1
     return 0
示例#53
0
    def get_files_helper(self):

        result = DBSession.query(Filedbentity).all()
        obj = {}
        for item in result:
            res = item.to_dict()
            if item.dbentity_id not in obj:
                obj[item.dbentity_id] = []
            obj[item.dbentity_id].append(res)
        return obj
示例#54
0
文件: app.py 项目: HuuBaa/huublog
def authenticate_api():
    email = request.form['email'].encode('utf8')
    passwd = request.form['passwd'].encode('utf8')
    sess = DBSession()
    try:
        user = sess.query(Users).filter(Users.email == email).one()
    except:
        return 'emailError'
    sess.close()
    s = '%s:%s' % (email, passwd)
    passwd = hashlib.sha1(s.encode('utf8')).hexdigest()
    if passwd == user.passwd:
        cookie_str = '%s-%s-%s' % (user.id, user.email, user.passwd)
        L = [user.id, hashlib.sha1(cookie_str.encode('utf8')).hexdigest()]
        resp = make_response()
        resp.set_cookie('huusession', '-'.join(L))
        return resp
    else:
        return 'passwordError'
示例#55
0
文件: app.py 项目: HuuBaa/huublog
def queryAllDesc(table_class, offset=None, limit=None):
    if offset is None and limit is None:

        sess = DBSession()
        qClass = sess.query(table_class).order_by(
            table_class.create_at.desc()).all()
        sess.close()
    else:
        sess = DBSession()
        qClass = sess.query(table_class).order_by(
            table_class.create_at.desc()).offset(offset).limit(limit).all()
        sess.close()

    for user in qClass:
        user.passwd = '******'
    qClass_list = []
    for i in range(len(qClass)):
        qClass_list.append(qClass[i].to_dict())
    return qClass_list
示例#56
0
    def test_edit_success(self):
        user_data = {'email': '*****@*****.**', 'password': '******'}
        user = create_object(User, **user_data)
        self.signin(**user_data)

        pad = create_object(Pad, name='pad', user=user)
        new_name = 'new pad name'
        self.testapp.post('/pads/{}/edit/'.format(pad.id), {'name': new_name},
                          status=302)
        self.assertEquals(new_name, DBSession.query(Pad).get(pad.id).name)
示例#57
0
def create_translation(client_id, contents=list(), gist_type='Service'):
    gist = TranslationGist(client_id=client_id, type=gist_type)
    DBSession.add(gist)
    DBSession.flush()
    for content in contents:
        atom = TranslationAtom(client_id=client_id,
                               content=content[0],
                               locale_id=content[1],
                               parent=gist)
        DBSession.add(atom)
    DBSession.flush()
    return gist
示例#58
0
def pad_notes(request):
    pad_id = request.matchdict['pad_id']
    pad = DBSession.query(Pad).filter(
        Pad.id == pad_id,
        Pad.user_id == get_current_user(request).id
    ).first() or raise_404(text='Pad not found')
    pad_notes = pad.notes.order_by(
        _get_order_by(request.params.get('order'))
    ).all()
    return _response_dict(request, pad=pad, pad_notes=pad_notes)
示例#59
0
文件: company.py 项目: blockmov/BMTF
 def on_set_business_hours(cls, company_id, opening_hours, closing_hours):
     """ 设置公司营业时间 """
     session = DBSession()
     com = session.query(cls).filter(cls.company_id == company_id).first()
     if com:
         com.opening_hours = opening_hours
         com.closing_hours = closing_hours
         try:
             session.add(com)
             session.commit()
         except Exception as why:
             session.rollback()
             message = "companies.on_set_business_hours: {0}".format(why)
             logging.info(message)
             return -1
     return 0
示例#60
0
def save_to_log(from_who='user',
                message_type=None,
                message=None,
                comment_text='',
                msg_text=''):
    """Сохранить в лог. Внимательно передавать from_who

    from_who - 'bot', 'user', 'system'. От кого сообщение
    message - тип message. Сообщение от пользователя.
    comment_text - дополнительный текст.
    msg_text - текст сообщения. Использовать для сохранения ответа бота на message пользователя

    Примеры.
    save_to_log('user', message) - сохранить сообщение от пользователя.
    save_to_log('system', comment_text=err_text) - сохранить сообщение от системы. Например, об ошибке.
    save_to_log('bot', message=message_from_user, msg_text=bot_msg_text) - сохранить сообщение от бота пользоателю.
    """
    if from_who not in ('bot', 'user', 'system'):
        comment_text += ' ' + from_who
        from_who = 'need_help'
    operation = None
    tid = None
    session = DBSession()
    if message:
        tid = message.from_user.id
        if from_who == 'user':
            if message.content_type == 'text':
                msg_text = message.text
            if message.content_type == 'contact':
                msg_text = str(message.contact)

        operation = session.query(Operation).filter_by(telegramid=tid).first()

    if operation is None: operation = Operation()
    log = Log(datetime=datetime.datetime.now(),
              from_who=from_who,
              user_id=tid,
              msg_text=msg_text,
              msg_type=message_type,
              operation=operation.current_operation,
              status=operation.operation_status,
              additional_info=operation.additional_info_db,
              function=inspect.stack()[1][3],
              comment=comment_text)
    session.add(log)
    session.commit()
    session.close()