Coverage for model/profile.py: 100%
40 statements
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-05 04:22 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2024-11-05 04:22 +0000
1from flask import Blueprint
3from mongo import *
4from .auth import *
5from .utils import *
7__all__ = ['profile_api']
9profile_api = Blueprint('profile_api', __name__)
12@profile_api.route('/', methods=['GET'])
13@profile_api.route('/<username>', methods=['GET'])
14@login_required
15def view_profile(user, username=None):
16 user = user if username is None else User(username)
17 if not user:
18 return HTTPError('Profile not exist.', 404)
20 data = {
21 'email': user.obj.email,
22 'displayedName': user.obj.profile.displayed_name,
23 'bio': user.obj.profile.bio
24 }
25 data.update(user.info)
27 return HTTPResponse('Profile exist.', data=data)
30@profile_api.route('/', methods=['POST'])
31@login_required
32@Request.json('bio', vars_dict={'displayed_name': 'displayedName'})
33def edit_profile(user, displayed_name, bio):
34 profile = user.obj.profile or {}
36 if displayed_name is not None:
37 profile[
38 'displayed_name'] = displayed_name if displayed_name != "" else user.username
39 if bio is not None:
40 profile['bio'] = bio
42 user.obj.update(profile=profile)
44 cookies = {'jwt': user.cookie}
45 return HTTPResponse('Uploaded.', cookies=cookies)
48@profile_api.route('/config', methods=['PUT'])
49@login_required
50@Request.json('font_size', 'theme', 'indent_type', 'tab_size', 'language')
51def edit_config(user, font_size, theme, indent_type, tab_size, language):
52 try:
53 config = {
54 'font_size': font_size,
55 'theme': theme,
56 'indent_type': indent_type,
57 'tab_size': tab_size,
58 'language': language
59 }
60 user.obj.update(editor_config=config)
61 except ValidationError as ve:
62 return HTTPError('Update fail.', 400, data=ve.to_dict())
63 user.reload()
64 cookies = {'jwt': user.cookie}
65 return HTTPResponse('Uploaded.', cookies=cookies)