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

59 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-09-21 16:24 +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 Review, 

12) 

13from src.products.models.product import Bracelet, Earring, Necklace, Pendant, Ring, Watch 

14 

15 

16UserModel = get_user_model() 

17 

18 

19sample_reviews = [ 

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

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

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

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

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

25 "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.", 

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

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

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

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

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

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

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

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

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

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

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

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

38] 

39 

40 

41class Command(BaseCommand): 

42 help = 'Creating products' 

43 

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

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

46 

47 django.setup() 

48 

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

50 

51 created_users = self.create_fake_users() 

52 self.create_fake_reviews(created_users) 

53 

54 self.stdout.write( 

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

56 ) 

57 

58 def create_fake_users(self): 

59 

60 users_data = [ 

61 { 

62 'username': 'Simon', 

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

64 'password': '!1Aabb', 

65 'first_name': 'Simon', 

66 'last_name': 'Smith', 

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

68 }, 

69 { 

70 'username': 'Ava', 

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

72 'password': '!1Aabb', 

73 'first_name': 'Ava', 

74 'last_name': 'Johnson', 

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

76 }, 

77 { 

78 'username': 'Sophia', 

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

80 'password': '!1Aabb', 

81 'first_name': 'Sophia', 

82 'last_name': 'Brown', 

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

84 }, 

85 { 

86 'username': 'Michael', 

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

88 'password': '!1Aabb', 

89 'first_name': 'Michael', 

90 'last_name': 'Clark', 

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

92 }, 

93 { 

94 'username': 'Emma', 

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

96 'password': '!1Aabb', 

97 'first_name': 'Emma', 

98 'last_name': 'Watson', 

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

100 }, 

101 { 

102 'username': 'Olivia', 

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

104 'password': '!1Aabb', 

105 'first_name': 'Olivia', 

106 'last_name': 'Smith', 

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

108 }, 

109 { 

110 'username': 'William', 

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

112 'password': '!1Aabb', 

113 'first_name': 'William', 

114 'last_name': 'Lewis', 

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

116 }, 

117 { 

118 'username': 'Isabella', 

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

120 'password': '!1Aabb', 

121 'first_name': 'Isabella', 

122 'last_name': 'Jones', 

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

124 }, 

125 { 

126 'username': 'Mia', 

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

128 'password': '!1Aabb', 

129 'first_name': 'Mia', 

130 'last_name': 'Davis', 

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

132 }, 

133 { 

134 'username': 'Liam', 

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

136 'password': '!1Aabb', 

137 'first_name': 'Liam', 

138 'last_name': 'Martin', 

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

140 }, 

141 { 

142 'username': 'Amelia', 

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

144 'password': '!1Aabb', 

145 'first_name': 'Amelia', 

146 'last_name': 'Wilson', 

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

148 }, 

149 ] 

150 

151 created_users = [] 

152 

153 for item in users_data: 

154 user, created = UserModel.objects.get_or_create( 

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

156 ) 

157 

158 if created: 

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

160 user.save() 

161 

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

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

164 user.userprofile.save() 

165 

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

167 user.userphoto.save() 

168 

169 created_users.append(user) 

170 

171 return created_users 

172 

173 def create_fake_reviews(self, users): 

174 earrings = Earring.objects.all() 

175 necklaces = Necklace.objects.all() 

176 pendants = Pendant.objects.all() 

177 bracelets = Bracelet.objects.all() 

178 watches = Watch.objects.all() 

179 rings = Ring.objects.all() 

180 

181 earrings_content_type = ContentType.objects.get_for_model( 

182 Earring) 

183 necklaces_content_type = ContentType.objects.get_for_model(Necklace) 

184 pendants_content_type = ContentType.objects.get_for_model(Pendant) 

185 bracelets_content_type = ContentType.objects.get_for_model(Bracelet) 

186 watches_content_type = ContentType.objects.get_for_model(Watch) 

187 rings_content_type = ContentType.objects.get_for_model(Ring) 

188 

189 all_products_with_their_content_types = [ 

190 [earrings, earrings_content_type], 

191 [necklaces, necklaces_content_type], 

192 [pendants, pendants_content_type], 

193 [bracelets, bracelets_content_type], 

194 [watches, watches_content_type], 

195 [rings, rings_content_type], 

196 ] 

197 

198 for element in all_products_with_their_content_types: 

199 products, content_type = element 

200 

201 for product in products: 

202 

203 shuffled_users = users.copy() 

204 random.shuffle(shuffled_users) 

205 

206 first_six_users = shuffled_users[:6] 

207 

208 for user in first_six_users: 

209 rating = randint(3, 5) 

210 comment = random.choice(sample_reviews) 

211 

212 Review.objects.create( 

213 user=user, 

214 rating=rating, 

215 approved=True, 

216 comment=comment, 

217 content_type=content_type, 

218 object_id=product.pk, 

219 )