コード例 #1
0
def user_upgrade(user_id):
    try:
        User.objects(id=user_id).update_one(set__user_type='advanced')
        code = 200
    except Exception as e:
        code = 403
    return code
コード例 #2
0
def delete_lib(user_id, lib_id):
    """
    删除已有的分类列表
    """
    d_lib = Library.objects(id=lib_id).first()
    try:
        d_lib.delete()
        User.objects(id=user_id).update_one(dec__lib_amount=1)
        code = 200
    except Exception as e:
        print(str(e))
        code = 403
    return code
コード例 #3
0
def add_lib(user_id, lib_name):
    """
    添加新的分类列表
    """
    new_lib = Library(owner_id=user_id, lib_name=lib_name)
    try:
        new_lib.save()
        User.objects(id=user_id).update_one(inc__lib_amount=1)
        code = 200
        new_lib_id = new_lib.id
    except Exception as e:
        print(str(e))
        new_lib_id = ''
        code = 403
    return new_lib_id, code
コード例 #4
0
def LogHandler(uname, challenge):
    content = {}
    userquery = User.objects(username=uname).first()
    if userquery == None:
        # 无结果返回
        message = 'Oops! User not found...'
        status_code = 403
        content = None
    else:
        h_real = None
        password_hash = userquery.password_hash
        salt = salt_manager.matchSalt(uname)
        a = (password_hash.lower() + salt)
        if salt:
            h_real = md5(a.encode('utf8')).hexdigest()
            print(h_real)

        # challenge = h_real  # 应用时请注释!
        if challenge == h_real:
            # 认证成功
            if not salt_manager.delSalt(uname):
                print("Error: Salt Not Exists")
            status_code = 200
            message = 'Verification success! '
            content['userid'] = str(userquery.id)
        else:
            # 密码验证错误,认证失败
            status_code = 403
            message = 'Password verification failed! '
            content = None
    return content, status_code, message
コード例 #5
0
def get_lib(user_id):
    """
    获取当前用户的所有分类列表
    """
    try:
        lib_query = Library.objects(owner_id=user_id)
        lib_id_list = [str(query.id) for query in lib_query]
        lib_name_list = [query.lib_name for query in lib_query]
        doc_amount_list = [len(query.doc_list) for query in lib_query]
        all_doc = User.objects(id=user_id).first().doc_amount
        type_list = []
        for i in range(len(lib_id_list)):
            if not lib_name_list[i] == '待读列表':
                type_list.append(2)
            else:
                type_list.append(0)
        type_list.append(1)
        doc_amount_list.append(all_doc)
        lib_id_list.append('1')
        lib_name_list.append('全部文献')
        final = [{
            'lib_id': lib_id,
            'lib_name': lib_name,
            'doc_count': doc_count,
            'type': lib_type
        } for lib_id, lib_name, doc_count, lib_type in zip(
            lib_id_list, lib_name_list, doc_amount_list, type_list)]
        my_response = {'libs': final}
        code = 200
    except Exception as e:
        print(e)
        my_response = ''
        code = 403
    return my_response, code
コード例 #6
0
def RegisterHandler(username, password_hash):
    userQuery = User.objects(username=username).first()
    if userQuery == None:
        # 创建新的用户记录
        new_user = User(
            username=username,
            password_hash=password_hash,
            user_type='normal'
        )
        new_user.save()

        # 为新用户创建待读列表
        new_lib = Library(
            owner_id=new_user.id,
            lib_name='待读列表'
        )
        new_lib.save()

        return 'Register Success', 200
    else:
        msg = 'Username exists!'
        return msg, 403
コード例 #7
0
def PreLoginHandler(userInfo):
    data = {}
    if 'username' not in userInfo:
        status_code = 404
    else:
        uname = userInfo['username']
        unameQuery = User.objects(username=uname)
        if len(unameQuery):
            data['id'] = salt_manager.getNewSalt(uname)
            status_code = 200
        else:
            status_code = 403

    return data, status_code
コード例 #8
0
def get_metadata(user_id, stream, name):
    identifier = get_identifier(stream)
    if identifier:
        (key, value), = identifier.items()
        rurl = "http://api.semanticscholar.org/v1/paper/"
        final = "?include_unknown_references=TRUE"
        if key == 'None':
            try:
                user = User.objects(id=user_id).first()
                user_type = user.user_type
                user_doc_amount = user.doc_amount
                user_doc_amount += 1
                if not user_type == 'advanced':
                    if user_doc_amount > 10:
                        return 'Full'
                user.update(doc_amount=user_doc_amount)
                new_document = Documents(owner_id=user_id,
                                         save_name=name,
                                         color=0,
                                         save_note=0)
                new_metadata = Metadata(title=str(name),
                                        user_score=0,
                                        paper_id='-',
                                        publish_date='-',
                                        publish_source='-')
                new_document.metadata = new_metadata
                new_document.save()
                return new_document.id
            except Exception as e:
                print(str(e))
        url = ''
        if key == 'doi':
            url = 'https://dx.doi.org/' + value
            rurl = rurl + value + final
        elif key == 'arXiv':
            url = 'https://arxiv.org/abs/' + value
            rurl = rurl + "arXiv:" + value + final
        response = requests.get(url=rurl)
        json_data = json.loads(response.text)
        if 'error' in dict(json_data):
            try:
                user = User.objects(id=user_id).first()
                user_type = user.user_type
                user_doc_amount = user.doc_amount
                user_doc_amount += 1
                if not user_type == 'advanced':
                    if user_doc_amount > 10:
                        return 'Full'
                user.update(doc_amount=user_doc_amount)
                new_document = Documents(owner_id=user_id,
                                         save_name=name,
                                         color=0,
                                         save_note=0)
                new_metadata = Metadata(title=str(name),
                                        user_score=0,
                                        paper_id='-',
                                        author=['-'],
                                        publish_date='-',
                                        publish_source='-',
                                        link_url='-')
                new_document.metadata = new_metadata
                new_document.save()
                return new_document.id
            except Exception as e:
                print(str(e))
        title = json_data['title']
        try:
            source = json_data['venue']
        except Exception as e:
            print(str(e))
            source = '-'
        paper_id = key + ':' + value
        author = []
        for i in list(json_data['authors']):
            author.append(i['name'])
        try:
            publish_date = json_data['year']
        except Exception as e:
            print(str(e))
            publish_date = '-'
        topic = []
        topic_id = []
        for i in list(json_data['topics']):
            topic.append(i['topic'])
        topic.append("test_topic")
        # user_info
        user = User.objects(id=user_id).first()
        user_type = user.user_type
        user_doc_amount = user.doc_amount
        user_doc_amount += 1
        if not user_type == 'advanced':
            if user_doc_amount > 10:
                return 'Full'
        user.update(doc_amount=user_doc_amount)
        for one_topic in topic:
            try:
                Topic(topic_name=one_topic).save()
            except mongoengine.errors.NotUniqueError:
                continue
        for one_topic in topic:
            topic_id.append(str(Topic.objects.get(topic_name=one_topic).id))
        # document
        new_metadata = Metadata(title=title,
                                paper_id=paper_id,
                                author=author,
                                publish_date=str(publish_date),
                                publish_source=source,
                                link_url=url,
                                user_score=0)
        new_document = Documents(owner_id=user_id,
                                 metadata=new_metadata,
                                 color=0,
                                 topic=topic_id,
                                 save_name=name,
                                 save_note=0)
        new_document.save()
        new_document_id = new_document.id
        # 回到topic插入
        for tid in topic_id:
            Topic.objects(id=tid).update_one(push__doc_list=new_document_id)
        return new_document_id
    else:
        return 'Error'