This repository has been archived by the owner on Jan 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
71 lines (52 loc) · 1.59 KB
/
db.py
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
from peewee import *
from playhouse.shortcuts import model_to_dict, dict_to_model
import datetime
import helper
db = SqliteDatabase('clinics.db')
class BaseModel(Model):
class Meta:
database = db
class Clinic(BaseModel):
name = TextField()
id = CharField(unique=True)
date_added = DateTimeField(default=datetime.datetime.now)
address = TextField()
latitude = FloatField(null=True)
longitude = FloatField(null=True)
registered = BooleanField(default=False)
@classmethod
def get_as_dict(cls, expr):
query = cls.select().where(expr).dicts()
return query.get()
def get_info(id="all"):
if id == "all":
data = Clinic.select()
result = []
for i in data:
result.append(model_to_dict(i))
return result
else:
return model_to_dict(Clinic.get_by_id(id))
def is_registered(id):
clinic = Clinic.get_or_none(Clinic.id == id)
if clinic is not None:
return clinic.registered, clinic
def register_at(clinic: Clinic):
clinic.registered = True
clinic.save()
if __name__ == '__main__':
db.connect()
db.create_tables([Clinic])
clinics = []
for url in helper.get_doctors():
details = helper.get_clinic_details(url)
if details is not None:
clinics.append(details)
for c in clinics:
existing = Clinic.get_or_none(Clinic.id == c.get('id'))
if not existing:
clinic = Clinic.create(**c)
print(f"{clinic.name} got added to DB")
db.close()
else:
print(f"db is imported into another module")