· 9 years ago · Apr 05, 2016, 01:06 AM
1# coding=utf-8
2from MyInstagram.models import User
3from MyInstagram.models import City
4from MyInstagram.models import Photo
5from MyInstagram.models import Post
6from MyInstagram.models import Comment
7from django.core.files import File
8from datetime import datetime
9import random
10
11
12def gen_username():
13 s = ""
14 for i in range(random.randint(8, 12)):
15 s += random.choice("qwertyuiopasdfghjklzcvbxnm1234567890")
16 return s
17
18
19def gen_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", "@rambler.ru"])
24 return s
25
26
27def gen_password():
28 s = ""
29 for i in range(random.randint(20, 30)):
30 s += random.choice("qwertyuiopasdfghjklzcvbxnm1234567890*.@#$%")
31 return s
32
33
34def gen_first_name(sex=0):
35 if sex == 0:
36 f = open('Generate/names-m.txt')
37 else:
38 f = open('Generate/names-f.txt')
39 names = f.readlines()
40 name = random.choice(names).strip()
41 f.close()
42 return name
43
44
45def get_last_name(sex=0):
46 f = open('Generate/surnames.txt')
47 surnames = f.readlines()
48 surname = random.choice(surnames).strip()
49 if sex == 1:
50 surname += u"а".encode("utf-8")
51 f.close()
52 return surname
53
54
55def get_random_city():
56 f = open('Generate/cities.txt')
57 cities = f.readlines()
58 city = random.choice(cities).strip()
59 obj, created = City.objects.get_or_create(city=city)
60 return obj
61
62
63def gen_user():
64 username = gen_username()
65 email = gen_email()
66 password = gen_password()
67 usr = User(username=username, email=email, password=password)
68 if random.randint(0, 30) <= 20:
69 if random.randint(0, 1) == 0:
70 usr.first_name = gen_first_name()
71 usr.last_name = get_last_name()
72 else:
73 usr.first_name = gen_first_name(sex=1)
74 usr.last_name = get_last_name(sex=1)
75 if random.randint(0, 20) <= 15:
76 usr.city = get_random_city()
77 return usr
78
79
80def gen_users(n=10000):
81 users = []
82 for i in range(n):
83 users.append(gen_user())
84 User.objects.bulk_create(users)
85 for usr in User.objects.all()[:]:
86 usr.subscriptions.add(usr)
87
88
89def get_random_user():
90 users = User.objects.all()[:]
91 return random.choice(users)
92
93
94def get_random_post():
95 posts = Post.objects.all()[:]
96 return random.choice(posts)
97
98
99def get_unique_photo_name():
100 strq = datetime.strftime(datetime.now(), "%Y%m%d_%H%M%S_")
101 strq += str(random.randint(0, 1000000000)) + ".jpg"
102 return strq
103
104
105def get_random_photo():
106 name = str(random.randint(1, 11)) + ".jpg"
107 p = Photo()
108 with open('Generate/Image/' + name, 'r') as f:
109 p.photo.save(get_unique_photo_name(), File(f), True)
110 return p
111
112
113def gen_post():
114 user = get_random_user()
115 photo = get_random_photo()
116 post = Post(user=user, photo=photo)
117 return post
118
119
120def gen_posts(n=10000):
121 posts = []
122 for i in range(n):
123 posts.append(gen_post())
124 Post.objects.bulk_create(posts)
125
126
127def gen_random_comment():
128 f = open('Generate/comments.txt')
129 comments = f.readlines()
130 comment = random.choice(comments).strip()
131 f.close()
132 return comment
133
134
135def gen_comment():
136 user = get_random_user()
137 post = get_random_post()
138 text = gen_random_comment()
139 return Comment(user=user, post=post, text=text)
140
141
142def gen_comments(n=10000):
143 comments = []
144 for i in range(n):
145 comments.append(gen_comment())
146 Comment.objects.bulk_create(comments)
147
148
149def gen_like():
150 post = get_random_post()
151 while post.like_users.count() == User.objects.all().count():
152 post = get_random_post()
153 usr = get_random_user()
154 while usr in post.like_users.all()[:]:
155 usr = get_random_user()
156 post.like_users.add(usr)
157
158
159def gen_likes(n=10000):
160 for i in range(n):
161 gen_like()
162
163
164def gen_subscribe():
165 usr1 = get_random_user()
166 while usr1.subscriptions.count() == User.objects.all().count():
167 usr1 = get_random_user()
168 usr2 = get_random_user()
169 while usr2 in usr1.subscriptions.all()[:]:
170 usr2 = get_random_user()
171 usr1.subscriptions.add(usr2)
172
173
174def gen_subscriptions(n=10000):
175 for i in range(n):
176 gen_subscribe()