Example #1
0
def registration_controller(request):
    errors = {}
    is_valid = True
    data = request.get('data')

    if 'login' not in data:
        errors.update({'login': '******'})
        is_valid = False
    if 'password' not in data:
        errors.update({'password': '******'})
        is_valid = False

    if not is_valid:
        response = make_response(request, 400, errors)
        return response

    hmac_obj = hmac.new(SECRET_KEY.encode(), data.get('password').encode())
    password_digest = hmac_obj.digest()

    with session_scope() as db_session:
        user = User(name=data.get('login'), password=password_digest)
        db_session.add(user)
    token = login(request, user)
    response = make_response(request, 200, {'token': token})
    return response
Example #2
0
    def setupRecipes(self, recipes, selected=None):
        tree = self.recipesList

        try:
            tree.DeleteChildren(self.recipesRoot)
        except:
            self.recipesRoot = tree.AddRoot(_("Recipes"))

        if recipes["recipes"]:
            self.addRecipesToTree(tree, self.recipesRoot, recipes,
                                  self.icons[FOLDER_ICON],
                                  self.icons[FOLDER_OPEN_ICON],
                                  self.icons[FILE_ICON])

            item, cookie = tree.GetFirstChild(tree.GetRootItem())
            while item and tree.ItemHasChildren(item):
                item, cookie = tree.GetFirstChild(item)
            with session_scope(self.database) as session:
                recipe = session.query(Recipe)\
                    .get(self.recipesList.GetPyData(item))

                self.setStatusBarText(recipe)
                self.showRecipe(recipe, self.recipe_panel)
        else:
            self.clearRecipe(self.recipe_panel)
Example #3
0
    def commit_reddit(self, unique_id, synonyms, text, author, subreddit,
                      date):
        with session_scope() as session:
            if self.post_exists(session, unique_id):
                return False

            synonyms = [
                self.get_synonym(session, synonym) for synonym in synonyms
            ]
            if None in synonyms:
                raise RuntimeError("Synonyms missing from the database.")

            hashed_author = self.hash_identifier(author)
            hashed_id = self.hash_identifier(unique_id)

            reddit_post = RedditPost(author_id=hashed_author,
                                     subreddit=subreddit,
                                     synonyms=synonyms,
                                     date=date,
                                     contents=text,
                                     id=hashed_id)

            session.add(reddit_post)
            session.commit()

            return True
 def get_max_id_from_viewed_candidates_and_category(self, category_id,
                                                    company_id):
     with session_scope() as session:
         return session.query(ViewedCandidatesByCompanyModel.candidate_id).join(CandidateModel).\
             filter(ViewedCandidatesByCompanyModel.company_id.like(company_id),
                    CandidateModel.field_of_work.like(category_id)).\
             order_by(ViewedCandidatesByCompanyModel.viewed_id.desc()).first()
Example #5
0
 def add_ocena(ucenik, predmet, vrednost_ocene):
     check_type(ucenik, Ucenik)
     check_type(predmet, Predmet)
     if not isinstance(vrednost_ocene,
                       int) or vrednost_ocene < 1 or vrednost_ocene > 5:
         raise ValueError('vrednost_ocene is out of predefined domain')
     daos = {'korisnik': None, 'predmet': None}
     try:
         with session_scope() as session:
             daos['korisnik'] = DAOManager.get_korisnik_dao(session)
             daos['predmet'] = DAOManager.get_predmet_dao(session)
             slusa = daos['korisnik'].get_uceniks_predmets_slusa(
                 ucenik, predmet)
             if slusa is None:
                 raise UpdateInfoError('Ucenik ne slusa uneti predmet')
             ocena = Ocena(vrednost_ocene, slusa)
             daos['predmet'].add_ocena(ocena)
             return True
     except UpdateInfoError:
         raise
     except:
         return False
     finally:
         for dao in daos.values():
             DAOManager.release(dao)
 def sign_up(self, name, email, password, description):
     with session_scope() as session:
         session.add(
             CompanyModel(name=name,
                          email=email,
                          password=password,
                          description=description))
def registration_controller(request):
    errors = {}
    is_valid = True
    request = get_login_pass_from_data(request)
    print(f'request-updated: {request}')

    if not 'password' in request:
        errors.update({'password': '******'})
        is_valid = False
    if not 'login' in request:
        errors.update({'login': '******'})
        is_valid = False

    if not is_valid:
        return make_response(request, 400, {'errors': errors})

    hmac_obj = hmac.new(SECRET_KEY, request.get('password').encode())
    password_digest = hmac_obj.digest()

    with session_scope() as db_session:
        user = User(name=request.get('login'), password=password_digest)
        db_session.add(user)

    token = login(request, user)

    return make_response(request, 200, {'token': token})
def registration_controller(request):
    errors = {}
    is_valid = True
    data = request.get('data')

    if not 'password' in data:
        errors.update({'password': '******'})
        is_valid = False
    if not 'login' in data:
        errors.update({'login': '******'})
        is_valid = False

    if not is_valid:
        return make_response(request, 400, {'errors': errors})

    # сокрытие пароля для хранения на сервере
    hmac_obj = hmac.new(SECRET_KEY.encode(), data.get('password').encode())
    password_digest = hmac_obj.digest()

    with session_scope() as db_session:
        user = User(name=data.get('login'), password=password_digest)
        db_session.add(user)

    token = login(request, user)
    return make_response(request, 200, {'token': token})
def make_response(request, code, data=None):

    action = request.get('action')
    token = request.get('token')

    result = {
        'action': action,
        'time': request.get('time'),
        'token': token,
        'data': data,
        'code': code
    }

    # print(f'action={action}')
    # print(f'token={token}')

    if action == 'echo' and token:
        with session_scope() as db_session:
            user_session = db_session.query(UserSession).filter_by(
                token=token).first()
            # print(f'user_session={user_session}')
            if user_session and not user_session.closed:
                result['username'] = user_session.user.name

    return result
Example #10
0
def authenticate(login, password):
    with session_scope() as db_session:
        user = db_session.query(User).filter_by(name=login).first()
        hmac_obj = hmac.new(SECRET_KEY.encode(), password.encode())
        password_digest = hmac_obj.hexdigest()
        if user and hmac.compare_digest(password_digest, user.password):
            return user
Example #11
0
def delete_message_controller(request):
    data = request.get('data')
    message_id = data.get('message_id')
    with session_scope() as session:
        message = session.query(Message).filter_by(id=message_id).first()
        session.delete(message)
        return make_response(request, 200)
Example #12
0
def get_items():
    with session_scope() as session:
        entries = [{
            "title": e.title,
            "link": e.link,
            "image": e.image
        } for e in session.query(Entry).all()]
    return entries
Example #13
0
def index():
    with session_scope() as db_session:
        data = db_session.query(Log).all()
        # when using the object retrieved from the database outside
        # the with-scope, use the line below to extract the data.
        db_session.expunge_all()

    return render_template('index.html', data=data)
Example #14
0
def echo_controller(request):
    data = request.get('data')
    message = Message(data=data)
    with session_scope() as db_session:
        db_session.add(message)

        response = make_response(request, 200, data)
        return response
Example #15
0
 def write_message_to_candidate(self, candidate_id, company_id, message):
     with session_scope() as session:
         session.add(
             MessageModel(company_id=company_id,
                          candidate_id=candidate_id,
                          send_by=0,
                          seen=0,
                          message=message))
Example #16
0
 def get_all_messages_with_candidate(self, candidate_id, company_id):
     with session_scope() as session:
         session.query(MessageModel).filter(
             MessageModel.candidate_id.like(candidate_id),
             MessageModel.company_id.like(company_id)).all()
         session.query(MessageModel).filter(MessageModel.candidate_id.like(candidate_id),
                                            MessageModel.company_id.like(company_id)).\
             update({MessageModel.seen: 1}, synchronize_session=False)
Example #17
0
def logout_controller(request):
    with session_scope() as db_session:
        user_session = db_session.query(Session).filter_by(
            token=request.get('token')).first()
        user_session.closed = datetime.now()

        response = make_response(request, 200, 'Session closed')
        return response
Example #18
0
    def saveRecipe(self, edit_recipe):
        self.setStatusBarText(_("saving recipe"))
        errors = False

        ingredients = edit_recipe.get_ingredients()
        if ingredients is None:
            errors = True

        title = edit_recipe.get_name()
        if not title:
            errors = True
            #TODO: display error message

        if not errors:
            with session_scope(self.database) as session:
                logging.debug("user %s" % session.query(User).get(self.userId))
                recipe = None
                if edit_recipe.get_recipe_id():
                    recipe = session.query(Recipe)\
                        .get(edit_recipe.get_recipe_id())
                if not recipe:
                    recipe = Recipe()
                recipe.title = title
                recipe.description = edit_recipe.get_description()
                recipe.algorythm = edit_recipe.get_algorythm()
                recipe.time = edit_recipe.get_time()
                recipe.difficulty = edit_recipe.get_difficulty()
                recipe.groups = session.query(Group)\
                    .filter(Group.id.in_(edit_recipe.get_groups())).all()
                recipe.user = session.query(User).get(self.userId)

                if not recipe.id:
                    self.setStatusBarText(session.add(recipe))
                elif recipe.ingredients:
                    for ingredient in recipe.ingredients:
                        session.delete(ingredient)
                    recipe.ingredients = []
                    session.flush()

                for substance, amount, unit in ingredients:
                    try:
                        session.add(Ingredient(recipe=recipe,
                                               substance=self.database
                                               .getSubstance(substance,
                                                             session),
                                               unit=self.database
                                               .getUnit(unit,
                                                        session),
                                               amount=amount))
                    except sqlalchemy.exc.IntegrityError as e:
                        logging.error(e)

            current = self.tabsContainer.GetSelection()
            self.tabsContainer.SetSelection(self.tabs["recipes"])
         #TODO: this should be a delete, but that causes everything to blow up
            self.tabsContainer.RemovePage(current)
        else:
            self.setStatusBarText(_("errors"))
Example #19
0
def get_messages_controller(request):
    with session_scope() as db_session:
        messages = reduce(
            lambda value, item: value + [{'data': item.data, 'created': item.created.timestamp()}],
            db_session.query(Message).all(),
            []
        )
        response = make_response(request, 200, messages)
        return response
Example #20
0
    def get_new_posts(self, synonym=None, with_sentiment=False, limit=None):
        with session_scope() as session:
            query = session.query(Post).filter(Post.sentiment.is_(None))
            if limit:
                query = query.order_by(Post.date).limit(limit)

            posts = query.all()

            return {post.id: post.contents for post in posts}
Example #21
0
    def commit_synonyms(self, synonyms):
        with session_scope() as session:
            for synonym in synonyms:
                if self.get_synonym(session, synonym):
                    continue

                session.add(Synonym(name=synonym))

            session.commit()
Example #22
0
 def __set_job_record_status(self, job_id, status, result = None):
   job_record = self.__get_job_record(job_id)
   job_record.job_status = status
   if status in [QueuedJobStatus.FAIL, QueuedJobStatus.SUCCESS]:
     job_record.job_result = json.dumps(result)
     job_record.timestamp = datetime.datetime.now()
   with session_scope() as db_session:
     db_session.add(job_record)
     db_session.commit()
Example #23
0
def plot_intersections_curvature(punctured_region_size):
    with session_scope() as s:
        statement = s.query(Result).filter(
            Result.punctured_region_size == punctured_region_size).statement
        df = pd.read_sql(statement, s.bind)

        df.groupby("intersections")["curvature"].mean().plot()
        df.plot.scatter(x="intersections", y="curvature")
        plt.show()
Example #24
0
def random_integer():
    min_ = -1000000000
    max_ = 1000000000
    rand = randint(min_, max_)
    with session_scope() as session:
        while session.query(Ocena).filter(
                Ocena.ocena_id == rand).limit(1).first() is not None:
            rand = randint(min_, max_)
    return rand
Example #25
0
 def setupEditRecipe(self, tab):
     with session_scope(self.database) as session:
         substance_names = [name.name for name in
                            self.database.getSubstances(session)]
         unit_names = [name.name for name in
                       self.database.getUnits(session)]
         groups = self.database.getGroups(session)
         tab.setup(groups, substance_names, unit_names)
         tab.set_save_action(self.saveRecipe)
Example #26
0
def login(request, user):
    hash_obj = hashlib.sha256()
    hash_obj.update(SECRET_KEY.encode())
    hash_obj.update(str(request.get('time')).encode())
    token = hash_obj.hexdigest()
    with session_scope() as db_session:
        user_session = UserSession(user=user, token=token)
        db_session.add(user_session)
    return token
Example #27
0
def add_to_database(link_string, server, parent, to_be_visited):
    with session_scope() as session:
        # try:
        session.add(
            Websites(website_link=link_string, server=server,
                     parent_id=parent))
        to_be_visited.append(link_string)
        print(link_string)
        return to_be_visited
Example #28
0
def update_message_controller(request):
    data = request.get('data')
    message_id = data.get('message_id')
    message_data = data.get('message_data')
    with session_scope() as db_session:
        message = db_session.query(Message).filter_by(id=message_id).first()
        message.data = message_data

        response = make_response(request, 200)
        return response
Example #29
0
def plot_min_cycled(words):
    with session_scope() as s:
        for word in words:
            cycled_words = Word(word[1:]).get_cyles()

            statement = s.query(Result).filter(
                Result.word.in_(cycled_words), ).statement

            df = pd.read_sql(statement, s.bind)
            print(df)
Example #30
0
	def __get_korisnik_by_pk(korisnik_dao_call, primary_key):
		dao = None
		try:
			with session_scope() as session:
				dao = DAOManager.get_korisnik_dao(session)
				return korisnik_dao_call(dao, primary_key)
		except:
			return None
		finally:
			DAOManager.release(dao)
Example #31
0
	def get_uceniks_slusa(ucenik):
		dao = None
		try:
			with session_scope() as session:
				dao = DAOManager.get_korisnik_dao(session)
				return dao.get_uceniks_slusa(ucenik)
		except:
			return None
		finally:
			DAOManager.release(dao)
Example #32
0
	def get_slusa_by_pk(ucenik_id, predmet_id):
		dao = None
		try:
			with session_scope() as session:
				dao = DAOManager.get_korisnik_dao(session)
				slusa = dao.get_slusa_by_pk(ucenik_id, predmet_id)
				return slusa
		except:
			return None
		finally:
			DAOManager.release(dao)
Example #33
0
	def get_razred_by_godina(godina):
		dao = None
		try:
			with session_scope() as session:
				dao = DAOManager.get_razred_dao(session)
				return dao.get_razred_by_godina(godina)
		except:
			return None
		finally:
			if dao is not None:
				DAOManager.release(dao)
Example #34
0
	def get_profesors_predmets_slusa(predmet, profesor):
		dao = None
		try:
			with session_scope() as session:
				dao = DAOManager.get_korisnik_dao(session)
				slusas = dao.get_profesors_predmets_slusa(predmet, profesor)
				return slusas
		except:
			return None
		finally:
			DAOManager.release(dao)
Example #35
0
 def getRecipes(self, name=None, ingredients=None, groups=None):
     print name, ingredients
     with session_scope(self.database) as session:
         self.setupRecipes(self.database.
                           getRecipesByGroups(session,
                                              title=name,
                                              ingredients=ingredients))
     if name or ingredients:
         self.recipesList.ExpandAllChildren(self.recipesList.GetRootItem())
     else:
         self.recipesList.CollapseAllChildren(self.recipesList.GetRootItem())
Example #36
0
    def selectRecipe(self, event):
        event.Skip()
        item = event.GetItem()
        if item and not self.recipesList.ItemHasChildren(item):
            with session_scope(self.database) as session:
                recipe = session.query(Recipe)\
                    .get(self.recipesList.GetPyData(item))

                self.setStatusBarText(recipe)
                self.showRecipe(recipe, self.recipe_panel)
        else:
            self.recipesList.Toggle(item)
Example #37
0
 def popNextQueuedJob(self):
   if self.__len__() == 0:
     return None
   try:
     queued_job = self.__get_next_queued_job()
     queued_job_record = self.__get_job_record(queued_job.job_id)
     with session_scope() as db_session:
       db_session.delete(queued_job) # remove this job from the queue
       db_session.commit()
       return queued_job_record
   except:
     return None
Example #38
0
 def recipe_options(self, event):
     with session_scope(self.database) as session:
         recipe = session.query(Recipe).get(self.selected_recipe)
         if event.GetId() == 0:  # open in new tab
             self.open_recipe_tab(recipe)
         elif event.GetId() == 1:  # edit
             self.edit_recipe(recipe)
         elif event.GetId() == 2:  # new
             self.edit_recipe()
         elif event.GetId() == 3:  # delete
             session.delete(recipe)
             session.commit()
             self.selected_recipe = None
             self.setupRecipes(self.database.getRecipesByGroups(session))
         self.Layout()
Example #39
0
 def addJobRecord(self, function_id, function_inputs_dict):
   with session_scope() as db_session:
     jid = None
     try:
       new_job = JobRecord(function_id,json.dumps(function_inputs_dict))
       db_session.add(new_job)
       db_session.commit()
     except:
       jid= -1
     try:
       new_queued_job = QueuedJob(new_job.id)
       db_session.add(new_queued_job)
       db_session.commit()
       jid = new_job.id
     except:
       db_session.delete(new_job)
       db_session.commit()
       jid = -1
     return jid
Example #40
0
    def setup(self):
        self.tabs = {"recipes": 0,
                     "edit": 1}

        self.database = DBConfig('sqlite:///database.db')

        session = self.database.getSession()
        self.userId = session.query(User)\
            .filter(User.name.like('dan')).first().id

        session.close()

        self.tabsContainer.SetSelection(self.tabs["recipes"])
        isz = (16, 16)
        self.il = wx.ImageList(isz[0], isz[1])
        self.icons = {
            FOLDER_ICON: self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER,
                                                              wx.ART_OTHER, isz)),
            FOLDER_OPEN_ICON: self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_FILE_OPEN,
                                                                   wx.ART_OTHER,
                                                              isz)),
            FILE_ICON: self.il.Add(wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE,
                                                            wx.ART_OTHER, isz))}
        self.recipesList.SetImageList(self.il)
        self.getRecipes()
        with session_scope(self.database) as session:
            substance_names = [name.name for name in
                               self.database.getSubstances(session)]
            self.searchIngredients.SetChoices(substance_names)

        self.searchIngredients.SetItemsCallback(self.filterIngredients)

        self.setupEditRecipe(self.edit_recipe_tab)

        self.recipes_menu_options = [_("open in new tab"), _("edit"),
                                     _("new"), _("delete")]

        self.setStatusBarText(_("setup: OK"))
Example #41
0
def main():
    """ Prompt user & initialization
    """

    print("braindocs2database")
    print("Copyright (c) 2015 ai-one inc. All rights reserved.")
    print("")
    print(">>> Current settings:\n")

    cfg = PromptingConfigParser()

    # add default settings
    cfg.add_section("braindocs")
    cfg.add_section("database")
    cfg.set("database", "url", "sqlite:///braindocs_export.db")
    cfg.set("braindocs", "username", "bd_user")
    cfg.set("braindocs", "password", "bd_pw")
    cfg.set("braindocs", "url", "https://nathandev2.cloudapp.net/at")

    # read settings from file
    cfg.read("braindocs2database.config")

    # prompt update
    print(cfg)
    while promptyesno("\nUpdate settings?"):
        print("")
        cfg.prompt()
        with open("braindocs2database.config", 'wb') as fp:
            cfg.write(fp)
        print("\n>>> Updated settings:\n")
        print(cfg)
    
    """ Setup database connection
    """

    engine = create_engine(cfg.get("database", "url"), echo=False)

    #Base.metadata.drop_all(engine) # optional: drop all
    Base.metadata.create_all(engine)
 
    Session = sessionmaker(bind=engine)

    """ Setup braindocs connection
    """

    bd = BraindocsApi(
        username=cfg.get("braindocs", "username"),
        password=cfg.get("braindocs", "password"),
        baseURL=cfg.get("braindocs", "url"))

    """ Import braindocs entries into database
    """

    print("\nStarting import...")
    with session_scope(Session()) as session:
        for ar in iterprogress(bd.getAnalysisResults(), "importing analysis results", 1):
            arentry = AnalysisResult()
            for key, value in ar.iteritems():
                if isinstance(value, (list, dict)):
                    value = json.dumps(value, ensure_ascii=False)
                if key == "create_date":
                    value = dateutil.parser.parse(value).replace(tzinfo=None)
                setattr(arentry, key, value)
                if key == "analysisId":
                    for tu in bd.getAnalysisDetailsTextUnits(value):
                        tuentry = AnalysisResultTextUnit()
                        for key, value in tu.iteritems():
                            setattr(tuentry, key, value)
                        arentry.textunits.append(tuentry)
            session.merge(arentry)
Example #42
0
 def __len__(self):
   with session_scope() as db_session:
     all_jobs = db_session.query(QueuedJob).all()
     return len(all_jobs)
Example #43
0
 def removeJobRecord(self,job_id):
   job_record = self.__get_job_record(job_id)
   with session_scope() as db_session:
     db_session.delete(job_record)
     db_session.commit()
     return job_record
Example #44
0
 def __get_job_record(self,job_id):
   with session_scope() as db_session:
     return db_session.query(JobRecord).filter_by(id=job_id).one()
Example #45
0
 def __get_next_queued_job(self):
   with session_scope() as db_session:
     return db_session.query(QueuedJob).order_by(QueuedJob.id).first()