1 | from flask import Flask, jsonify, request, abort, g |
||
2 | from flask_restful import Api,Resource, reqparse |
||
3 | from flask_httpauth import HTTPBasicAuth |
||
4 | from passlib.apps import custom_app_context as pass_context |
||
5 | from models import dtlogin, dtusers |
||
6 | |||
7 | from flask_jwt_extended import (create_access_token,create_refresh_token, |
||
8 | jwt_required, jwt_refresh_token_required,get_jwt_identity, get_raw_jwt) |
||
9 | |||
10 | auth = HTTPBasicAuth() |
||
11 | |||
12 | #parsing incoming data |
||
13 | parser = reqparse.RequestParser() |
||
14 | parser.add_argument('username', help = 'Please fill in the username', required = True) |
||
15 | parser.add_argument('password', help= 'This field cannot be blank', required = True) |
||
16 | class UserResource(Resource): |
||
17 | """get all registered users""" |
||
18 | def get(self): |
||
19 | return dtusers, 200 |
||
20 | |||
21 | """create new user""" |
||
22 | def post(self): |
||
23 | #json_data = request.get_json(force=True) |
||
24 | data = parser.parse_args() |
||
25 | |||
26 | |||
27 | try: |
||
28 | dtusers.append(data) |
||
29 | access_token = create_access_token(identity = data['username']) |
||
30 | refresh_token = create_refresh_token(identity = data['username']) |
||
31 | |||
32 | return { |
||
33 | 'message': 'User {} was created'.format(data['username']), |
||
34 | 'access_token': access_token, |
||
35 | 'refresh_token': refresh_token |
||
36 | } |
||
37 | |||
38 | except: |
||
39 | return {'message':'Somenthing went wrong'}, 500 |
||
40 | |||
41 | return {"status":"success", "data": data }, 201 |
||
42 | |||
43 | |||
44 | class User(Resource): |
||
45 | """get user by id""" |
||
46 | def get(self, uid): |
||
47 | for user in dtusers: |
||
48 | if (uid== user['id']): |
||
49 | return user, 200 |
||
50 | return "No record found", 404 |
||
51 | |||
52 | """Modify / update an existing user""" |
||
53 | def put(self, uid): |
||
54 | json_data = request.get_json(force=True) |
||
55 | for user in dtusers: |
||
56 | if (uid == user['id']): |
||
57 | user.update(json_data) |
||
58 | return {"status":"success", "data": json_data }, 201 |
||
59 | return "Request not found", 404 |
||
60 | |||
61 | """Delete user""" |
||
62 | def delete(self, uid): |
||
63 | for user in dtusers: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
64 | if (uid == user['id']): |
||
65 | dtusers[:]=[user for user in dtusers if not(uid == user.get('id'))] |
||
66 | return {"status":"Deleted successfuly", "data": user}, 201 |
||
67 | return "Record not found", 404 |
||
68 |