-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.py
More file actions
116 lines (89 loc) · 3.45 KB
/
User.py
File metadata and controls
116 lines (89 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
import uuid
from flask import Flask
from flask import request
from flask import render_template
from werkzeug.security import generate_password_hash, check_password_hash
from flask_httpauth import HTTPBasicAuth
from database import SQLite
from errors import ApplicationError
app = Flask(__name__)
class User:
def __init__(self,id,email,password,name,address,phone_number):
self.id=id
self.email=email
self.password=password
self.name=name
self.address=address
self.phone_number=phone_number
def to_dict(self):
user_data = self.__dict__
del user_data["password"]
return user_data
@staticmethod
def create_user(user, password, email, address, phone_number):
result = None
with SQLite() as db:
result = db.execute("INSERT INTO user (username, password, email, address, phone_number) VALUES (?, ?, ?, ?, ?)",
(user, password, email, address, phone_number,))
if result.rowcount == 0:
raise ApplicationError("No value present", 404)
@staticmethod
def find_by_username(username):
result = None
with SQLite() as db:
result = db.execute(
"SELECT id, email, password, username, address, phone_number FROM user WHERE username = ?",
(username,))
user = result.fetchone()
if user is None:
raise ApplicationError(
"Post with name {} not found".format(username), 404)
return User(*user)
@staticmethod
def find_by_id(id):
result = None
with SQLite() as db:
result = db.execute(
"SELECT id, email, password, username, address, phone_number FROM user WHERE id = ?",
(id,))
user = result.fetchone()
if user is None:
raise ApplicationError(
"Post with id {} not found".format(id), 404)
return User(*user)
@staticmethod
def edit(user_id, part_to_edit, value):
if part_to_edit=="email" or part_to_edit=="password":
raise ApplicationError("You can't change your email or password",404)
elif part_to_edit=="username":
with SQLite() as db:
result = db.execute(
"UPDATE user SET username = ? WHERE id = ?;",
(value, user_id,))
elif part_to_edit=="address":
with SQLite() as db:
result = db.execute(
"UPDATE user SET address = ? WHERE id = ?;",
(value, user_id,))
elif part_to_edit=="phone_number":
with SQLite() as db:
result = db.execute(
"UPDATE user SET phone_number = ? WHERE id = ?;",
(value, user_id,))
else:
raise ApplicationError("{} doesnt exist".format(part_to_edit),404)
@staticmethod
def all():
with SQLite() as db:
result = db.execute(
"SELECT id, email, password, username, address, phone_number FROM user").fetchall()
return [User(*row) for row in result]
@staticmethod
def delete(id):
result = None
with SQLite() as db:
result = db.execute("DELETE FROM user WHERE id = ?",
(id,))
if result.rowcount == 0:
raise ApplicationError("No value present", 404)