def update_feeds(feed_ids, post_id): post_service: PostService = di.get(PostService) post = post_service.get(post_id) feed_service: FeedService = di.get(FeedService) for feed_id in feed_ids: feed_service.add_post(feed_id, post)
def seed_users(count): from app.database.repositories import UserRepo from app import di from app.database.models import User, Profile from app.database.repositories import ProfileRepo user_repo: UserRepo = di.get(UserRepo) profile_repo: ProfileRepo = di.get(ProfileRepo) count = int(count) time = datetime.now().strftime('%d%m%y%H%M%S') with click.progressbar(length=count, show_percent=True, show_eta=True, show_pos=True) as bar: for idx in bar: user = User(email=f"{idx}@{time}.com", password=generate_password_hash(fake.password())) user_repo.save(user) profile_repo.save( Profile(first_name=fake.first_name(), last_name=fake.last_name(), interests=fake.paragraph(), birth_date=fake.date_of_birth().strftime('%Y-%m-%d'), gender=choice(['male', 'female']), city_id=randint(1, 10), user_id=user.id)) db.commit()
def load_user(id): user_repo: UserRepo = di.get(UserRepo) profile_repo = di.get(ProfileRepo) user = user_repo.find_by_id(id) if id else None if user: user.profile = profile_repo.find_by_user_id(user.id) return user
def create_subscribers(app, friendship: Friendship): if friendship.status != FriendshipStatus.CONFIRMED: return subscribe: SubscribeManager = di.get(SubscribeManager) subscribe.add_subscribe(friendship.source_id, friendship.destination_id) subscribe.add_subscribe(friendship.destination_id, friendship.source_id)
def seed_cities(count): from app.database.repositories import CityRepo from app import di from app.database.models import City repo: CityRepo = di.get(CityRepo) for _ in range(int(count)): repo.save(City(name=fake.city())) db.commit()
def validate_email(form, field): # todo: Исправить user_repo: UserRepo = di.get(UserRepo) if user_repo.find_by_email(field.data) is not None: raise ValidationError('E-mail already registered')
def add_post(author_id, post_id): subscribe_manager: SubscribeManager = di.get(SubscribeManager) for items in subscribe_manager.get_subscribers_generator(author_id): update_feeds.delay(feed_ids=[item.subscriber for item in items], post_id=post_id)
def build_feed(feed_id): from app.feed.services import FeedService from app import di feed: FeedService = di.get(FeedService) feed.build(feed_id)