-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.py
executable file
·143 lines (127 loc) · 3.41 KB
/
console.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python3
""" HAANNA console """
import cmd
import models.mongo_setup as mongo_setup
import services.data_service as svc
import infraestructure.state as state
import json
import shlex
from models.user import Users, UserExists
from models.reviews import Reviews
from models.stores import Stores
classes = {'Users': Users, 'Reviews': Reviews, 'Stores': Stores}
class HAANACommand(cmd.Cmd):
""" HANNACommand class """
mongo_setup.global_init()
prompt = "(haana) "
def do_quit(self, line):
""" quit command """
return True
def do_EOF(self, line):
""" EOF command """
return True
def emptyline(self):
""" emptyline """
pass
def do_log(self, line):
""" Logg in to the app """
data = line.split()
email = data[0]
account = svc.find_account(email)
if not account:
print(f"Could not fin account with email {email}")
return
state.active_account = account
print("Logged in Succesfully.")
def do_make_review(self, line):
""" Reviews make by the user for the store
this function receives a JSON"""
if not state.active_account:
print("You must login first to register a review.")
else:
line1 = json.loads(line)
for key, value in line1.items():
if (key == "store_id"):
store_id = value
if (key == "description"):
description = value
if (key == "score"):
score = value
user_id = state.active_account.id
svc.write_review(store_id, user_id, description, score)
print("Review saved correctly")
def do_list_reviews(self, line):
""" Recives one argument: store_id """
reviews = svc.show_reviews(line)
print(reviews)
def do_list_store(self, line):
""" Recives two arguments type of store
and the location"""
line1 = json.loads(line)
for key, value in line1.items():
if (key == "type"):
type_store = value
if (key == "location"):
location = value
store = svc.show_stores(type_store, location)
print(store)
def do_add_product(self, line):
""" Add a producto to an store, recieving an JSON
with an store _id and producto info (name, value,
description and img_link) """
line1 = json.loads(line)
for key, value in line1.items():
if (key == "_id"):
_id = value
if (key == "name"):
name = value
if (key == "value"):
value = value
if (key == "description"):
description = value
if (key == "img_link"):
img_link = value
new_product = svc.add_product(_id, name, value, description, img_link)
print(new_product)
def do_update_prod(self, line):
svc.update_product()
svc.create_product()
def do_create(self, line):
args = line.split()
if len(line) == 0:
print("Class missing")
return
if args[0] in classes:
new_dict = self._key_value_parser(args[1:])
instance = classes[args[0]](**new_dict)
else:
print("* class doesn't exist *")
return False
try:
instance.save()
print(instance.to_json())
except UserExists:
print("Email already used!!")
del instance
def _key_value_parser(self, args):
"""creates a dictionary from a list of strings"""
new_dict = {}
for arg in args:
if "=" in arg:
kvp = arg.split('=', 1)
key = kvp[0]
value = kvp[1]
if value[0] == value[-1] == '"':
value = shlex.split(value)[0].replace('_', ' ')
else:
try:
value = int(value)
except:
try:
value = float(value)
except:
continue
new_dict[key] = value
return new_dict
if __name__ == '__main__':
HAANACommand().cmdloop()