We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
For the edit action button in admin/users the super admin should be allowed to make the users as an admin.
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->
The patch for the users is working but is_admin is a readonly property even for the super user. Used the patch request for users and changed the value for is_admin from true to false. But there is no change after the request to the value of is_admin. 

**To Reproduce**
Steps to reproduce the behavior:
1. Go to v1/users/user_id
2. Send a patch request
3. Change the value of is_admin for some user from false to true.
4. See error

![image](https://user-images.githubusercontent.com/22127980/42907807-8f6542fc-8afc-11e8-84cd-0fe415042fb2.png)

After the request
![image](https://user-images.githubusercontent.com/22127980/42907825-9ecafdd6-8afc-11e8-8372-5cc65888b08b.png)





--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `app/api/users.py`
Content:
```
1 import base64
2 
3 from flask import Blueprint, request, jsonify, abort, make_response
4 from flask_jwt import current_identity as current_user
5 from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
6 
7 from app import get_settings
8 from app.api.bootstrap import api
9 from app.api.helpers.db import safe_query, get_count
10 from app.api.helpers.exceptions import ConflictException
11 from app.api.helpers.exceptions import ForbiddenException
12 from app.api.helpers.files import create_save_image_sizes, make_frontend_url
13 from app.api.helpers.mail import send_email_confirmation, send_email_change_user_email, send_email_with_action
14 from app.api.helpers.permission_manager import has_access
15 from app.api.helpers.permissions import is_user_itself
16 from app.api.helpers.utilities import get_serializer, str_generator
17 from app.api.schema.users import UserSchema, UserSchemaPublic
18 from app.models import db
19 from app.models.access_code import AccessCode
20 from app.models.discount_code import DiscountCode
21 from app.models.email_notification import EmailNotification
22 from app.models.event_invoice import EventInvoice
23 from app.models.feedback import Feedback
24 from app.models.mail import USER_REGISTER_WITH_PASSWORD
25 from app.models.notification import Notification
26 from app.models.session import Session
27 from app.models.speaker import Speaker
28 from app.models.ticket_holder import TicketHolder
29 from app.models.user import User
30 from app.models.users_events_role import UsersEventsRoles
31 
32 user_misc_routes = Blueprint('user_misc', __name__, url_prefix='/v1')
33 
34 
35 class UserList(ResourceList):
36     """
37     List and create Users
38     """
39     def before_create_object(self, data, view_kwargs):
40         """
41         method to check if there is an existing user with same email which is received in data to create a new user
42         :param data:
43         :param view_kwargs:
44         :return:
45         """
46         if db.session.query(User.id).filter_by(email=data['email'], deleted_at=None).scalar() is not None:
47             raise ConflictException({'pointer': '/data/attributes/email'}, "Email already exists")
48 
49     def after_create_object(self, user, data, view_kwargs):
50         """
51         method to send-
52         email notification
53         mail link for register verification
54         add image urls
55         :param user:
56         :param data:
57         :param view_kwargs:
58         :return:
59         """
60         s = get_serializer()
61         hash = str(base64.b64encode(str(s.dumps([user.email, str_generator()])).encode()), 'utf-8')
62         link = make_frontend_url('/email/verify'.format(id=user.id), {'token': hash})
63         send_email_with_action(user, USER_REGISTER_WITH_PASSWORD, app_name=get_settings()['app_name'],
64                                email=user.email)
65         send_email_confirmation(user.email, link)
66 
67         if data.get('original_image_url'):
68             uploaded_images = create_save_image_sizes(data['original_image_url'], 'user', user.id)
69             uploaded_images['small_image_url'] = uploaded_images['thumbnail_image_url']
70             del uploaded_images['large_image_url']
71             self.session.query(User).filter_by(id=user.id).update(uploaded_images)
72 
73     decorators = (api.has_permission('is_admin', methods="GET"),)
74     schema = UserSchema
75     data_layer = {'session': db.session,
76                   'model': User,
77                   'methods': {
78                       'before_create_object': before_create_object,
79                       'after_create_object': after_create_object
80                   }}
81 
82 
83 class UserDetail(ResourceDetail):
84     """
85     User detail by id
86     """
87     def before_get(self, args, kwargs):
88 
89         if current_user.is_admin or current_user.is_super_admin or current_user:
90             self.schema = UserSchema
91         else:
92             self.schema = UserSchemaPublic
93 
94     def before_get_object(self, view_kwargs):
95         """
96         before get method for user object
97         :param view_kwargs:
98         :return:
99         """
100         if view_kwargs.get('notification_id') is not None:
101             notification = safe_query(self, Notification, 'id', view_kwargs['notification_id'], 'notification_id')
102             if notification.user_id is not None:
103                 view_kwargs['id'] = notification.user_id
104             else:
105                 view_kwargs['id'] = None
106 
107         if view_kwargs.get('feedback_id') is not None:
108             print(view_kwargs['feedback_id'])
109             feedback = safe_query(self, Feedback, 'id', view_kwargs['feedback_id'], 'feedback_id')
110             if feedback.user_id is not None:
111                 view_kwargs['id'] = feedback.user_id
112             else:
113                 view_kwargs['id'] = None
114 
115         if view_kwargs.get('attendee_id') is not None:
116             attendee = safe_query(self, TicketHolder, 'id', view_kwargs['attendee_id'], 'attendee_id')
117             if attendee.user is not None:
118                 if (not has_access('is_user_itself',
119                                    user_id=attendee.user.id) or not has_access('is_coorganizer',
120                                                                                event_id=attendee.event_id)):
121                     raise ForbiddenException({'source': ''}, 'Access Forbidden')
122                 view_kwargs['id'] = attendee.user.id
123             else:
124                 view_kwargs['id'] = None
125 
126         if view_kwargs.get('event_invoice_id') is not None:
127             event_invoice = safe_query(self, EventInvoice, 'id', view_kwargs['event_invoice_id'], 'event_invoice_id')
128             if event_invoice.user_id is not None:
129                 view_kwargs['id'] = event_invoice.user_id
130             else:
131                 view_kwargs['id'] = None
132 
133         if view_kwargs.get('users_events_role_id') is not None:
134             users_events_role = safe_query(self, UsersEventsRoles, 'id', view_kwargs['users_events_role_id'],
135                                            'users_events_role_id')
136             if users_events_role.user_id is not None:
137                 view_kwargs['id'] = users_events_role.user_id
138 
139         if view_kwargs.get('speaker_id') is not None:
140             speaker = safe_query(self, Speaker, 'id', view_kwargs['speaker_id'], 'speaker_id')
141             if speaker.user_id is not None:
142                 view_kwargs['id'] = speaker.user_id
143             else:
144                 view_kwargs['id'] = None
145 
146         if view_kwargs.get('session_id') is not None:
147             session = safe_query(self, Session, 'id', view_kwargs['session_id'], 'session_id')
148             if session.creator_id is not None:
149                 view_kwargs['id'] = session.creator_id
150             else:
151                 view_kwargs['id'] = None
152 
153         if view_kwargs.get('access_code_id') is not None:
154             access_code = safe_query(self, AccessCode, 'id', view_kwargs['access_code_id'], 'access_code_id')
155             if access_code.marketer_id is not None:
156                 view_kwargs['id'] = access_code.marketer_id
157             else:
158                 view_kwargs['id'] = None
159 
160         if view_kwargs.get('discount_code_id') is not None:
161             discount_code = safe_query(self, DiscountCode, 'id', view_kwargs['discount_code_id'], 'discount_code_id')
162             if discount_code.marketer_id is not None:
163                 view_kwargs['id'] = discount_code.marketer_id
164             else:
165                 view_kwargs['id'] = None
166 
167         if view_kwargs.get('email_notification_id') is not None:
168             email_notification = safe_query(self, EmailNotification, 'id', view_kwargs['email_notification_id'],
169                                             'email_notification_id')
170             if email_notification.user_id is not None:
171                 view_kwargs['id'] = email_notification.user_id
172             else:
173                 view_kwargs['id'] = None
174 
175     def before_update_object(self, user, data, view_kwargs):
176         if data.get('original_image_url') and data['original_image_url'] != user.original_image_url:
177             uploaded_images = create_save_image_sizes(data['original_image_url'], 'user', user.id)
178             data['original_image_url'] = uploaded_images['original_image_url']
179             data['small_image_url'] = uploaded_images['thumbnail_image_url']
180             data['thumbnail_image_url'] = uploaded_images['thumbnail_image_url']
181             data['icon_image_url'] = uploaded_images['icon_image_url']
182 
183         if data.get('email') and data['email'] != user.email:
184             view_kwargs['email_changed'] = user.email
185 
186         if data.get('is_admin') != user.is_admin:
187             user.is_admin = not user.is_admin
188 
189     def after_update_object(self, user, data, view_kwargs):
190         """
191         method to mail user about email change
192         :param user:
193         :param data:
194         :param view_kwargs:
195         :return:
196         """
197         if view_kwargs.get('email_changed'):
198             send_email_change_user_email(user, view_kwargs.get('email_changed'))
199 
200     decorators = (api.has_permission('is_user_itself', fetch="user_id,id", fetch_as="user_id",
201                   model=[Notification, Feedback, UsersEventsRoles, Session, EventInvoice, AccessCode,
202                          DiscountCode, EmailNotification, Speaker, User],
203                   fetch_key_url="notification_id, feedback_id, users_events_role_id, session_id, \
204                   event_invoice_id, access_code_id, discount_code_id, email_notification_id, speaker_id, id",
205                                      leave_if=lambda a: a.get('attendee_id')), )
206     schema = UserSchema
207     data_layer = {'session': db.session,
208                   'model': User,
209                   'methods': {
210                       'before_get_object': before_get_object,
211                       'before_update_object': before_update_object,
212                       'after_update_object': after_update_object
213                   }}
214 
215 
216 class UserRelationship(ResourceRelationship):
217     """
218     User Relationship
219     """
220     decorators = (is_user_itself, )
221     schema = UserSchema
222     data_layer = {'session': db.session,
223                   'model': User}
224 
225 
226 @user_misc_routes.route('/users/checkEmail', methods=['POST'])
227 def is_email_available():
228     email = request.json.get('email', None)
229     if email:
230         if get_count(db.session.query(User).filter_by(email=email)):
231             return jsonify(
232                 result="False"
233             )
234         else:
235             return jsonify(
236                 result="True"
237             )
238     else:
239         abort(
240             make_response(jsonify(error="Email field missing"), 422)
241         )
242
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/app/api/users.py b/app/api/users.py
--- a/app/api/users.py
+++ b/app/api/users.py
@@ -183,7 +183,7 @@
         if data.get('email') and data['email'] != user.email:
             view_kwargs['email_changed'] = user.email
 
-        if data.get('is_admin') != user.is_admin:
+        if has_access('is_super_admin') and data.get('is_admin') != user.is_admin:
             user.is_admin = not user.is_admin
 
     def after_update_object(self, user, data, view_kwargs):

{"golden_diff": "diff --git a/app/api/users.py b/app/api/users.py\n--- a/app/api/users.py\n+++ b/app/api/users.py\n@@ -183,7 +183,7 @@\n         if data.get('email') and data['email'] != user.email:\n             view_kwargs['email_changed'] = user.email\n \n-        if data.get('is_admin') != user.is_admin:\n+        if has_access('is_super_admin') and data.get('is_admin') != user.is_admin:\n             user.is_admin = not user.is_admin\n \n     def after_update_object(self, user, data, view_kwargs):\n", "issue": "For the edit action button in admin/users the super admin should be allowed to make the users as an admin.\n**Describe the bug**\r\n<!-- A clear and concise description of what the bug is. -->\r\nThe patch for the users is working but is_admin is a readonly property even for the super user. Used the patch request for users and changed the value for is_admin from true to false. But there is no change after the request to the value of is_admin. \r\n\r\n**To Reproduce**\r\nSteps to reproduce the behavior:\r\n1. Go to v1/users/user_id\r\n2. Send a patch request\r\n3. Change the value of is_admin for some user from false to true.\r\n4. See error\r\n\r\n![image](https://user-images.githubusercontent.com/22127980/42907807-8f6542fc-8afc-11e8-84cd-0fe415042fb2.png)\r\n\r\nAfter the request\r\n![image](https://user-images.githubusercontent.com/22127980/42907825-9ecafdd6-8afc-11e8-8372-5cc65888b08b.png)\r\n\r\n\r\n\r\n\n", "before_files": [{"content": "import base64\n\nfrom flask import Blueprint, request, jsonify, abort, make_response\nfrom flask_jwt import current_identity as current_user\nfrom flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship\n\nfrom app import get_settings\nfrom app.api.bootstrap import api\nfrom app.api.helpers.db import safe_query, get_count\nfrom app.api.helpers.exceptions import ConflictException\nfrom app.api.helpers.exceptions import ForbiddenException\nfrom app.api.helpers.files import create_save_image_sizes, make_frontend_url\nfrom app.api.helpers.mail import send_email_confirmation, send_email_change_user_email, send_email_with_action\nfrom app.api.helpers.permission_manager import has_access\nfrom app.api.helpers.permissions import is_user_itself\nfrom app.api.helpers.utilities import get_serializer, str_generator\nfrom app.api.schema.users import UserSchema, UserSchemaPublic\nfrom app.models import db\nfrom app.models.access_code import AccessCode\nfrom app.models.discount_code import DiscountCode\nfrom app.models.email_notification import EmailNotification\nfrom app.models.event_invoice import EventInvoice\nfrom app.models.feedback import Feedback\nfrom app.models.mail import USER_REGISTER_WITH_PASSWORD\nfrom app.models.notification import Notification\nfrom app.models.session import Session\nfrom app.models.speaker import Speaker\nfrom app.models.ticket_holder import TicketHolder\nfrom app.models.user import User\nfrom app.models.users_events_role import UsersEventsRoles\n\nuser_misc_routes = Blueprint('user_misc', __name__, url_prefix='/v1')\n\n\nclass UserList(ResourceList):\n    \"\"\"\n    List and create Users\n    \"\"\"\n    def before_create_object(self, data, view_kwargs):\n        \"\"\"\n        method to check if there is an existing user with same email which is received in data to create a new user\n        :param data:\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if db.session.query(User.id).filter_by(email=data['email'], deleted_at=None).scalar() is not None:\n            raise ConflictException({'pointer': '/data/attributes/email'}, \"Email already exists\")\n\n    def after_create_object(self, user, data, view_kwargs):\n        \"\"\"\n        method to send-\n        email notification\n        mail link for register verification\n        add image urls\n        :param user:\n        :param data:\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        s = get_serializer()\n        hash = str(base64.b64encode(str(s.dumps([user.email, str_generator()])).encode()), 'utf-8')\n        link = make_frontend_url('/email/verify'.format(id=user.id), {'token': hash})\n        send_email_with_action(user, USER_REGISTER_WITH_PASSWORD, app_name=get_settings()['app_name'],\n                               email=user.email)\n        send_email_confirmation(user.email, link)\n\n        if data.get('original_image_url'):\n            uploaded_images = create_save_image_sizes(data['original_image_url'], 'user', user.id)\n            uploaded_images['small_image_url'] = uploaded_images['thumbnail_image_url']\n            del uploaded_images['large_image_url']\n            self.session.query(User).filter_by(id=user.id).update(uploaded_images)\n\n    decorators = (api.has_permission('is_admin', methods=\"GET\"),)\n    schema = UserSchema\n    data_layer = {'session': db.session,\n                  'model': User,\n                  'methods': {\n                      'before_create_object': before_create_object,\n                      'after_create_object': after_create_object\n                  }}\n\n\nclass UserDetail(ResourceDetail):\n    \"\"\"\n    User detail by id\n    \"\"\"\n    def before_get(self, args, kwargs):\n\n        if current_user.is_admin or current_user.is_super_admin or current_user:\n            self.schema = UserSchema\n        else:\n            self.schema = UserSchemaPublic\n\n    def before_get_object(self, view_kwargs):\n        \"\"\"\n        before get method for user object\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if view_kwargs.get('notification_id') is not None:\n            notification = safe_query(self, Notification, 'id', view_kwargs['notification_id'], 'notification_id')\n            if notification.user_id is not None:\n                view_kwargs['id'] = notification.user_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('feedback_id') is not None:\n            print(view_kwargs['feedback_id'])\n            feedback = safe_query(self, Feedback, 'id', view_kwargs['feedback_id'], 'feedback_id')\n            if feedback.user_id is not None:\n                view_kwargs['id'] = feedback.user_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('attendee_id') is not None:\n            attendee = safe_query(self, TicketHolder, 'id', view_kwargs['attendee_id'], 'attendee_id')\n            if attendee.user is not None:\n                if (not has_access('is_user_itself',\n                                   user_id=attendee.user.id) or not has_access('is_coorganizer',\n                                                                               event_id=attendee.event_id)):\n                    raise ForbiddenException({'source': ''}, 'Access Forbidden')\n                view_kwargs['id'] = attendee.user.id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('event_invoice_id') is not None:\n            event_invoice = safe_query(self, EventInvoice, 'id', view_kwargs['event_invoice_id'], 'event_invoice_id')\n            if event_invoice.user_id is not None:\n                view_kwargs['id'] = event_invoice.user_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('users_events_role_id') is not None:\n            users_events_role = safe_query(self, UsersEventsRoles, 'id', view_kwargs['users_events_role_id'],\n                                           'users_events_role_id')\n            if users_events_role.user_id is not None:\n                view_kwargs['id'] = users_events_role.user_id\n\n        if view_kwargs.get('speaker_id') is not None:\n            speaker = safe_query(self, Speaker, 'id', view_kwargs['speaker_id'], 'speaker_id')\n            if speaker.user_id is not None:\n                view_kwargs['id'] = speaker.user_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('session_id') is not None:\n            session = safe_query(self, Session, 'id', view_kwargs['session_id'], 'session_id')\n            if session.creator_id is not None:\n                view_kwargs['id'] = session.creator_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('access_code_id') is not None:\n            access_code = safe_query(self, AccessCode, 'id', view_kwargs['access_code_id'], 'access_code_id')\n            if access_code.marketer_id is not None:\n                view_kwargs['id'] = access_code.marketer_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('discount_code_id') is not None:\n            discount_code = safe_query(self, DiscountCode, 'id', view_kwargs['discount_code_id'], 'discount_code_id')\n            if discount_code.marketer_id is not None:\n                view_kwargs['id'] = discount_code.marketer_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('email_notification_id') is not None:\n            email_notification = safe_query(self, EmailNotification, 'id', view_kwargs['email_notification_id'],\n                                            'email_notification_id')\n            if email_notification.user_id is not None:\n                view_kwargs['id'] = email_notification.user_id\n            else:\n                view_kwargs['id'] = None\n\n    def before_update_object(self, user, data, view_kwargs):\n        if data.get('original_image_url') and data['original_image_url'] != user.original_image_url:\n            uploaded_images = create_save_image_sizes(data['original_image_url'], 'user', user.id)\n            data['original_image_url'] = uploaded_images['original_image_url']\n            data['small_image_url'] = uploaded_images['thumbnail_image_url']\n            data['thumbnail_image_url'] = uploaded_images['thumbnail_image_url']\n            data['icon_image_url'] = uploaded_images['icon_image_url']\n\n        if data.get('email') and data['email'] != user.email:\n            view_kwargs['email_changed'] = user.email\n\n        if data.get('is_admin') != user.is_admin:\n            user.is_admin = not user.is_admin\n\n    def after_update_object(self, user, data, view_kwargs):\n        \"\"\"\n        method to mail user about email change\n        :param user:\n        :param data:\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if view_kwargs.get('email_changed'):\n            send_email_change_user_email(user, view_kwargs.get('email_changed'))\n\n    decorators = (api.has_permission('is_user_itself', fetch=\"user_id,id\", fetch_as=\"user_id\",\n                  model=[Notification, Feedback, UsersEventsRoles, Session, EventInvoice, AccessCode,\n                         DiscountCode, EmailNotification, Speaker, User],\n                  fetch_key_url=\"notification_id, feedback_id, users_events_role_id, session_id, \\\n                  event_invoice_id, access_code_id, discount_code_id, email_notification_id, speaker_id, id\",\n                                     leave_if=lambda a: a.get('attendee_id')), )\n    schema = UserSchema\n    data_layer = {'session': db.session,\n                  'model': User,\n                  'methods': {\n                      'before_get_object': before_get_object,\n                      'before_update_object': before_update_object,\n                      'after_update_object': after_update_object\n                  }}\n\n\nclass UserRelationship(ResourceRelationship):\n    \"\"\"\n    User Relationship\n    \"\"\"\n    decorators = (is_user_itself, )\n    schema = UserSchema\n    data_layer = {'session': db.session,\n                  'model': User}\n\n\n@user_misc_routes.route('/users/checkEmail', methods=['POST'])\ndef is_email_available():\n    email = request.json.get('email', None)\n    if email:\n        if get_count(db.session.query(User).filter_by(email=email)):\n            return jsonify(\n                result=\"False\"\n            )\n        else:\n            return jsonify(\n                result=\"True\"\n            )\n    else:\n        abort(\n            make_response(jsonify(error=\"Email field missing\"), 422)\n        )\n", "path": "app/api/users.py"}], "after_files": [{"content": "import base64\n\nfrom flask import Blueprint, request, jsonify, abort, make_response\nfrom flask_jwt import current_identity as current_user\nfrom flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship\n\nfrom app import get_settings\nfrom app.api.bootstrap import api\nfrom app.api.helpers.db import safe_query, get_count\nfrom app.api.helpers.exceptions import ConflictException\nfrom app.api.helpers.exceptions import ForbiddenException\nfrom app.api.helpers.files import create_save_image_sizes, make_frontend_url\nfrom app.api.helpers.mail import send_email_confirmation, send_email_change_user_email, send_email_with_action\nfrom app.api.helpers.permission_manager import has_access\nfrom app.api.helpers.permissions import is_user_itself\nfrom app.api.helpers.utilities import get_serializer, str_generator\nfrom app.api.schema.users import UserSchema, UserSchemaPublic\nfrom app.models import db\nfrom app.models.access_code import AccessCode\nfrom app.models.discount_code import DiscountCode\nfrom app.models.email_notification import EmailNotification\nfrom app.models.event_invoice import EventInvoice\nfrom app.models.feedback import Feedback\nfrom app.models.mail import USER_REGISTER_WITH_PASSWORD\nfrom app.models.notification import Notification\nfrom app.models.session import Session\nfrom app.models.speaker import Speaker\nfrom app.models.ticket_holder import TicketHolder\nfrom app.models.user import User\nfrom app.models.users_events_role import UsersEventsRoles\n\nuser_misc_routes = Blueprint('user_misc', __name__, url_prefix='/v1')\n\n\nclass UserList(ResourceList):\n    \"\"\"\n    List and create Users\n    \"\"\"\n    def before_create_object(self, data, view_kwargs):\n        \"\"\"\n        method to check if there is an existing user with same email which is received in data to create a new user\n        :param data:\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if db.session.query(User.id).filter_by(email=data['email'], deleted_at=None).scalar() is not None:\n            raise ConflictException({'pointer': '/data/attributes/email'}, \"Email already exists\")\n\n    def after_create_object(self, user, data, view_kwargs):\n        \"\"\"\n        method to send-\n        email notification\n        mail link for register verification\n        add image urls\n        :param user:\n        :param data:\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        s = get_serializer()\n        hash = str(base64.b64encode(str(s.dumps([user.email, str_generator()])).encode()), 'utf-8')\n        link = make_frontend_url('/email/verify'.format(id=user.id), {'token': hash})\n        send_email_with_action(user, USER_REGISTER_WITH_PASSWORD, app_name=get_settings()['app_name'],\n                               email=user.email)\n        send_email_confirmation(user.email, link)\n\n        if data.get('original_image_url'):\n            uploaded_images = create_save_image_sizes(data['original_image_url'], 'user', user.id)\n            uploaded_images['small_image_url'] = uploaded_images['thumbnail_image_url']\n            del uploaded_images['large_image_url']\n            self.session.query(User).filter_by(id=user.id).update(uploaded_images)\n\n    decorators = (api.has_permission('is_admin', methods=\"GET\"),)\n    schema = UserSchema\n    data_layer = {'session': db.session,\n                  'model': User,\n                  'methods': {\n                      'before_create_object': before_create_object,\n                      'after_create_object': after_create_object\n                  }}\n\n\nclass UserDetail(ResourceDetail):\n    \"\"\"\n    User detail by id\n    \"\"\"\n    def before_get(self, args, kwargs):\n\n        if current_user.is_admin or current_user.is_super_admin or current_user:\n            self.schema = UserSchema\n        else:\n            self.schema = UserSchemaPublic\n\n    def before_get_object(self, view_kwargs):\n        \"\"\"\n        before get method for user object\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if view_kwargs.get('notification_id') is not None:\n            notification = safe_query(self, Notification, 'id', view_kwargs['notification_id'], 'notification_id')\n            if notification.user_id is not None:\n                view_kwargs['id'] = notification.user_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('feedback_id') is not None:\n            print(view_kwargs['feedback_id'])\n            feedback = safe_query(self, Feedback, 'id', view_kwargs['feedback_id'], 'feedback_id')\n            if feedback.user_id is not None:\n                view_kwargs['id'] = feedback.user_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('attendee_id') is not None:\n            attendee = safe_query(self, TicketHolder, 'id', view_kwargs['attendee_id'], 'attendee_id')\n            if attendee.user is not None:\n                if (not has_access('is_user_itself',\n                                   user_id=attendee.user.id) or not has_access('is_coorganizer',\n                                                                               event_id=attendee.event_id)):\n                    raise ForbiddenException({'source': ''}, 'Access Forbidden')\n                view_kwargs['id'] = attendee.user.id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('event_invoice_id') is not None:\n            event_invoice = safe_query(self, EventInvoice, 'id', view_kwargs['event_invoice_id'], 'event_invoice_id')\n            if event_invoice.user_id is not None:\n                view_kwargs['id'] = event_invoice.user_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('users_events_role_id') is not None:\n            users_events_role = safe_query(self, UsersEventsRoles, 'id', view_kwargs['users_events_role_id'],\n                                           'users_events_role_id')\n            if users_events_role.user_id is not None:\n                view_kwargs['id'] = users_events_role.user_id\n\n        if view_kwargs.get('speaker_id') is not None:\n            speaker = safe_query(self, Speaker, 'id', view_kwargs['speaker_id'], 'speaker_id')\n            if speaker.user_id is not None:\n                view_kwargs['id'] = speaker.user_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('session_id') is not None:\n            session = safe_query(self, Session, 'id', view_kwargs['session_id'], 'session_id')\n            if session.creator_id is not None:\n                view_kwargs['id'] = session.creator_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('access_code_id') is not None:\n            access_code = safe_query(self, AccessCode, 'id', view_kwargs['access_code_id'], 'access_code_id')\n            if access_code.marketer_id is not None:\n                view_kwargs['id'] = access_code.marketer_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('discount_code_id') is not None:\n            discount_code = safe_query(self, DiscountCode, 'id', view_kwargs['discount_code_id'], 'discount_code_id')\n            if discount_code.marketer_id is not None:\n                view_kwargs['id'] = discount_code.marketer_id\n            else:\n                view_kwargs['id'] = None\n\n        if view_kwargs.get('email_notification_id') is not None:\n            email_notification = safe_query(self, EmailNotification, 'id', view_kwargs['email_notification_id'],\n                                            'email_notification_id')\n            if email_notification.user_id is not None:\n                view_kwargs['id'] = email_notification.user_id\n            else:\n                view_kwargs['id'] = None\n\n    def before_update_object(self, user, data, view_kwargs):\n        if data.get('original_image_url') and data['original_image_url'] != user.original_image_url:\n            uploaded_images = create_save_image_sizes(data['original_image_url'], 'user', user.id)\n            data['original_image_url'] = uploaded_images['original_image_url']\n            data['small_image_url'] = uploaded_images['thumbnail_image_url']\n            data['thumbnail_image_url'] = uploaded_images['thumbnail_image_url']\n            data['icon_image_url'] = uploaded_images['icon_image_url']\n\n        if data.get('email') and data['email'] != user.email:\n            view_kwargs['email_changed'] = user.email\n\n        if has_access('is_super_admin') and data.get('is_admin') != user.is_admin:\n            user.is_admin = not user.is_admin\n\n    def after_update_object(self, user, data, view_kwargs):\n        \"\"\"\n        method to mail user about email change\n        :param user:\n        :param data:\n        :param view_kwargs:\n        :return:\n        \"\"\"\n        if view_kwargs.get('email_changed'):\n            send_email_change_user_email(user, view_kwargs.get('email_changed'))\n\n    decorators = (api.has_permission('is_user_itself', fetch=\"user_id,id\", fetch_as=\"user_id\",\n                  model=[Notification, Feedback, UsersEventsRoles, Session, EventInvoice, AccessCode,\n                         DiscountCode, EmailNotification, Speaker, User],\n                  fetch_key_url=\"notification_id, feedback_id, users_events_role_id, session_id, \\\n                  event_invoice_id, access_code_id, discount_code_id, email_notification_id, speaker_id, id\",\n                                     leave_if=lambda a: a.get('attendee_id')), )\n    schema = UserSchema\n    data_layer = {'session': db.session,\n                  'model': User,\n                  'methods': {\n                      'before_get_object': before_get_object,\n                      'before_update_object': before_update_object,\n                      'after_update_object': after_update_object\n                  }}\n\n\nclass UserRelationship(ResourceRelationship):\n    \"\"\"\n    User Relationship\n    \"\"\"\n    decorators = (is_user_itself, )\n    schema = UserSchema\n    data_layer = {'session': db.session,\n                  'model': User}\n\n\n@user_misc_routes.route('/users/checkEmail', methods=['POST'])\ndef is_email_available():\n    email = request.json.get('email', None)\n    if email:\n        if get_count(db.session.query(User).filter_by(email=email)):\n            return jsonify(\n                result=\"False\"\n            )\n        else:\n            return jsonify(\n                result=\"True\"\n            )\n    else:\n        abort(\n            make_response(jsonify(error=\"Email field missing\"), 422)\n        )\n", "path": "app/api/users.py"}]}