· 9 years ago · Apr 09, 2016, 04:42 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():
59 f = open('Random/categories.txt')
60 categories = f.readlines()
61 f.close()
62 return category(name = random.choice(categories).strip())
63
64
65def g_producer():
66 f = open('Random/producers.txt')
67 producers = f.readlines()
68 f.close()
69 return producer(name = random.choice(producers).strip())
70
71def g_price():
72 price = random.randint(1, 2000)
73 return price
74
75def get_object(variable):
76 objects = variable.objects.all()[:]
77 return random.choice(objects)
78
79
80def g_product(amount_of_producers, amount_of_countries):
81 f = open('Random/products.txt')
82 products = f.readlines()
83 f.close()
84 price = g_price()
85 producer1 = producer.objects.get(pk=random.randint(1, amount_of_producers))
86 manufacturerCountry = ManufacturerCountry.objects.get(pk=random.randint(1, amount_of_countries))
87 return product(name = random.choice(products).strip(), price = price, producer=producer1, manufacturerCountry = manufacturerCountry)
88
89
90def get_user():
91 users = User.objects.all()[:]
92 return random.choice(users)
93
94
95def g_order(amount_of_users):
96 #order1 = order(user = get_user)
97 #order1.save()
98 #i = random.randint(1, 10)
99 #order1.products.add(i)
100 user = User.objects.get(pk=random.randint(1, amount_of_users))
101 return order(user = user)
102
103def g_products(n):
104 products = []
105 amount_of_producers = producer.objects.all().count()
106 amount_of_countries = ManufacturerCountry.objects.all().count()
107 for i in range(n):
108 products.append(g_product(amount_of_producers, amount_of_countries))
109 product.objects.bulk_create(products)
110
111def g_countries(n = 10):
112 countries = []
113 for i in range(n):
114 countries.append(g_country())
115 ManufacturerCountry.objects.bulk_create(countries)
116
117
118def g_categories(n = 10):
119 objects = []
120 for i in range(n):
121 objects.append(g_category())
122 category.objects.bulk_create(objects)
123
124def g_producers(n = 10):
125 objects = []
126 for i in range(n):
127 objects.append(g_producer())
128 producer.objects.bulk_create(objects)
129
130
131def g_users(n=10):
132 users = []
133 for i in range(n):
134 users.append(g_user())
135 User.objects.bulk_create(users)
136
137def g_orders(n=10):
138 orders = []
139 amount_of_users = User.objects.all().count()
140 for i in range(n):
141 orders.append(g_order(amount_of_users))
142 order.objects.bulk_create(orders)
143
144def add_products_to_orders(n=10):
145 i = 1
146 amount_of_products = product.objects.all().count()
147 while i < n+1:
148 orderr = order.objects.get(id= i)
149 j = 1
150 while j < random.randint(1, 10):
151 y = random.randint(1, amount_of_products)
152 j = j+1
153 orderr.products.add(y)
154 i = i+1
155 #order1 = order(user = get_user)
156 #order1.save()
157 #i = random.randint(1, 10)
158 #order1.products.add(i)
159
160def add_products_to_categories(n=10):
161 i = 1
162 amount_of_products = product.objects.all().count()
163 while i < n+1:
164 categoryy = category.objects.get(id= i)
165 #amount_of_products = product.objects.all().count()
166 #j = random.randint(1, amount_of_products)
167 #orderr.products.add(j)
168 j = 1
169 while j < random.randint(2, 10):
170 j = random.randint(1, amount_of_products)
171 categoryy.products.add(j)
172 j = j+1
173 i = i+1
174
175#countries = []
176#producers = []
177#categories = []
178
179def generate(n=3):
180 #g_users(n=n)
181 #g_countries(n=n)
182 #g_producers(n=n)
183 #g_categories(n=n)
184 #g_orders(n=n)
185 #g_products(n=n)
186 add_products_to_orders(n=n)
187
188generate(n=10000)
189#g_products(n=5)
190#g_orders(n=10000)
191#add_products(n=10)
192#User.objects.all()
193#order.objects.all()