-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_test_data.py
More file actions
154 lines (135 loc) · 5.73 KB
/
load_test_data.py
File metadata and controls
154 lines (135 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import django
import random
from datetime import timedelta
from io import BytesIO
import qrcode
from django.core.files.uploadedfile import SimpleUploadedFile
from myApp.config import DOMAIN, PLANS
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'beQR.settings')
django.setup()
from django.utils import timezone
from django.contrib.auth import get_user_model
from myApp.models import SubscriptionPlan, Item, NotificationPreference, Notification
Customer = get_user_model()
def create_subscription_plans():
for plan_name, plan_data in PLANS.items():
SubscriptionPlan.objects.update_or_create(
name=plan_name,
defaults=plan_data
)
print("Planes de suscripción creados o actualizados.")
def create_customers():
plans = list(SubscriptionPlan.objects.all())
customers = [
{
'username': 'usuario1',
'email': 'usuario1@example.com',
'password': 'contraseña123',
'first_name': 'Usuario',
'last_name': 'Uno',
'gender': 'M',
'phone': '123456789',
},
{
'username': 'usuario2',
'email': 'usuario2@example.com',
'password': 'contraseña123',
'first_name': 'Usuario',
'last_name': 'Dos',
'gender': 'F',
'phone': '987654321',
},
{
'username': 'usuario3',
'email': 'usuario3@example.com',
'password': 'contraseña123',
'first_name': 'Usuario',
'last_name': 'Tres',
'gender': 'O',
'phone': '555555555',
},
]
for i, customer_data in enumerate(customers):
customer, created = Customer.objects.update_or_create(
username=customer_data['username'],
defaults=customer_data
)
if created:
customer.set_password(customer_data['password'])
customer.subscription_plan = plans[i % len(plans)]
customer.subscription_end_date = timezone.now() + timedelta(days=customer.subscription_plan.duration_days)
customer.auto_renew = bool(i % 2)
customer.save()
print("Clientes creados o actualizados.")
def create_items():
customers = Customer.objects.all()
items = [
{'name': 'Llaves', 'description': 'Llaves de casa'},
{'name': 'Cartera', 'description': 'Cartera de cuero'},
{'name': 'Teléfono', 'description': 'Smartphone'},
{'name': 'Laptop', 'description': 'Computadora portátil'},
{'name': 'Tablet', 'description': 'Tablet Android'},
{'name': 'Reloj', 'description': 'Reloj de pulsera'},
]
for customer in customers:
max_items = customer.subscription_plan.max_items
for item_data in items[:max_items]:
item, created = Item.objects.update_or_create(
owner=customer,
name=item_data['name'],
defaults={'description': item_data['description']}
)
if created or not item.qr_code:
owner_uuid = str(customer.uuid)
url = f'{DOMAIN}/scan-qr/{owner_uuid}'
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
image_io = BytesIO()
img.save(image_io, 'PNG')
image_file = SimpleUploadedFile(f'{customer.username}/qr_codes/{item.name}.png',
image_io.getvalue(), content_type='image/png')
item.qr_code = image_file
item.save()
print("Items creados o actualizados respetando los límites del plan y con códigos QR generados.")
def create_notification_preferences():
customers = Customer.objects.all()
for customer in customers:
NotificationPreference.objects.update_or_create(
user=customer,
defaults={
'email_notifications': random.choice([True, False]),
'sms_notifications': random.choice([True, False]),
'push_notifications': random.choice([True, False]),
'whatsapp_notifications': random.choice([True, False]),
'notification_start_time': timezone.now().time().replace(hour=random.randint(0, 12),
minute=random.randint(0, 59)),
'notification_end_time': timezone.now().time().replace(hour=random.randint(13, 23),
minute=random.randint(0, 59)),
'show_contact_info_on_scan': random.choice([True, False]),
}
)
print("Preferencias de notificación creadas o actualizadas.")
def create_notifications():
customers = Customer.objects.all()
severity_choices = ['low', 'medium', 'high', 'urgent']
reason_choices = ['lost_item', 'found_item', 'system']
for customer in customers:
for _ in range(5):
Notification.objects.create(
user=customer,
severity=random.choice(severity_choices),
reason=random.choice(reason_choices),
message=f'Notificación de prueba para {customer.username}: {random.choice(reason_choices)} - {random.choice(severity_choices)}',
is_read=random.choice([True, False]),
)
print("Notificaciones creadas.")
if __name__ == '__main__':
create_subscription_plans()
create_customers()
create_items()
create_notification_preferences()
create_notifications()
print("Datos de prueba cargados exitosamente.")