· 9 years ago · Apr 18, 2016, 07:39 PM
1from shop.models import User
2from shop.models import ManufacturerCountry
3from shop.models import producer
4from shop.models import product
5from shop.models import category
6from shop.models import order
7from django.core.files import File
8from datetime import datetime
9import random
10
11
12def g_username():
13 s = ""
14 for i in range(random.randint(6, 18)):
15 s += random.choice("qwertyuiopasdfghjklzcvbxnm1234567890_-QWERTYUIOPASDFGHJKLZXCVBNM")
16 return s
17
18
19def g_email():
20 s = ""
21 for i in range(random.randint(13, 20)):
22 s += random.choice("qwertyuiopasdfghjklzcvbxnm1234567890")
23 s += random.choice(["@gmail.com", "@mail.ru", "@yandex.ru", "@yahoo.com"])
24 return s
25
26def g_password():
27 s = ""
28 for i in range(random.randint(20, 30)):
29 s += random.choice("qwertyuiopasdfghjklzcvbxnm1234567890 QWERTYUIOPASDFGHJKLZXCVBNM_-")
30 return s
31
32
33def g_name():
34 f = open('Random/names.txt')
35 names = f.readlines()
36 name = random.choice(names).strip()
37 f.close()
38 return name
39
40
41def g_user():
42 return User(username = g_username(), first_name = g_name(), last_name = g_surname(), email = g_email(), password = g_password())
43
44def g_surname():
45 f = open('Random/surnames.txt')
46 surnames = f.readlines()
47 surname = random.choice(surnames).strip()
48 f.close()
49 return surname
50
51
52def g_country():
53 f = open('Random/countries.txt')
54 countries = f.readlines()
55 f.close()
56 return ManufacturerCountry(name = random.choice(countries).strip())
57
58def g_category(i):
59 f = open('Random/categories.txt')
60 categories = f.readlines()
61 name = categories[i]
62 f.close()
63 return category(name = name.strip())
64
65
66def g_producer():
67 f = open('Random/producers.txt')
68 producers = f.readlines()
69 f.close()
70 return producer(name = random.choice(producers).strip())
71
72def g_price():
73 price = random.randint(1, 2000)
74 return price
75
76def get_object(variable):
77 objects = variable.objects.all()[:]
78 return random.choice(objects)
79
80
81def g_product(amount_of_producers, amount_of_countries):
82 f = open('Random/products.txt')
83 products = f.readlines()
84 f.close()
85 price = g_price()
86 producer1 = producer.objects.get(pk=random.randint(1, amount_of_producers))
87 manufacturerCountry = ManufacturerCountry.objects.get(pk=random.randint(1, amount_of_countries))
88 return product(name = random.choice(products).strip(), price = price, producer=producer1, manufacturerCountry = manufacturerCountry)
89
90
91def get_user():
92 users = User.objects.all()[:]
93 return random.choice(users)
94
95
96def g_order(amount_of_users):
97 user = User.objects.get(pk=random.randint(1, amount_of_users))
98 return order(user = user)
99
100def g_products(n):
101 products = []
102 amount_of_producers = producer.objects.all().count()
103 amount_of_countries = ManufacturerCountry.objects.all().count()
104 for i in range(n):
105 products.append(g_product(amount_of_producers, amount_of_countries))
106 product.objects.bulk_create(products)
107
108def g_countries(n = 10):
109 countries = []
110 for i in range(n):
111 countries.append(g_country())
112 ManufacturerCountry.objects.bulk_create(countries)
113
114
115def g_categories(n = 10):
116 objects = []
117 for i in range(n):
118 objects.append(g_category(i))
119 category.objects.bulk_create(objects)
120
121
122def g_producers(n = 10):
123 objects = []
124 for i in range(n):
125 objects.append(g_producer())
126 producer.objects.bulk_create(objects)
127
128
129def g_users(n=10):
130 users = []
131 for i in range(n):
132 users.append(g_user())
133 User.objects.bulk_create(users)
134
135def g_orders(n=10):
136 orders = []
137 amount_of_users = User.objects.all().count()
138 for i in range(n):
139 orders.append(g_order(amount_of_users))
140 order.objects.bulk_create(orders)
141
142def add_product_to_order(x,y):
143 ThroughModel = order.products.through
144 orderr = order.objects.get(id= x)
145 this_product = product.objects.get(id=y)
146 return ThroughModel(order_id = orderr.id, product_id = this_product.id)
147
148
149def add_products_to_orders(n=10):
150 i = 1
151 amount_of_products = product.objects.all().count()
152 while i < n+1:
153 orderr = order.objects.get(id= i)
154 prdcts = []
155 array = []
156 j = 1
157 counter = 0
158 while j < random.randint(2, amount_of_products):
159 f = 0
160 flag = True
161 a = random.randint(1, amount_of_products)
162 #print a
163 while f < counter:
164 if array[f] == a :
165 flag = False
166 f = f + 1
167 if flag == True:
168 prdcts.append(add_product_to_order(i,a))
169 array.append(a)
170 counter = counter + 1
171 j = j+1
172 i = i+1
173 order.products.through.objects.bulk_create(prdcts)
174
175
176def add_product_to_category(x,y):
177 ThroughModel = category.products.through
178 categoryy = category.objects.get(id= x)
179 this_product = product.objects.get(id=y)
180 return ThroughModel(category_id = categoryy.id, product_id = this_product.id)
181
182
183def add_products_to_categories(n=134):
184 i = 1
185 amount_of_products = product.objects.all().count()
186 while i < n+1:
187 categoryy = category.objects.get(id= i)
188 s = categoryy.name
189 prdcts = []
190 array = []
191 j = 1
192 while j < amount_of_products:
193 this_product = product.objects.get(id=j)
194 string = this_product.name
195 if (string.find(s) > -1):
196 prdcts.append(add_product_to_category(i, j))
197 array.append(j)
198 j = j+1
199 i = i+1
200 category.products.through.objects.bulk_create(prdcts)
201
202
203def generate(n=3):
204 #g_users(n=n)
205 #g_countries(n=n)
206 #g_producers(n=n)
207 #g_categories(n=134)
208 #g_orders(n=n)
209 #g_products(n=n)
210 #add_products_to_orders(n=n)
211 add_products_to_categories(n=134)
212
213generate(n=500)