legacy/pairent_backend/pairent_app/factories.py

147 lines
4.8 KiB
Python
Raw Normal View History

2023-05-05 13:03:33 +02:00
import factory
2023-05-07 08:35:50 +02:00
import random
2023-05-16 15:00:44 +02:00
import time, datetime, uuid
2023-05-07 08:35:50 +02:00
from django.db import models
2023-05-05 13:03:33 +02:00
2023-05-15 05:12:05 +02:00
from pairent_app.models import Apartament, User
2023-05-05 13:03:33 +02:00
2023-05-05 14:08:27 +02:00
factory.Faker.override_default_locale('ru_RU');
2023-05-16 15:00:44 +02:00
class UUID(factory.declarations.BaseDeclaration):
def evaluate(self, instance, step, extra):
return str(uuid.uuid4()).upper();
2023-05-15 05:12:05 +02:00
class OpenID_Address(factory.declarations.BaseDeclaration):
def evaluate(self, instance, step, extra):
return ''.join(random.choices(list('abcdef12345678990'), k=6)) + "@vvsu.ru";
2023-05-07 08:35:50 +02:00
class PhoneNumber(factory.declarations.BaseDeclaration):
def evaluate(self, instance, step, extra):
return '+799' + str(random.randint(0, 99999999)).zfill(8);
2023-05-15 05:12:05 +02:00
class CSV(factory.declarations.BaseDeclaration):
def __init__(self, min, max, count_min, count_max):
self.min = min; self.max = max;
2023-05-15 05:18:08 +02:00
self.count_min = count_min;
self.count_max = count_max;
super().__init__();
2023-05-07 08:35:50 +02:00
2023-05-15 05:12:05 +02:00
def evaluate(self, instance, step, extra):
len_ = random.randint(self.count_min, self.count_max);
out = [];
2023-05-15 05:18:08 +02:00
for i in range(len_): out.append(str(random.randint(self.min, self.max)));
2023-05-15 05:12:05 +02:00
return ','.join(out);
2023-05-07 08:35:50 +02:00
class Random(factory.declarations.BaseDeclaration):
2023-05-15 05:12:05 +02:00
def __init__(self, min, max = None, multiply = 1, list = None):
2023-05-07 08:35:50 +02:00
if (max == None):
self.min = 0;
self.max = min;
else:
self.min = min;
self.max = max;
self.multiply = multiply;
2023-05-15 05:12:05 +02:00
self.list = list;
2023-05-15 05:18:08 +02:00
super().__init__();
2023-05-07 08:35:50 +02:00
def evaluate(self, instance, step, extra):
2023-05-15 05:12:05 +02:00
if (type(self.list) == list):
return random.choice(self.list);
2023-05-07 08:35:50 +02:00
if (self.multiply != None):
return random.randint(self.min, self.max) * self.multiply;
return random.randint(self.min, self.max);
class Date(factory.declarations.BaseDeclaration):
2023-05-15 05:12:05 +02:00
2023-05-15 05:18:08 +02:00
def __init__(self, min_year = 2022, max_year = datetime.date.today().year):
2023-05-15 05:12:05 +02:00
self.min_year = min_year;
self.max_year = max_year;
2023-05-15 05:18:08 +02:00
super().__init__();
2023-05-15 05:12:05 +02:00
2023-05-07 08:35:50 +02:00
def evaluate(self, instance, step, extra):
2023-05-15 05:12:05 +02:00
return datetime.datetime(random.randint(self.min_year, self.max_year), random.randint(1,12), random.randint(1,27)).strftime("%G-%m-%d");
2023-05-07 08:35:50 +02:00
2023-05-05 13:03:33 +02:00
class ApartmentFactory(factory.django.DjangoModelFactory):
class Meta:
model = Apartament
# Base data
2023-05-07 08:35:50 +02:00
price = Random(3,10,5000);
lastPrice = Random(3,10,5000);
bail = Random(2,10,1000);
agencyCommission = Random(3,5,2500);
utilitiesPrice = Random(4,8,1000)
minimumLeasePeriod = Random(1,2,6);
address = factory.Faker('address', locale='ru_RU');
description = factory.Faker('sentence', locale='ru_RU');
perimetrs = Random(15,40);
rooms = Random(0,4);
ceilingHeight = Random(3,5);
floorHouse = Random(2,20);
floor = Random(2,20);
2023-05-05 13:03:33 +02:00
2023-05-07 08:35:50 +02:00
phoneNumber = PhoneNumber()
2023-05-05 13:03:33 +02:00
2023-05-07 08:35:50 +02:00
timeToBus = Random(1, 12, 5);
timeToTrain = Random(1, 12, 5);
2023-05-05 13:03:33 +02:00
# Apartment props
2023-05-07 08:35:50 +02:00
isFurniture = Random(0, 1);
isAnimal = Random(0, 1);
isTelevision = Random(0, 1);
isChild = Random(0, 1);
isInternet = Random(0, 1);
isBathroom = Random(0, 1);
isRefrigerator = Random(0, 1);
isWasher = Random(0, 1);
isAirConditioning = Random(0, 1);
isFreshRepair = Random(0, 1);
2023-05-05 13:03:33 +02:00
# House props
2023-05-07 08:35:50 +02:00
isElevator = Random(0, 1);
isParking = Random(0, 1);
isGarbageChute = Random(0, 1);
isConcierge = Random(0, 1);
2023-05-05 13:03:33 +02:00
2023-05-07 08:35:50 +02:00
# views = [] # factory.Faker('random_number')
2023-05-05 13:03:33 +02:00
2023-05-15 05:12:05 +02:00
dateCreate = Date()
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = User
2023-05-15 05:18:08 +02:00
favorites_apartments = CSV(1, 100, 1, 16);
2023-05-15 05:12:05 +02:00
comparison_apartments = CSV(1, 100, 1, 5);
name = factory.faker.Faker('name');
date_of_birth = Date(1980, 2006);
about_me = factory.faker.Faker('sentence');
gender = Random(0,0,0, ['f', 'm', 'n', '?'])
phone = PhoneNumber()
email = OpenID_Address()
telegram = '@uwu'
discord = '@uwu' # они поменяли формат
city = Random(0,0,0, ['Владивосток', 'Хабаровск', 'Урюпинск', 'Мухосранск', 'Нью-Йорк'])
2023-05-16 14:53:30 +02:00
role = 's'
openid_addr = OpenID_Address();
2023-05-16 15:00:44 +02:00
openid_id = UUID();
2023-05-17 06:17:40 +02:00
photo_provider = Random(0,0,0, ['VVSU', 'GRAVATAR']);
def fake(size: int):
"""
This method is designed to be conveniently used within a
django shell, like this:
>>> from pairent_app.factories import fake;
>>> fake(42);
"""
ApartmentFactory.create_batch(size);
UserFactory.create_batch(size);