-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoppinglists.py
140 lines (125 loc) · 5.3 KB
/
shoppinglists.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
import re
Shoppingitems = []
# an empty list to store my item
class Shoppinglist(object):
Shoppinglists = {}
sharedshoppinglists = {}
# an empty list to store my shoppinglist
def __init__(self, shoppinglistname=None, owner=None, itemname=None):
"""initializing class instance variables"""
self.shoppinglistname = shoppinglistname
self.itemname = itemname
self.owner = owner
def create(self, shoppinglistname, owner):
"""defining method to create shopping list"""
if re.match("[a-zA-Z0-9- .]+$", shoppinglistname):
if shoppinglistname != '':
# call the get_myshopping_lists function that contains individual
# user shoppinglists
my_shoppings = self.get_myshopping_lists(owner)
if my_shoppings != {}:
# check's if user already has a shopping list
if shoppinglistname not in my_shoppings.keys():
self.Shoppinglists[shoppinglistname] = {
'shoppinglistname': shoppinglistname,
'owner': owner,
}
return 1
return 2
else:
# user's first shopping list
self.Shoppinglists[shoppinglistname] = {
'shoppinglistname': shoppinglistname,
'owner': owner,
}
return 1
return 3
return 4
def get_myshopping_lists(self, owner):
"""defining method to get one user's shopping lists"""
data = self.Shoppinglists
my_shoppings = {}
for shoppinglistname in data.keys():
# loop through the shoppinglistnames in the shopping list and
# assign the dictionary to variables
shopping = data[shoppinglistname]
shoppingowner = shopping['owner']
if shoppingowner == owner:
my_shoppings[shoppinglistname] = {
'shoppinglistname': shoppinglistname,
'owner': owner,
}
else:
result = my_shoppings
return my_shoppings
def delete(self, shoppinglistname):
"""defining method to delete shopping list"""
if shoppinglistname in self.Shoppinglists.keys():
# checks if the shoppinglistname being deleted exists
if Shoppingitems:
for dic in range(0, len(Shoppingitems)):
if Shoppingitems[dic]['shoppinglistname'] == shoppinglistname:
del Shoppingitems[dic]['itemname']
del Shoppingitems[dic]['shoppinglistname']
del Shoppingitems[dic]['owner']
del self.Shoppinglists[shoppinglistname]
return 1
return 2
else:
del self.Shoppinglists[shoppinglistname]
return 1
return 2
def get_shopping_lists(self):
"""defining method to get all shopping lists"""
return self.Shoppinglists
def get_shopping_list(self, shoppinglistname):
"""defining method to get one shopping list"""
return self.Shoppinglists[shoppinglistname]
def edit(self, old, shoppinglistname, owner):
"""defining method to edit shopping list"""
if re.match("[a-zA-Z0-9- .]+$", shoppinglistname):
if shoppinglistname != '':
if old in self.Shoppinglists.keys():
del self.Shoppinglists[old]
self.Shoppinglists[shoppinglistname] = {
'shoppinglistname': shoppinglistname,
'owner': owner,
}
return 1
return 3
return 2
return 4
@classmethod
def createitem(cls, itemname, shoppinglistname, owner):
"""defining method to create an item in a shopping list"""
if re.match("[a-zA-Z0-9- .]+$", itemname):
if itemname != '':
Shoppingitems.append(
{'shoppinglistname': shoppinglistname, 'itemname': itemname, 'owner': owner })
return 1
return 2
return 3
@classmethod
def getitems(cls):
""" defining method to delete an item from shopping list"""
return Shoppingitems
@classmethod
def itemedit(cls, itemname, old):
"""defining method to edit an item in a shopping"""
if re.match("[a-zA-Z0-9- .]+$", itemname):
if itemname != "":
for dic in range(len(Shoppingitems)):
if Shoppingitems[dic]['itemname'] == old:
del Shoppingitems[dic]['itemname']
Shoppingitems[dic]['itemname'] = itemname
return 1
return 2
return 3
@classmethod
def deleteitem(cls, itemname, shoppinglistname):
""" defining method to delete an item from shopping"""
for dic in range(0, len(Shoppingitems)):
if Shoppingitems[dic]['itemname'] == itemname and Shoppingitems[dic]['shoppinglistname'] == shoppinglistname:
del Shoppingitems[dic]
return 1
return 2