def index(self, page=1, order_by=ORDER_BY.NAME): clans_query = ClanPrototype._model_class.objects.all() clans_number = clans_query.count() page = int(page) - 1 url_builder = UrlBuilder(url('accounts:clans:'), arguments={'order_by': order_by.value}) index_filter = IndexFilter(url_builder=url_builder, values={'order_by': order_by.value}) paginator = Paginator(page, clans_number, clans_settings.CLANS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) clans_query = clans_query.order_by(order_by.order_field) clans_from, clans_to = paginator.page_borders(page) clans = [ ClanPrototype(clan_model) for clan_model in clans_query[clans_from:clans_to]] memberships = [MembershipPrototype(membership_model) for membership_model in MembershipPrototype._db_filter(clan__in=[clan.id for clan in clans], role=MEMBER_ROLE.LEADER)] accounts = {account_model.id: AccountPrototype(model=account_model) for account_model in AccountPrototype._db_filter(id__in=[membership.account_id for membership in memberships])} leaders = {membership.clan_id:accounts[membership.account_id] for membership in memberships} return self.template('clans/index.html', {'clans': clans, 'page_id': PAGE_ID.INDEX, 'paginator': paginator, 'index_filter': index_filter, 'leaders': leaders})
def _initiate_battle_with_bot(self, record): # search free bot # since now bots needed only for PvP, we can do simplified search battled_accounts_ids = Battle1x1Prototype._model_class.objects.all( ).values_list('account_id', flat=True) try: bot_account = AccountPrototype( model=AccountPrototype._model_class.objects.filter( is_bot=True).exclude(id__in=battled_accounts_ids)[0]) except IndexError: bot_account = None if bot_account is None: return [record], [] bot_hero = HeroPrototype.get_by_account_id(bot_account.id) self.logger.info('start battle between account %d and bot %d' % (record.account_id, bot_account.id)) # create battle for bot self.add_to_arena_queue(bot_hero.id) bot_record = self.arena_queue[bot_account.id] self._initiate_battle(record, bot_record, calculate_ratings=False) return [], [record, bot_record]
def portal_day_started(sender, **kwargs): # pylint: disable=W0613 accounts_query = AccountPrototype.live_query().filter( active_end_at__gt=datetime.datetime.now(), ban_game_end_at__lt=datetime.datetime.now(), ban_forum_end_at__lt=datetime.datetime.now(), premium_end_at__lt=datetime.datetime.now()) accounts_number = accounts_query.count() if accounts_number < 1: return account_model = accounts_query[random.randint(0, accounts_number - 1)] account = AccountPrototype(model=account_model) settings[portal_settings.SETTINGS_ACCOUNT_OF_THE_DAY_KEY] = str(account.id) environment.workers.accounts_manager.cmd_run_account_method( account_id=account.id, method_name=AccountPrototype.prolong_premium.__name__, data={'days': portal_settings.PREMIUM_DAYS_FOR_HERO_OF_THE_DAY}) message = ''' Поздравляем! Ваш герой выбран героем дня и Вы получаете %(days)d дней подписки! ''' % { 'days': portal_settings.PREMIUM_DAYS_FOR_HERO_OF_THE_DAY } MessagePrototype.create(get_system_user(), account, message)
def thin_out_accounts(number, prolong_active_to): from . import models from the_tale.game.bills import models as bills_models from the_tale.blogs import models as blogs_models from the_tale.forum import models as forum_models restricted_accounts = set() restricted_accounts |= set( models.RandomPremiumRequest.objects.values_list('initiator', flat=True)) restricted_accounts |= set( models.RandomPremiumRequest.objects.values_list('receiver', flat=True)) restricted_accounts |= set( Account.objects.exclude(clan_id=None).values_list('id', flat=True)) restricted_accounts |= set( bills_models.Bill.objects.values_list('owner_id', flat=True)) restricted_accounts |= set( blogs_models.Post.objects.values_list('author_id', flat=True)) restricted_accounts |= set( blogs_models.Post.objects.values_list('moderator_id', flat=True)) restricted_accounts |= set( forum_models.Post.objects.values_list('author_id', flat=True)) restricted_accounts |= set( forum_models.Thread.objects.values_list('author_id', flat=True)) for account_model in Account.objects.all().order_by('created_at')[number:]: if account_model.id not in restricted_accounts: remove_account(AccountPrototype(account_model), force=True) Account.objects.all().update(active_end_at=datetime.datetime.now() + datetime.timedelta(seconds=prolong_active_to))
def for_clan(self): self.clan = self.clan_info.clan requests = MembershipRequestPrototype.get_for_clan(self.clan_info.clan_id) accounts = {model.id: AccountPrototype(model) for model in AccountPrototype._db_filter(id__in=[request.account_id for request in requests])} return self.template('clans/membership/for_clan.html', {'requests': requests, 'page_id': PAGE_ID.FOR_CLAN, 'accounts': accounts})
def get_candidates_for(cls, account): friendship_query = cls._model_class.objects.filter( friend_2_id=account.id, is_confirmed=False) accounts_ids = friendship_query.values_list('friend_1_id', flat=True) return [ AccountPrototype(model=model) for model in Account.objects.filter(id__in=accounts_ids) ]
def block_expired_accounts(): expired_before = datetime.datetime.now() - datetime.timedelta( seconds=accounts_settings.FAST_ACCOUNT_EXPIRED_TIME) for account_model in Account.objects.filter(is_fast=True, created_at__lt=expired_before): remove_account(AccountPrototype(account_model))
def for_account(self): requests = MembershipRequestPrototype.get_for_account(self.account.id) accounts = {model.id: AccountPrototype(model) for model in AccountPrototype._db_filter(id__in=[request.account_id for request in requests] + [request.initiator_id for request in requests])} clans = {model.id: ClanPrototype(model) for model in ClanPrototype._db_filter(id__in=[request.clan_id for request in requests])} return self.template('clans/membership/for_account.html', {'requests': requests, 'accounts': accounts, 'clans': clans, 'page_id': PAGE_ID.FOR_ACCOUNT,})
def get_achievements_source_iterator(self, achievement): from the_tale.accounts.prototypes import AccountPrototype from the_tale.game.heroes.prototypes import HeroPrototype if achievement.type.source.is_ACCOUNT: return (AccountPrototype(model=account_model) for account_model in AccountPrototype._db_all()) if achievement.type.source.is_GAME_OBJECT: return (HeroPrototype(model=hero_model) for hero_model in HeroPrototype._db_all())
def get_achievements_source_iterator(self, achievement): from the_tale.accounts.prototypes import AccountPrototype if achievement.type.source.is_ACCOUNT: return (AccountPrototype(model=account_model) for account_model in AccountPrototype._db_all()) if achievement.type.source.is_GAME_OBJECT: return ( heroes_logic.load_hero(hero_model=hero_model) for hero_model in heroes_models.Hero.objects.all().iterator())
def recalculate_accounts_might(): for account_model in AccountPrototype.live_query(): account = AccountPrototype(model=account_model) new_might = calculate_might(account) if account.might != new_might: account.set_might(new_might) account.cmd_update_hero() recalculate_folclor_rating()
def get_friends_for(cls, account): friendship_query = cls._model_class.objects.filter( models.Q(friend_1_id=account.id) | models.Q(friend_2_id=account.id), is_confirmed=True) values = list( friendship_query.values_list('friend_1_id', 'friend_2_id')) if not values: return [] friends_1_ids, friends_2_ids = zip(*values) accounts_ids = (set(friends_1_ids) | set(friends_2_ids)) - set( [account.id]) return [ AccountPrototype(model=model) for model in Account.objects.filter(id__in=accounts_ids) ]
def process(self): from the_tale.news import logic as news_logic news = news_logic.load_news(self.news_id) if news is None: return True accounts = (AccountPrototype(model=account_model) for account_model in AccountPrototype._db_filter(news_subscription=True).iterator()) subject = '«Сказка»::Новости: %s' % news.caption context = {'news': news} html_content = jinja2.render(self.EMAIL_HTML_TEMPLATE, context) text_content = jinja2.render(self.EMAIL_TEXT_TEMPLATE, context) return logic.send_mail(accounts, subject, text_content, html_content)
def portal_day_started(sender, **kwargs): accounts_query = AccountPrototype.live_query().filter( active_end_at__gt=datetime.datetime.now(), ban_game_end_at__lt=datetime.datetime.now(), ban_forum_end_at__lt=datetime.datetime.now(), premium_end_at__lt=datetime.datetime.now()) accounts_number = accounts_query.count() if accounts_number < 1: return account = None for i in range(1000): account_model = accounts_query[random.randint(0, accounts_number - 1)] account = AccountPrototype(model=account_model) # explicity check for premium, since infinit subscribers does not filtered by previouse query if not account.is_premium: break else: return # if not premium account does not found settings[portal_settings.SETTINGS_ACCOUNT_OF_THE_DAY_KEY] = str(account.id) environment.workers.accounts_manager.cmd_run_account_method( account_id=account.id, method_name=AccountPrototype.prolong_premium.__name__, data={'days': portal_settings.PREMIUM_DAYS_FOR_HERO_OF_THE_DAY}) message = ''' Поздравляем! Ваш герой выбран героем дня и Вы получаете %(days)d дней подписки! ''' % { 'days': portal_settings.PREMIUM_DAYS_FOR_HERO_OF_THE_DAY } pm_tt_api.send_message(sender_id=accounts_logic.get_system_user_id(), recipients_ids=[account.id], body=message, async=True)
def __init__(self, request, *args, **kwargs): super(Resource, self).__init__(request, *args, **kwargs) self.account = self.request.user if self.account.is_authenticated(): self.account = AccountPrototype(model=self.account)
def voter(self): return AccountPrototype(self._model.voter) @classmethod
def author(self): if self._model.author: return AccountPrototype(self._model.author) return None
def friend_2(self): return AccountPrototype(model=self._model.friend_2)
def last_poster(self): return AccountPrototype(self._model.last_poster) if self._model.last_poster else None @lazy_property
def sender(self): return AccountPrototype(self._model.sender)
def owner(self): return AccountPrototype(self._model.owner)
def author(self): return AccountPrototype(self._model.author) if self._model.author else None @property
def get_accounts_for_thread(cls, thread): accounts = Account.objects.filter(subscription__thread_id=thread.id) return [AccountPrototype(model=account) for account in accounts]
def owner(self): if not hasattr(self, '_owner'): self._owner = AccountPrototype(self._model.owner) return self._owner
def get_accounts_for_subcategory(cls, subcategory): accounts = Account.objects.filter(subscription__subcategory_id=subcategory.id) return [AccountPrototype(model=account) for account in accounts]
def account(self): return AccountPrototype(model=self._model.account)
def recipient(self): return AccountPrototype(self._model.recipient)