Coverage for src/products/management/commands/create_reviews.py: 0%

54 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-08-04 12:59 +0300

1import os 

2import random 

3from random import randint 

4import django 

5from django.core.management.base import BaseCommand 

6from django.contrib.contenttypes.models import ContentType 

7 

8from django.contrib.auth import get_user_model 

9 

10from src.products.models import ( 

11 Earwear, 

12 Fingerwear, 

13 Neckwear, 

14 Review, 

15 Wristwear, 

16) 

17 

18 

19UserModel = get_user_model() 

20 

21 

22sample_reviews = [ 

23 "Beautiful piece, exactly as described. The craftsmanship is impressive, and it really stands out on special occasions.", 

24 "The gemstone sparkles brilliantly in the light, catching everyone's attention. It adds just the right amount of sparkle to any outfit.", 

25 "A bit smaller than expected, but still lovely. It’s delicate and subtle, fitting perfectly with many styles.", 

26 "Shipping was fast and the packaging was excellent. The item arrived safely and looked brand new right out of the box.", 

27 "Perfect gift for a loved one, highly recommend! They wear it every day without fail.", 

28 "The attention to detail is remarkable. You can tell a lot of care went into making this piece. I’m very happy with my purchase.", 

29 "The stones are well set and the finish is flawless. It looks expensive but was surprisingly affordable.", 

30 "I bought this as a gift, and it was very well received. The recipient hasn’t taken it off since!", 

31 "Comfortable, stylish, and well-made — everything I was looking for in a jewelry piece.", 

32 "This item exceeded my expectations for the price. It’s now a favorite in my collection.", 

33 "It looks great with both casual clothes and more formal attire, very versatile.", 

34 "I appreciate the durability — it’s been through daily wear and still looks new.", 

35 "The packaging was lovely, making it perfect for gifting right away.", 

36 "Subtle yet elegant — just the right touch without being too flashy. Works with all kinds of looks.", 

37 "Feels personal and thoughtfully made. It really shows that it’s crafted with intention.", 

38 "This piece has a timeless design that fits any style, whether minimal or bold.", 

39 "Matches beautifully with other accessories, and the quality is consistent across the board.", 

40 "It arrived ready to gift, with care in every detail — from the item itself to the packaging.", 

41] 

42 

43 

44class Command(BaseCommand): 

45 help = 'Creating products' 

46 

47 def handle(self, *args, **options): 

48 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.settings') 

49 

50 django.setup() 

51 

52 self.stdout.write(self.style.SUCCESS('Starting creating reviews...')) 

53 

54 created_users = self.create_fake_users() 

55 self.create_fake_reviews(created_users) 

56 

57 self.stdout.write( 

58 self.style.SUCCESS('All reviews created successfully.') 

59 ) 

60 

61 def create_fake_users(self): 

62 

63 users_data = [ 

64 { 

65 'username': 'Simon', 

66 'email': 'simon.smith@mail.com', 

67 'password': '!1Aabb', 

68 'first_name': 'Simon', 

69 'last_name': 'Smith', 

70 'photo': 'image/upload/v1748258755/boy2_aijwxt_rqfolu.jpg', 

71 }, 

72 { 

73 'username': 'Ava', 

74 'email': 'ava.johnson@mail.com', 

75 'password': '!1Aabb', 

76 'first_name': 'Ava', 

77 'last_name': 'Johnson', 

78 'photo': 'image/upload/v1748258753/3d-illustration-cute-little-girl-with-backpack-her-hands_fxkmfo_nflhb4.jpg', 

79 }, 

80 { 

81 'username': 'Sophia', 

82 'email': 'sophia.brown@mail.com', 

83 'password': '!1Aabb', 

84 'first_name': 'Sophia', 

85 'last_name': 'Brown', 

86 'photo': 'image/upload/v1748258753/3d-illustration-cute-little-girl-hat-jacket_bdlvpk_qo4b95.jpg', 

87 }, 

88 { 

89 'username': 'Michael', 

90 'email': 'michael.clark@mail.com', 

91 'password': '!1Aabb', 

92 'first_name': 'Michael', 

93 'last_name': 'Clark', 

94 'photo': 'image/upload/v1748258754/little-boy-cap-with-backpack-street-3d-rendering_i2mw52_frkquw.jpg', 

95 }, 

96 { 

97 'username': 'Emma', 

98 'email': 'emma.watson@mail.com', 

99 'password': '!1Aabb', 

100 'first_name': 'Emma', 

101 'last_name': 'Watson', 

102 'photo': 'image/upload/v1748258754/girl2_rjcjz2_y54zla.jpg', 

103 }, 

104 { 

105 'username': 'Olivia', 

106 'email': 'olivia.smith@mail.com', 

107 'password': '!1Aabb', 

108 'first_name': 'Olivia', 

109 'last_name': 'Smith', 

110 'photo': 'image/upload/v1748258753/3d-illustration-cute-little-girl-bokeh-background_wyylpf_seqqgg.jpg', 

111 }, 

112 { 

113 'username': 'William', 

114 'email': 'william.lewis@mail.com', 

115 'password': '!1Aabb', 

116 'first_name': 'William', 

117 'last_name': 'Lewis', 

118 'photo': 'image/upload/v1748258753/boy1_cli59g_czbmce.jpg', 

119 }, 

120 { 

121 'username': 'Isabella', 

122 'email': 'isabella.jones@mail.com', 

123 'password': '!1Aabb', 

124 'first_name': 'Isabella', 

125 'last_name': 'Jones', 

126 'photo': 'image/upload/v1748258753/girl1_lclcth_lqhndu.jpg', 

127 }, 

128 { 

129 'username': 'Mia', 

130 'email': 'mia.davis@mail.com', 

131 'password': '!1Aabb', 

132 'first_name': 'Mia', 

133 'last_name': 'Davis', 

134 'photo': 'image/upload/v1748267948/bambino-cartoon-carino-che-posa-per-il-ritratto_z6edpw.jpg', 

135 }, 

136 { 

137 'username': 'Liam', 

138 'email': 'liam.martin@mail.com', 

139 'password': '!1Aabb', 

140 'first_name': 'Liam', 

141 'last_name': 'Martin', 

142 'photo': 'image/upload/v1748267949/l-uomo-del-fitness-dei-cartoni-animati-3d_egayjv.jpg', 

143 }, 

144 { 

145 'username': 'Amelia', 

146 'email': 'amelia.wilson@mail.com', 

147 'password': '!1Aabb', 

148 'first_name': 'Amelia', 

149 'last_name': 'Wilson', 

150 'photo': 'image/upload/v1748267948/cute-cartoon-kid-posing-portrait_kknsfr.jpg', 

151 }, 

152 ] 

153 

154 created_users = [] 

155 

156 for item in users_data: 

157 user, created = UserModel.objects.get_or_create( 

158 email=item['email'], defaults={'username': item['username']} 

159 ) 

160 

161 if created: 

162 user.set_password(item['password']) 

163 user.save() 

164 

165 user.userprofile.first_name = item['first_name'] 

166 user.userprofile.last_name = item['last_name'] 

167 user.userprofile.save() 

168 

169 user.userphoto.photo = item['photo'] 

170 user.userphoto.save() 

171 

172 created_users.append(user) 

173 

174 return created_users 

175 

176 def create_fake_reviews(self, users): 

177 fingerwears = Fingerwear.objects.all() 

178 wristwears = Wristwear.objects.all() 

179 neckwears = Neckwear.objects.all() 

180 earwears = Earwear.objects.all() 

181 

182 fingerwear_content_type = ContentType.objects.get_for_model(Fingerwear) 

183 wristwears_content_type = ContentType.objects.get_for_model(Wristwear) 

184 neckwears_content_type = ContentType.objects.get_for_model(Neckwear) 

185 earwears_content_type = ContentType.objects.get_for_model(Earwear) 

186 

187 all_products_with_their_content_types = [ 

188 [fingerwears, fingerwear_content_type], 

189 [wristwears, wristwears_content_type], 

190 [neckwears, neckwears_content_type], 

191 [earwears, earwears_content_type], 

192 ] 

193 

194 for element in all_products_with_their_content_types: 

195 products, content_type = element 

196 

197 for product in products: 

198 

199 shuffled_users = users.copy() 

200 random.shuffle(shuffled_users) 

201 

202 first_six_users = shuffled_users[:6] 

203 

204 for user in first_six_users: 

205 rating = randint(3, 5) 

206 comment = random.choice(sample_reviews) 

207 

208 Review.objects.create( 

209 user=user, 

210 rating=rating, 

211 approved=True, 

212 comment=comment, 

213 content_type=content_type, 

214 object_id=product.pk, 

215 )