· 9 years ago · Apr 09, 2016, 11:27 AM
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_ManufacturerCountry():
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, 10000)
73 return price
74
75def g_product():
76 f = open('Random/products.txt')
77 products = f.readlines()
78 f.close()
79 return product(name = random.choice(products).strip(), price = g_price(), producer = g_producer(), manufacturerCountry = g_ManufacturerCountry())
80
81
82def get_user():
83 users = User.objects.all()[:]
84 return random.choice(users)
85
86
87def g_order():
88 #order1 = order(user = get_user)
89 #order1.save()
90 #i = random.randint(1, 10)
91 #order1.products.add(i)
92 return order(user = get_user())
93
94def g_products(n):
95 products = []
96 for i in range(n):
97 products.append(g_product())
98 product.objects.bulk_create(products)
99
100def g_categories(n=10):
101 categories = []
102 for i in range(n):
103 categories.append(g_category())
104 category.objects.bulk_create(categories)
105
106def g_users(n=10):
107 users = []
108 for i in range(n):
109 users.append(g_user())
110 User.objects.bulk_create(users)
111
112def g_orders(n=10):
113 orders = []
114 for i in range(n):
115 orders.append(g_order())
116 order.objects.bulk_create(orders)
117 add_products(n=n)
118
119def add_products(n=10):
120 i = 1
121 while i < n+1:
122 orderr = order.objects.get(id= i)
123 #amount_of_products = product.objects.all().count()
124 #j = random.randint(1, amount_of_products)
125 #orderr.products.add(j)
126 amount_of_products = product.objects.all().count()
127 j = 1
128 while j < random.randint(1, amount_of_products):
129 j = random.randint(1, amount_of_products)
130 orderr.products.add(j)
131 j = j+1
132 i = i+1
133 print orderr
134 #order1 = order(user = get_user)
135 #order1.save()
136 #i = random.randint(1, 10)
137 #order1.products.add(i)
138
139#g_users(n=10)
140#g_products(n=10)
141#g_orders(n=10)
142#add_products(n=10)
143#User.objects.all()
144order.objects.all()