Example #1
0
    def jump(args):
        try:
            search_history_id = args.get('search_history_id')
            paper_id = args.get('paper_id')
            search_history = SearchHistory.objects(id=search_history_id).get()
            search_item = search_history.item
            paper = Paper.objects(id=paper_id).get()
            click_history = ClickHistory(
                search_item=search_item,
                search_history=search_history,
                paper=paper,
                user=User.objects(id=flask_login.current_user.id).get() if flask_login.current_user.is_authenticated else None
            )
            click_history.save()
            if ClickCount.objects(search_item=search_item, paper=paper).count() > 0:
                click_count = ClickCount.objects(search_item=search_item, paper=paper).get()

            else:
                click_count = ClickCount(
                    search_item=search_item, paper=paper
                )
            click_count.count = click_count.count + 1
            click_count.save()
            return redirect(paper.url)
        except Exception as e:
            logging.warning(e)
            abort(401)
Example #2
0
 def delete(id):
     try:
         user = User.objects(id=current_user.id).get()
         item = SubscriptionItem.objects(id=id).get()
         user.update(pull__subscriptions=item)
         return jsonify(status='ok')
     except Exception as e:
         logging.warning(e)
         return jsonify(status='error')
Example #3
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        email = request.form['email']
        if not username or not password or not email:
            return 'Input not valid.'
        try:
            user = User.objects(email=email).get()
        except Exception:
            user = User(username=username, password=bcrypt.generate_password_hash(password), email=email)
            user.save()
            flask_login.login_user(user)
            return 'Success.'
        else:
            return 'Already exists.'
    else:
        return current_app.send_static_file('register.html')
Example #4
0
 def add(args):
     try:
         keyword = args.get('keyword')
         user = User.objects(id=current_user.id).get()
         if SubscriptionItem.objects(keyword=keyword).count() == 0:
             item = SubscriptionItem(keyword=keyword)
             item.save()
             Subscription.update(item)
         else:
             item = SubscriptionItem.objects(keyword=keyword).first()
         user.update(push__subscriptions=item)
         return jsonify(status='ok', item=item.serialize())
     except Exception as e:
         logging.warning(e)
         return jsonify(status='error')
Example #5
0
    def search(args):
        keyword = args.get('keyword')

        # Store history in database
        if SearchItem.objects(keyword=keyword).count() == 0:
            search_item = SearchItem(keyword=keyword)
        else:
            search_item = SearchItem.objects(keyword=keyword).get()
        search_item.count += 1
        search_item.save()

        # Load cache
        # search_results = g.get('search_results', None)
        # if search_results is None:
        #     g.search_results = {}
        #     search_results = g.search_results
        #
        # search_id_to_results = g.get('search_id_to_results', None)
        # if search_id_to_results is None:
        #     g.search_id_to_results = {}
        #     search_id_to_results = g.search_id_to_results

        query_result = PaperProcessor(keyword)
        papers = query_result.papers_array

        # paper_ids = [x["DBID"] for x in papers]
        # search_item.update(add_to_set__papers=paper_ids)

        if flask_login.current_user.is_authenticated:
            search_history = SearchHistory(item=search_item,
                                           user=User.objects(id=flask_login.current_user.id).get(),
                                           papers=[x for x in query_result.papers_array])
        else:
            search_history = SearchHistory(item=search_item,
                                           papers=[x for x in query_result.papers_array])
        search_history.save()

        # # Word bag
        # bag = AbstractProcessor().process_list(return_list)
        # words = [[y, bag[y]] for y in sorted(list(bag.keys()), key=lambda x: bag[x], reverse=True)[:30]]

        # Return result
        return jsonify(
            response=str(search_history.id),
            meta_info={
                'page_count': math.ceil(len(papers)/RESULTS_PER_PAGE)
            }
        )
Example #6
0
def login():
    if request.method == 'POST':
        email = request.form['email']
        logging.debug("{} tries to login.".format(email))
        try:
            user = User.objects(email=email).get()
            if bcrypt.check_password_hash(user.password, request.form['password']):
                flask_login.login_user(user)
                logging.debug('Logged in successfully.')
                flash('Logged in successfully.')
                return redirect(url_for('index'))
            else:
                logging.debug('Wrong password.')
                flash('Wrong password.')
                return redirect(url_for('login'))
        except Exception as e:
            logging.warning(e)
            return "User not exist."
    else:
        return current_app.send_static_file('login.html')
Example #7
0
 def index():
     user = User.objects(id=current_user.id).get()
     return user.subscriptions
Example #8
0
 def get_timeline():
     user = User.objects(id=current_user.id).get()
     papers = Paper.objects(subscriptions__in=user.subscriptions).order_by('-date')
     return papers
Example #9
0
def user_loader(id):
    logging.debug("User loader:{}".format(id));
    try:
        return User.objects(id=id).get()
    except:
        return None