Spaces:
Runtime error
Runtime error
Vinay Jose commited on
Create models.py
Browse files
models.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import time
|
| 3 |
+
# ========================================================
|
| 4 |
+
# Contact Model
|
| 5 |
+
# ========================================================
|
| 6 |
+
PAGE_SIZE = 100
|
| 7 |
+
|
| 8 |
+
class Contact:
|
| 9 |
+
# mock contacts database
|
| 10 |
+
db = {}
|
| 11 |
+
def __init__(self, id_=None, first=None, last=None, phone=None, email=None):
|
| 12 |
+
self.id = id_
|
| 13 |
+
self.first = first
|
| 14 |
+
self.last = last
|
| 15 |
+
self.phone = phone
|
| 16 |
+
self.email = email
|
| 17 |
+
self.errors = {}
|
| 18 |
+
|
| 19 |
+
def __str__(self):
|
| 20 |
+
return json.dumps(self.__dict__, ensure_ascii=False)
|
| 21 |
+
|
| 22 |
+
def update(self, first, last, phone, email):
|
| 23 |
+
self.first = first
|
| 24 |
+
self.last = last
|
| 25 |
+
self.phone = phone
|
| 26 |
+
self.email = email
|
| 27 |
+
|
| 28 |
+
def validate(self):
|
| 29 |
+
if not self.email:
|
| 30 |
+
self.errors['email'] = "Email Required"
|
| 31 |
+
existing_contact = next(filter(lambda c: c.id != self.id and c.email == self.email, Contact.db.values()), None)
|
| 32 |
+
if existing_contact:
|
| 33 |
+
self.errors['email'] = "Email Must Be Unique"
|
| 34 |
+
return len(self.errors) == 0
|
| 35 |
+
|
| 36 |
+
def save(self):
|
| 37 |
+
if not self.validate():
|
| 38 |
+
return False
|
| 39 |
+
if self.id is None:
|
| 40 |
+
if len(Contact.db) == 0:
|
| 41 |
+
max_id = 1
|
| 42 |
+
else:
|
| 43 |
+
max_id = max(contact.id for contact in Contact.db.values())
|
| 44 |
+
self.id = max_id + 1
|
| 45 |
+
Contact.db[self.id] = self
|
| 46 |
+
Contact.save_db()
|
| 47 |
+
return True
|
| 48 |
+
|
| 49 |
+
def delete(self):
|
| 50 |
+
del Contact.db[self.id]
|
| 51 |
+
Contact.save_db()
|
| 52 |
+
|
| 53 |
+
@classmethod
|
| 54 |
+
def count(cls):
|
| 55 |
+
time.sleep(2)
|
| 56 |
+
return len(cls.db)
|
| 57 |
+
|
| 58 |
+
@classmethod
|
| 59 |
+
def all(cls, page=1):
|
| 60 |
+
page = int(page)
|
| 61 |
+
start = (page - 1) * PAGE_SIZE
|
| 62 |
+
end = start + PAGE_SIZE
|
| 63 |
+
return list(cls.db.values())[start:end]
|
| 64 |
+
|
| 65 |
+
@classmethod
|
| 66 |
+
def search(cls, text):
|
| 67 |
+
result = []
|
| 68 |
+
for c in cls.db.values():
|
| 69 |
+
match_first = c.first is not None and text in c.first
|
| 70 |
+
match_last = c.last is not None and text in c.last
|
| 71 |
+
match_email = c.email is not None and text in c.email
|
| 72 |
+
match_phone = c.phone is not None and text in c.phone
|
| 73 |
+
if match_first or match_last or match_email or match_phone:
|
| 74 |
+
result.append(c)
|
| 75 |
+
return result
|
| 76 |
+
|
| 77 |
+
@classmethod
|
| 78 |
+
def load_db(cls):
|
| 79 |
+
with open('contacts.json', 'r') as contacts_file:
|
| 80 |
+
contacts = json.load(contacts_file)
|
| 81 |
+
cls.db.clear()
|
| 82 |
+
for c in contacts:
|
| 83 |
+
cls.db[c['id']] = Contact(c['id'], c['first'], c['last'], c['phone'], c['email'])
|
| 84 |
+
|
| 85 |
+
@staticmethod
|
| 86 |
+
def save_db():
|
| 87 |
+
out_arr = [c.__dict__ for c in Contact.db.values()]
|
| 88 |
+
with open("contacts.json", "w") as f:
|
| 89 |
+
json.dump(out_arr, f, indent=2)
|
| 90 |
+
|
| 91 |
+
@classmethod
|
| 92 |
+
def find(cls, id_):
|
| 93 |
+
id_ = int(id_)
|
| 94 |
+
c = cls.db.get(id_)
|
| 95 |
+
if c is not None:
|
| 96 |
+
c.errors = {}
|
| 97 |
+
return c
|
| 98 |
+
|