|
| 1 | + |
| 2 | +# Copyright IBM Corp, All Rights Reserved. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +from flask_restful import Resource, fields, marshal_with, reqparse |
| 7 | +from flask_login import login_required |
| 8 | +import logging |
| 9 | +import sys |
| 10 | +import os |
| 11 | + |
| 12 | +sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..')) |
| 13 | +from common import log_handler, LOG_LEVEL, StringValidator |
| 14 | +from modules.user.user import User |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | +logger.setLevel(LOG_LEVEL) |
| 18 | +logger.addHandler(log_handler) |
| 19 | + |
| 20 | + |
| 21 | +class ValidationError(Exception): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +def email(email_str): |
| 26 | + """ |
| 27 | + Email Field to validate input email string |
| 28 | + :param email_str: email address |
| 29 | + :return: email address string or raise exception |
| 30 | + """ |
| 31 | + validator = StringValidator() |
| 32 | + if validator.validate(email_str, ["is_email"]): |
| 33 | + return email_str |
| 34 | + else: |
| 35 | + raise ValidationError("{} not a valid email".format(email_str)) |
| 36 | + |
| 37 | + |
| 38 | +user_profile_fields = { |
| 39 | + "username": fields.String, |
| 40 | + "name": fields.String, |
| 41 | + "email": fields.String, |
| 42 | + "bio": fields.String, |
| 43 | + "url": fields.String, |
| 44 | + "location": fields.String |
| 45 | +} |
| 46 | + |
| 47 | +profile_response_fields = { |
| 48 | + "result": fields.Nested(user_profile_fields), |
| 49 | + "success": fields.Boolean, |
| 50 | + "error": fields.String |
| 51 | +} |
| 52 | + |
| 53 | +update_response_fields = { |
| 54 | + "success": fields.Boolean, |
| 55 | + "error": fields.String |
| 56 | +} |
| 57 | + |
| 58 | +update_profile_parser = reqparse.RequestParser() |
| 59 | +update_profile_parser.add_argument('name', |
| 60 | + location='form', |
| 61 | + help='name for update') |
| 62 | +update_profile_parser.add_argument('email', |
| 63 | + type=email, |
| 64 | + location='form', |
| 65 | + help='email for update') |
| 66 | +update_profile_parser.add_argument('bio', |
| 67 | + location='form', |
| 68 | + help='bio for update') |
| 69 | +update_profile_parser.add_argument('url', |
| 70 | + location='form', |
| 71 | + help='url for update') |
| 72 | +update_profile_parser.add_argument('location', |
| 73 | + location='form', |
| 74 | + help='location for update') |
| 75 | + |
| 76 | + |
| 77 | +class UserProfile(Resource): |
| 78 | + """ |
| 79 | + User Profile class, supply get/put method |
| 80 | + """ |
| 81 | + @marshal_with(profile_response_fields) |
| 82 | + def get(self, user_id): |
| 83 | + """ |
| 84 | + Get user profile information |
| 85 | + :param user_id: user id of User to query |
| 86 | + :return: profile data, status code |
| 87 | + """ |
| 88 | + user_obj = User() |
| 89 | + user = user_obj.get_by_id(user_id) |
| 90 | + if not user: |
| 91 | + return {"error": "No such User", "success": False}, 400 |
| 92 | + |
| 93 | + data = { |
| 94 | + "result": { |
| 95 | + "username": user.username, |
| 96 | + "name": user.profile.name if user.profile else "", |
| 97 | + "email": user.profile.email if user.profile else "", |
| 98 | + "bio": user.profile.bio if user.profile else "", |
| 99 | + "url": user.profile.url if user.profile else "", |
| 100 | + "location": user.profile.location if user.profile else "", |
| 101 | + }, |
| 102 | + "success": True |
| 103 | + } |
| 104 | + |
| 105 | + return data, 200 |
| 106 | + |
| 107 | + @marshal_with(update_response_fields) |
| 108 | + def put(self, user_id): |
| 109 | + """ |
| 110 | + Update user profile |
| 111 | + :param user_id: user id of User to update profile |
| 112 | + :return: api response, status code |
| 113 | + """ |
| 114 | + args = update_profile_parser.parse_args() |
| 115 | + name, email_addr = args["name"], args["email"] |
| 116 | + bio, url = args["bio"], args["url"] |
| 117 | + location = args["location"] |
| 118 | + user_obj = User() |
| 119 | + user = user_obj.get_by_id(user_id) |
| 120 | + if not user: |
| 121 | + return {"error": "No such User", "success": False}, 400 |
| 122 | + else: |
| 123 | + user.update_profile(name=name, email=email_addr, |
| 124 | + bio=bio, url=url, location=location) |
| 125 | + return {"success": True}, 200 |
0 commit comments