def index(self, page=None, place=None): records_query = Record.objects.all() if place is not None: records_query = records_query.filter(actors__place_id=place.id) url_builder = UrlBuilder(reverse('game:chronicle:'), arguments={'place': place.id if place else None}) index_filter = IndexFilter(url_builder=url_builder, values={'place': place.id if place else None}) records_count = records_query.count() if page is None: page = Paginator.get_page_numbers(records_count, chronicle_settings.RECORDS_ON_PAGE) if page == 0: page = 1 page = int(page) - 1 paginator = Paginator(page, records_count, chronicle_settings.RECORDS_ON_PAGE, url_builder, inverse=True) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) record_from, record_to = paginator.page_borders(page) records = [ RecordPrototype(record) for record in records_query.select_related().order_by('created_at', 'created_at_turn')[record_from:record_to]] records = list(reversed(records)) return self.template('chronicle/index.html', {'records': records, 'place': place, 'paginator': paginator, 'index_filter': index_filter, 'url_builder': url_builder} )
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 index(self, page=1, author_id=None, order_by=ORDER_BY.CREATED_AT): posts_query = models.Post.objects.filter(state__in=[relations.POST_STATE.NOT_MODERATED, relations.POST_STATE.ACCEPTED]) is_filtering = False author_account = None if author_id is not None: author_id = int(author_id) author_account = AccountPrototype.get_by_id(author_id) if author_account: posts_query = posts_query.filter(author_id=author_account.id) is_filtering = True else: posts_query = models.Post.objects.none() if order_by is not None: if order_by == ORDER_BY.ALPHABET: posts_query = posts_query.order_by('caption') elif order_by == ORDER_BY.CREATED_AT: posts_query = posts_query.order_by('-created_at') elif order_by == ORDER_BY.RATING: posts_query = posts_query.order_by('-votes') else: order_by = ORDER_BY.CREATED_AT posts_query = posts_query.order_by('-created_at') url_builder = UrlBuilder(reverse('blogs:posts:'), arguments={'author_id': author_id, 'order_by': order_by}) posts_count = posts_query.count() page = int(page) - 1 paginator = Paginator(page, posts_count, conf.settings.POSTS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) post_from, post_to = paginator.page_borders(page) posts = [ prototypes.PostPrototype(post) for post in posts_query.select_related()[post_from:post_to]] votes = {} if self.account.is_authenticated(): votes = dict( (vote.post_id, prototypes.VotePrototype(vote)) for vote in models.Vote.objects.filter(post_id__in=[post.id for post in posts], voter=self.account._model) ) return self.template('blogs/index.html', {'posts': posts, 'page_type': 'index', 'votes': votes, 'order_by': order_by, 'ORDER_BY': ORDER_BY, 'is_filtering': is_filtering, 'current_page_number': page, 'author_account': author_account, 'paginator': paginator, 'url_builder': url_builder} )
def get_subcategory(self, page=1): threads_query = Thread.objects.filter(subcategory=self.subcategory._model) url_builder = UrlBuilder(reverse('forum:subcategories:show', args=[self.subcategory.id]), arguments={'page': page}) page -= 1 paginator = Paginator(page, threads_query.count(), forum_settings.THREADS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) thread_from, thread_to = paginator.page_borders(page) threads = ThreadPrototype.from_query(threads_query.select_related().order_by('-important', '-updated_at')[thread_from:thread_to]) important_threads = sorted([t for t in threads if t.important], key=lambda t: t.caption) threads = [t for t in threads if not t.important] read_state = ReadState(account=self.account) if self.account.is_authenticated: SubCategoryReadInfoPrototype.read_subcategory(subcategory=self.subcategory, account=self.account) return self.template('forum/subcategory.html', {'category': self.category, 'subcategory': self.subcategory, 'can_create_thread': can_create_thread(self.account, self.subcategory), 'paginator': paginator, 'can_subscribe': self.account.is_authenticated and not self.account.is_fast, 'has_subscription': SubscriptionPrototype.has_subscription(self.account, subcategory=self.subcategory), 'threads': threads, 'important_threads': important_threads, 'read_state': read_state } )
def get_subcategory(self, page=1): threads_query = Thread.objects.filter( subcategory=self.subcategory._model) url_builder = UrlBuilder(reverse('forum:subcategories:show', args=[self.subcategory.id]), arguments={'page': page}) page -= 1 paginator = Paginator(page, threads_query.count(), forum_settings.THREADS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) thread_from, thread_to = paginator.page_borders(page) threads = ThreadPrototype.from_query( threads_query.select_related().order_by( '-important', '-updated_at')[thread_from:thread_to]) important_threads = sorted(filter(lambda t: t.important, threads), key=lambda t: t.caption) threads = filter(lambda t: not t.important, threads) read_state = ReadState(account=self.account) if self.account.is_authenticated(): SubCategoryReadInfoPrototype.read_subcategory( subcategory=self.subcategory, account=self.account) return self.template( 'forum/subcategory.html', { 'category': self.category, 'subcategory': self.subcategory, 'can_create_thread': can_create_thread(self.account, self.subcategory), 'paginator': paginator, 'can_subscribe': self.account.is_authenticated() and not self.account.is_fast, 'has_subscription': SubscriptionPrototype.has_subscription( self.account, subcategory=self.subcategory), 'threads': threads, 'important_threads': important_threads, 'read_state': read_state })
def index(self, author=None, page=1, participant=None): threads_query = ThreadPrototype.threads_visible_to_account_query( self.account if self.account.is_authenticated( ) else None).order_by('-updated_at') is_filtering = False if author is not None: threads_query = threads_query.filter(author_id=author.id) is_filtering = True if participant is not None: threads_query = threads_query.filter( post__author__id=participant.id).distinct() is_filtering = True url_builder = UrlBuilder(reverse('forum:threads:'), arguments={ 'author': author.id if author else None, 'participant': participant.id if participant else None, 'page': page }) page -= 1 paginator = Paginator(page, threads_query.count(), forum_settings.THREADS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) thread_from, thread_to = paginator.page_borders(page) threads = list( ThreadPrototype(thread_model) for thread_model in threads_query.select_related().order_by( '-updated_at')[thread_from:thread_to]) return self.template( 'forum/threads_list.html', { 'is_filtering': is_filtering, 'pages_count': range(paginator.pages_count), 'current_page_number': page, 'author_account': author, 'participant_account': participant, 'paginator': paginator, 'threads': threads, 'read_state': ReadState(account=self.account) })
def index(context): accounts_query = AccountPrototype.live_query() if context.prefix: accounts_query = accounts_query.filter( nick__istartswith=context.prefix) accounts_count = accounts_query.count() url_builder = UrlBuilder(reverse('accounts:'), arguments={ 'page': context.page, 'prefix': context.prefix }) paginator = Paginator(context.page, accounts_count, conf.accounts_settings.ACCOUNTS_ON_PAGE, url_builder) if paginator.wrong_page_number: return dext_views.Redirect(paginator.last_page_url, permanent=False) account_from, account_to = paginator.page_borders(context.page) accounts_models = accounts_query.select_related().order_by( 'nick')[account_from:account_to] accounts = [AccountPrototype(model) for model in accounts_models] accounts_ids = [model.id for model in accounts_models] clans_ids = [model.clan_id for model in accounts_models] heroes = dict( (model.account_id, heroes_logic.load_hero(hero_model=model)) for model in Hero.objects.filter(account_id__in=accounts_ids)) clans = {clan.id: clan for clan in ClanPrototype.get_list_by_id(clans_ids)} return dext_views.Page('accounts/index.html', content={ 'heroes': heroes, 'prefix': context.prefix, 'accounts': accounts, 'clans': clans, 'resource': context.resource, 'current_page_number': context.page, 'paginator': paginator })
def index(context): context.account.reset_new_messages_number() query = models.Message.objects.filter(recipient_id=context.account.id, hide_from_recipient=False) senders_ids = list(set(query.values_list('sender_id', flat=True).order_by('sender').distinct())) senders = sorted(accounts_prototypes.AccountPrototype.get_list_by_id(senders_ids), key=lambda account: account.nick) if context.sender is not None: query = query.filter(sender_id=context.sender.id) if context.filter is not None: query = query.filter(text__icontains=context.filter) class Filter(list_filter.ListFilter): ELEMENTS = [list_filter.reset_element(), list_filter.filter_element('поиск:', attribute='filter', default_value=None), list_filter.choice_element('отправитель:', attribute='sender', choices=[(None, 'все')] + [(account.id, account.nick) for account in senders] ), list_filter.static_element('количество:', attribute='count', default_value=0) ] url_builder = UrlBuilder(url('accounts:messages:'), arguments={'page': context.page, 'sender': context.sender.id if context.sender is not None else None, 'filter': context.filter}) messages_count = query.count() index_filter = Filter(url_builder=url_builder, values={'sender': context.sender.id if context.sender is not None else None, 'filter': context.filter, 'count': messages_count}) # page = int(context.page) - 1 paginator = Paginator(context.page, messages_count, conf.settings.MESSAGES_ON_PAGE, url_builder) if paginator.wrong_page_number: return dext_views.Redirect(paginator.last_page_url, permanent=False) message_from, message_to = paginator.page_borders(context.page) messages = [ prototypes.MessagePrototype(message_model) for message_model in query.order_by('-created_at')[message_from:message_to]] return dext_views.Page('personal_messages/index.html', content= {'messages': messages, 'paginator': paginator, 'incoming': True, 'index_filter': index_filter, 'resource': context.resource})
def index(context): context.account.reset_new_messages_number() query = models.Message.objects.filter(recipient_id=context.account.id, hide_from_recipient=False) senders_ids = list(set(query.values_list('sender_id', flat=True).order_by('sender').distinct())) senders = sorted(accounts_prototypes.AccountPrototype.get_list_by_id(senders_ids), key=lambda account: account.nick) if context.sender is not None: query = query.filter(sender_id=context.sender.id) if context.filter is not None: query = query.filter(text__icontains=context.filter) class Filter(list_filter.ListFilter): ELEMENTS = [list_filter.reset_element(), list_filter.filter_element(u'поиск:', attribute='filter', default_value=None), list_filter.choice_element(u'отправитель:', attribute='sender', choices=[(None, u'все')] + [(account.id, account.nick) for account in senders] ), list_filter.static_element(u'количество:', attribute='count', default_value=0) ] url_builder = UrlBuilder(url('accounts:messages:'), arguments={'page': context.page, 'sender': context.sender.id if context.sender is not None else None, 'filter': context.filter}) messages_count = query.count() index_filter = Filter(url_builder=url_builder, values={'sender': context.sender.id if context.sender is not None else None, 'filter': context.filter, 'count': messages_count}) # page = int(context.page) - 1 paginator = Paginator(context.page, messages_count, conf.settings.MESSAGES_ON_PAGE, url_builder) if paginator.wrong_page_number: return dext_views.Redirect(paginator.last_page_url, permanent=False) message_from, message_to = paginator.page_borders(context.page) messages = [ prototypes.MessagePrototype(message_model) for message_model in query.order_by('-created_at')[message_from:message_to]] return dext_views.Page('personal_messages/index.html', content= {'messages': messages, 'paginator': paginator, 'incoming': True, 'index_filter': index_filter, 'resource': context.resource})
def get_post_url(self, post): thread_posts_ids = list( PostPrototype._db_filter( thread_id=post.thread_id).order_by('created_at').values_list( 'id', flat=True)) page = Paginator.get_page_numbers( thread_posts_ids.index(post.id) + 1, forum_settings.POSTS_ON_PAGE) return url('forum:threads:show', post.thread_id, page=page) + ('#m%d' % post.id)
def index(context): accounts_query = AccountPrototype.live_query() if context.prefix: accounts_query = accounts_query.filter(nick__istartswith=context.prefix) accounts_count = accounts_query.count() url_builder = UrlBuilder(reverse("accounts:"), arguments={"page": context.page, "prefix": context.prefix}) paginator = Paginator(context.page, accounts_count, conf.accounts_settings.ACCOUNTS_ON_PAGE, url_builder) if paginator.wrong_page_number: return dext_views.Redirect(paginator.last_page_url, permanent=False) account_from, account_to = paginator.page_borders(context.page) accounts_models = accounts_query.select_related().order_by("nick")[account_from:account_to] accounts = [AccountPrototype(model) for model in accounts_models] accounts_ids = [model.id for model in accounts_models] clans_ids = [model.clan_id for model in accounts_models] heroes = dict( (model.account_id, heroes_logic.load_hero(hero_model=model)) for model in Hero.objects.filter(account_id__in=accounts_ids) ) clans = {clan.id: clan for clan in ClanPrototype.get_list_by_id(clans_ids)} return dext_views.Page( "accounts/index.html", content={ "heroes": heroes, "prefix": context.prefix, "accounts": accounts, "clans": clans, "resource": context.resource, "current_page_number": context.page, "paginator": paginator, }, )
class ThreadPageData(object): def __init__(self): pass def initialize(self, account, thread, page, inline=False): from the_tale.game.heroes.prototypes import HeroPrototype self.account = account self.thread = thread url_builder = UrlBuilder(reverse('forum:threads:show', args=[self.thread.id]), arguments={'page': page}) page -= 1 self.paginator = Paginator(page, thread.posts_count+1, forum_settings.POSTS_ON_PAGE, url_builder) if self.paginator.wrong_page_number: return False post_from, post_to = self.paginator.page_borders(page) self.post_from = post_from self.posts = [PostPrototype(post_model) for post_model in Post.objects.filter(thread=self.thread._model).order_by('created_at')[post_from:post_to]] self.authors = {author.id:author for author in AccountPrototype.get_list_by_id([post.author_id for post in self.posts])} self.game_objects = {game_object.account_id:game_object for game_object in HeroPrototype.get_list_by_id([post.author_id for post in self.posts])} pages_on_page_slice = self.posts if post_from == 0: pages_on_page_slice = pages_on_page_slice[1:] self.has_post_on_page = any([post.author.id == self.account.id for post in pages_on_page_slice]) self.new_post_form = forms.NewPostForm() self.start_posts_from = page * forum_settings.POSTS_ON_PAGE self.inline = inline self.can_delete_posts = can_delete_posts(self.account, self.thread) self.can_change_posts = can_change_posts(self.account) self.can_delete_thread = not self.inline and can_delete_thread(self.account) self.can_change_thread = not self.inline and can_change_thread(self.account, self.thread) self.ignore_first_post = (self.inline and self.paginator.current_page_number==0) self.can_post = self.account.is_authenticated() and not self.account.is_fast self.no_posts = (len(self.posts) == 0) or (self.ignore_first_post and len(self.posts) == 1) self.can_subscribe = self.account.is_authenticated() and not self.account.is_fast self.has_subscription = SubscriptionPrototype.has_subscription(self.account, self.thread) return True
def index(context): url_builder = UrlBuilder(url('news:'), arguments={'page': context.page}) news_count = models.News.objects.all().count() paginator = Paginator(context.page, news_count, conf.settings.NEWS_ON_PAGE, url_builder) if paginator.wrong_page_number: return dext_views.Redirect(paginator.last_page_url) news_from, news_to = paginator.page_borders(context.page) news = logic.load_news_from_query(models.News.objects.all().order_by('-created_at')[news_from:news_to]) return dext_views.Page('news/index.html', content={'news': news, 'paginator': paginator, 'resource': context.resource} )
def index(self, author=None, page=1, participant=None): threads_query = ThreadPrototype.threads_visible_to_account_query(self.account if self.account.is_authenticated else None).order_by('-updated_at') is_filtering = False if author is not None: threads_query = threads_query.filter(author_id=author.id) is_filtering = True if participant is not None: threads_query = threads_query.filter(post__author__id=participant.id).distinct() is_filtering = True url_builder = UrlBuilder(reverse('forum:threads:'), arguments={'author': author.id if author else None, 'participant': participant.id if participant else None, 'page': page}) page -= 1 paginator = Paginator(page, threads_query.count(), forum_settings.THREADS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) thread_from, thread_to = paginator.page_borders(page) threads = list(ThreadPrototype(thread_model) for thread_model in threads_query.select_related().order_by('-updated_at')[thread_from:thread_to]) return self.template('forum/threads_list.html', {'is_filtering': is_filtering, 'pages_count': list(range(paginator.pages_count)), 'current_page_number': page, 'author_account': author, 'participant_account': participant, 'paginator': paginator, 'threads': threads, 'read_state': ReadState(account=self.account)} )
def index(context): accounts_query = AccountPrototype.live_query() if context.prefix: accounts_query = accounts_query.filter(nick__istartswith=context.prefix) accounts_count = accounts_query.count() url_builder = UrlBuilder(reverse('accounts:'), arguments={'page': context.page, 'prefix': context.prefix}) paginator = Paginator(context.page, accounts_count, conf.accounts_settings.ACCOUNTS_ON_PAGE, url_builder) if paginator.wrong_page_number: return dext_views.Redirect(paginator.last_page_url, permanent=False) account_from, account_to = paginator.page_borders(context.page) accounts_models = accounts_query.select_related().order_by('nick')[account_from:account_to] accounts = [AccountPrototype(model) for model in accounts_models] accounts_ids = [ model.id for model in accounts_models] clans_ids = [ model.clan_id for model in accounts_models] heroes = dict( (model.account_id, HeroPrototype(model=model)) for model in Hero.objects.filter(account_id__in=accounts_ids)) clans = {clan.id:clan for clan in ClanPrototype.get_list_by_id(clans_ids)} return dext_views.Page('accounts/index.html', content={'heroes': heroes, 'prefix': context.prefix, 'accounts': accounts, 'clans': clans, 'resource': context.resource, 'current_page_number': context.page, 'paginator': paginator } )
def index(context): url_builder = UrlBuilder(url('news:'), arguments={'page': context.page}) news_count = models.News.objects.all().count() paginator = Paginator(context.page, news_count, conf.settings.NEWS_ON_PAGE, url_builder) if paginator.wrong_page_number: return dext_views.Redirect(paginator.last_page_url) news_from, news_to = paginator.page_borders(context.page) news = logic.load_news_from_query( models.News.objects.all().order_by('-created_at')[news_from:news_to]) return dext_views.Page('news/index.html', content={ 'news': news, 'paginator': paginator, 'resource': context.resource })
def conversation(context): contacts_ids = tt_api.get_contacts(context.account.id) messages_count, messages = tt_api.get_conversation(context.account.id, context.contact.id, text=context.filter, offset=context.page*conf.settings.MESSAGES_ON_PAGE, limit=conf.settings.MESSAGES_ON_PAGE) class Filter(list_filter.ListFilter): ELEMENTS = [list_filter.reset_element(), list_filter.filter_element('поиск:', attribute='filter', default_value=None), list_filter.static_element('количество:', attribute='count', default_value=0) ] url_builder = UrlBuilder(url('accounts:messages:conversation'), arguments={'page': context.page, 'contact': context.contact.id, 'filter': context.filter}) index_filter = Filter(url_builder=url_builder, values={'contact': context.contact.id, 'filter': context.filter, 'count': messages_count}) paginator = Paginator(context.page, messages_count, conf.settings.MESSAGES_ON_PAGE, url_builder) if paginator.wrong_page_number: return dext_views.Redirect(paginator.last_page_url, permanent=False) accounts_ids = set(contacts_ids) for message in messages: accounts_ids.add(message.sender_id) accounts_ids.update(message.recipients_ids) accounts = {account.id: account for account in accounts_prototypes.AccountPrototype.get_list_by_id(list(accounts_ids))} contacts = [accounts[contact_id] for contact_id in contacts_ids] contacts.sort(key=lambda account: account.nick_verbose) return dext_views.Page('personal_messages/index.html', content= {'messages': messages, 'paginator': paginator, 'page': 'contacts', 'contacts': contacts, 'accounts': accounts, 'master_account': context.account, 'index_filter': index_filter, 'contact': context.contact, 'resource': context.resource})
def index( self, page=1, state=None, type=None, filter=None, contributor=None, order_by=relations.INDEX_ORDER_BY.UPDATED_AT ): words_query = prototypes.WordPrototype._db_all().order_by("normal_form") if contributor is not None: entities_ids = prototypes.ContributionPrototype._db_filter( type=relations.CONTRIBUTION_TYPE.WORD, account_id=contributor.id ).values_list("entity_id", flat=True) words_query = words_query.filter(models.Q(id__in=entities_ids) | models.Q(author_id=contributor.id)) if state: words_query = words_query.filter(state=state) if type: words_query = words_query.filter(type=type.utg_type) if filter: words_query = words_query.filter(forms__icontains=filter.lower()) if order_by.is_UPDATED_AT: words_query = words_query.order_by("-updated_at") elif order_by.is_TEXT: words_query = words_query.order_by("normal_form") words_count = words_query.count() url_builder = UrlBuilder( reverse("linguistics:words:"), arguments={ "state": state.value if state else None, "type": type.value if type else None, "contributor": contributor.id if contributor else None, "order_by": order_by.value, "filter": filter, }, ) index_filter = WordsIndexFilter( url_builder=url_builder, values={ "state": state.value if state else None, "type": type.value if type else None, "filter": filter, "contributor": contributor.nick if contributor else None, "order_by": order_by.value, "count": words_count, }, ) page = int(page) - 1 paginator = Paginator(page, words_count, linguistics_settings.WORDS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) words_from, words_to = paginator.page_borders(page) words = prototypes.WordPrototype.from_query(words_query[words_from:words_to]) authors = { account.id: account for account in AccountPrototype.from_query( AccountPrototype.get_list_by_id([word.author_id for word in words]) ) } clans = { clan.id: clan for clan in ClanPrototype.from_query( ClanPrototype.get_list_by_id([author.clan_id for author in authors.itervalues()]) ) } return self.template( "linguistics/words/index.html", { "words": words, "page_type": "dictionary", "paginator": paginator, "authors": authors, "clans": clans, "ALLOWED_WORD_TYPE": relations.ALLOWED_WORD_TYPE, "index_filter": index_filter, }, )
def index(self, key=None, state=None, filter=None, restriction=None, errors_status=None, page=1, contributor=None, order_by=relations.INDEX_ORDER_BY.UPDATED_AT): templates_query = prototypes.TemplatePrototype._db_all().order_by( 'raw_template') if contributor is not None: entities_ids = prototypes.ContributionPrototype._db_filter( type=relations.CONTRIBUTION_TYPE.TEMPLATE, account_id=contributor.id).values_list('entity_id', flat=True) templates_query = templates_query.filter( models.Q(id__in=entities_ids) | models.Q(author_id=contributor.id)) if key: templates_query = templates_query.filter(key=key) if state: templates_query = templates_query.filter(state=state) if errors_status: templates_query = templates_query.filter( errors_status=errors_status) if restriction: templates_query = templates_query.filter( templaterestriction__restriction_id=restriction.id) if filter: templates_query = templates_query.filter( raw_template__icontains=filter) if order_by.is_UPDATED_AT: templates_query = templates_query.order_by('-updated_at') elif order_by.is_TEXT: templates_query = templates_query.order_by('raw_template') page = int(page) - 1 templates_count = templates_query.count() url_builder = UrlBuilder( reverse('linguistics:templates:'), arguments={ 'state': state.value if state else None, 'errors_status': errors_status.value if errors_status else None, 'contributor': contributor.id if contributor else None, 'order_by': order_by.value, 'filter': filter, 'restriction': restriction.id if restriction is not None else None, 'key': key.value if key is not None else None }) index_filter = TemplatesIndexFilter( url_builder=url_builder, values={ 'state': state.value if state else None, 'errors_status': errors_status.value if errors_status else None, 'contributor': contributor.nick if contributor else None, 'order_by': order_by.value, 'filter': filter, 'restriction': restriction.id if restriction is not None else None, 'key': key.value if key is not None else None, 'count': templates_query.count() }) paginator = Paginator(page, templates_count, linguistics_settings.TEMPLATES_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) template_from, template_to = paginator.page_borders(page) templates = prototypes.TemplatePrototype.from_query( templates_query[template_from:template_to]) authors = { account.id: account for account in AccountPrototype.from_query( AccountPrototype.get_list_by_id( [template.author_id for template in templates])) } clans = { clan.id: clan for clan in ClanPrototype.from_query( ClanPrototype.get_list_by_id( [author.clan_id for author in authors.values()])) } return self.template( 'linguistics/templates/index.html', { 'key': key, 'templates': templates, 'index_filter': index_filter, 'page_type': 'all-templates', 'paginator': paginator, 'authors': authors, 'clans': clans, 'LEXICON_KEY': keys.LEXICON_KEY })
def index(self, page=1, state=None, type=None, filter=None, contributor=None, order_by=relations.INDEX_ORDER_BY.UPDATED_AT): words_query = prototypes.WordPrototype._db_all().order_by( 'normal_form') if contributor is not None: entities_ids = prototypes.ContributionPrototype._db_filter( type=relations.CONTRIBUTION_TYPE.WORD, account_id=contributor.id).values_list('entity_id', flat=True) words_query = words_query.filter( models.Q(id__in=entities_ids) | models.Q(author_id=contributor.id)) if state: words_query = words_query.filter(state=state) if type: words_query = words_query.filter(type=type.utg_type) if filter: words_query = words_query.filter(forms__icontains=filter.lower()) if order_by.is_UPDATED_AT: words_query = words_query.order_by('-updated_at') elif order_by.is_TEXT: words_query = words_query.order_by('normal_form') words_count = words_query.count() url_builder = UrlBuilder(reverse('linguistics:words:'), arguments={ 'state': state.value if state else None, 'type': type.value if type else None, 'contributor': contributor.id if contributor else None, 'order_by': order_by.value, 'filter': filter }) index_filter = WordsIndexFilter( url_builder=url_builder, values={ 'state': state.value if state else None, 'type': type.value if type else None, 'filter': filter, 'contributor': contributor.nick if contributor else None, 'order_by': order_by.value, 'count': words_count }) page = int(page) - 1 paginator = Paginator(page, words_count, linguistics_settings.WORDS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) words_from, words_to = paginator.page_borders(page) words = prototypes.WordPrototype.from_query( words_query[words_from:words_to]) authors = { account.id: account for account in AccountPrototype.from_query( AccountPrototype.get_list_by_id( [word.author_id for word in words])) } clans = { clan.id: clan for clan in ClanPrototype.from_query( ClanPrototype.get_list_by_id( [author.clan_id for author in authors.values()])) } return self.template( 'linguistics/words/index.html', { 'words': words, 'page_type': 'dictionary', 'paginator': paginator, 'authors': authors, 'clans': clans, 'ALLOWED_WORD_TYPE': relations.ALLOWED_WORD_TYPE, 'index_filter': index_filter })
def index(context): context.account.reset_new_messages_number() query = models.Message.objects.filter(recipient_id=context.account.id, hide_from_recipient=False) senders_ids = list(set(query.values_list("sender_id", flat=True).order_by("sender").distinct())) senders = sorted(accounts_prototypes.AccountPrototype.get_list_by_id(senders_ids), key=lambda account: account.nick) if context.sender is not None: query = query.filter(sender_id=context.sender.id) if context.filter is not None: query = query.filter(text__icontains=context.filter) class Filter(list_filter.ListFilter): ELEMENTS = [ list_filter.reset_element(), list_filter.filter_element(u"поиск:", attribute="filter", default_value=None), list_filter.choice_element( u"отправитель:", attribute="sender", choices=[(None, u"все")] + [(account.id, account.nick) for account in senders], ), list_filter.static_element(u"количество:", attribute="count", default_value=0), ] url_builder = UrlBuilder( url("accounts:messages:"), arguments={ "page": context.page, "sender": context.sender.id if context.sender is not None else None, "filter": context.filter, }, ) messages_count = query.count() index_filter = Filter( url_builder=url_builder, values={ "sender": context.sender.id if context.sender is not None else None, "filter": context.filter, "count": messages_count, }, ) # page = int(context.page) - 1 paginator = Paginator(context.page, messages_count, conf.settings.MESSAGES_ON_PAGE, url_builder) if paginator.wrong_page_number: return dext_views.Redirect(paginator.last_page_url, permanent=False) message_from, message_to = paginator.page_borders(context.page) messages = [ prototypes.MessagePrototype(message_model) for message_model in query.order_by("-created_at")[message_from:message_to] ] return dext_views.Page( "personal_messages/index.html", content={ "messages": messages, "paginator": paginator, "incoming": True, "index_filter": index_filter, "resource": context.resource, }, )
def index(self, page=1, owner=None, state=None, bill_type=None, voted=None, place=None):#pylint: disable=R0914 bills_query = Bill.objects.exclude(state=BILL_STATE.REMOVED) if owner is not None: bills_query = bills_query.filter(owner_id=owner.id) if state is not None: bills_query = bills_query.filter(state=state.value) if bill_type is not None: bills_query = bills_query.filter(type=bill_type.value) if place is not None: bills_query = bills_query.filter(actor__place_id=place.id) if not self.account.is_authenticated(): voted = None if voted is not None: if voted.is_NO: bills_query = bills_query.filter(~models.Q(vote__owner=self.account._model)).distinct() elif voted.is_YES: bills_query = bills_query.filter(vote__owner=self.account._model).distinct() else: bills_query = bills_query.filter(vote__owner=self.account._model, vote__type=voted.vote_type).distinct() url_builder = UrlBuilder(reverse('game:bills:'), arguments={'owner': owner.id if owner else None, 'state': state.value if state else None, 'bill_type': bill_type.value if bill_type else None, 'voted': voted.value if voted else None, 'place': place.id if place else None}) IndexFilter = LoginedIndexFilter if self.account.is_authenticated() else UnloginedIndexFilter #pylint: disable=C0103 index_filter = IndexFilter(url_builder=url_builder, values={'owner': owner.nick if owner else None, 'state': state.value if state else None, 'bill_type': bill_type.value if bill_type else None, 'voted': voted.value if voted else None, 'place': place.id if place else None}) bills_count = bills_query.count() page = int(page) - 1 paginator = Paginator(page, bills_count, bills_settings.BILLS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) bill_from, bill_to = paginator.page_borders(page) bills = [ BillPrototype(bill) for bill in bills_query.select_related().order_by('-updated_at')[bill_from:bill_to]] votes = {} if self.account.is_authenticated(): votes = dict( (vote.bill_id, VotePrototype(vote)) for vote in Vote.objects.filter(bill_id__in=[bill.id for bill in bills], owner=self.account._model) ) return self.template('bills/index.html', {'bills': bills, 'votes': votes, 'page_type': 'index', 'BILLS_BY_ID': BILLS_BY_ID, 'paginator': paginator, 'index_filter': index_filter} )
def create_paginator(self, total_pages, current_page): return Paginator(current_page_number=current_page, records_number=total_pages * 3, records_on_page=3, url_builder=None)
def initialize(self, account, thread, page, inline=False): self.account = account self.thread = thread url_builder = UrlBuilder(reverse('forum:threads:show', args=[self.thread.id]), arguments={'page': page}) page -= 1 self.paginator = Paginator(page, thread.posts_count + 1, forum_settings.POSTS_ON_PAGE, url_builder) if self.paginator.wrong_page_number: return False post_from, post_to = self.paginator.page_borders(page) self.post_from = post_from self.posts = [ PostPrototype(post_model) for post_model in Post.objects.filter(thread=self.thread._model). order_by('created_at')[post_from:post_to] ] self.authors = { author.id: author for author in AccountPrototype.get_list_by_id( [post.author_id for post in self.posts]) } self.game_objects = { game_object.account_id: game_object for game_object in heroes_logic.load_heroes_by_account_ids( [post.author_id for post in self.posts]) } pages_on_page_slice = self.posts if post_from == 0: pages_on_page_slice = pages_on_page_slice[1:] self.has_post_on_page = any([ post.author.id == self.account.id for post in pages_on_page_slice ]) self.new_post_form = forms.NewPostForm() self.start_posts_from = page * forum_settings.POSTS_ON_PAGE self.inline = inline self.can_delete_posts = can_delete_posts(self.account, self.thread) self.can_change_posts = can_change_posts(self.account) self.can_delete_thread = not self.inline and can_delete_thread( self.account) self.can_change_thread = not self.inline and can_change_thread( self.account, self.thread) self.ignore_first_post = (self.inline and self.paginator.current_page_number == 0) self.can_post = self.account.is_authenticated( ) and not self.account.is_fast self.no_posts = (len(self.posts) == 0) or (self.ignore_first_post and len(self.posts) == 1) self.can_subscribe = self.account.is_authenticated( ) and not self.account.is_fast self.has_subscription = SubscriptionPrototype.has_subscription( self.account, self.thread) return True
def index(self, key=None, state=None, filter=None, restriction=None, errors_status=None, page=1, contributor=None, order_by=relations.INDEX_ORDER_BY.UPDATED_AT): templates_query = prototypes.TemplatePrototype._db_all().order_by('raw_template') if contributor is not None: entities_ids = prototypes.ContributionPrototype._db_filter(type=relations.CONTRIBUTION_TYPE.TEMPLATE, account_id=contributor.id).values_list('entity_id', flat=True) templates_query = templates_query.filter(models.Q(id__in=entities_ids)|models.Q(author_id=contributor.id)) if key: templates_query = templates_query.filter(key=key) if state: templates_query = templates_query.filter(state=state) if errors_status: templates_query = templates_query.filter(errors_status=errors_status) if restriction: templates_query = templates_query.filter(templaterestriction__restriction_id=restriction.id) if filter: templates_query = templates_query.filter(raw_template__icontains=filter) if order_by.is_UPDATED_AT: templates_query = templates_query.order_by('-updated_at') elif order_by.is_TEXT: templates_query = templates_query.order_by('raw_template') page = int(page) - 1 templates_count = templates_query.count() url_builder = UrlBuilder(reverse('linguistics:templates:'), arguments={ 'state': state.value if state else None, 'errors_status': errors_status.value if errors_status else None, 'contributor': contributor.id if contributor else None, 'order_by': order_by.value, 'filter': filter, 'restriction': restriction.id if restriction is not None else None, 'key': key.value if key is not None else None}) index_filter = TemplatesIndexFilter(url_builder=url_builder, values={'state': state.value if state else None, 'errors_status': errors_status.value if errors_status else None, 'contributor': contributor.nick if contributor else None, 'order_by': order_by.value, 'filter': filter, 'restriction': restriction.id if restriction is not None else None, 'key': key.value if key is not None else None, 'count': templates_query.count()}) paginator = Paginator(page, templates_count, linguistics_settings.TEMPLATES_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) template_from, template_to = paginator.page_borders(page) templates = prototypes.TemplatePrototype.from_query(templates_query[template_from:template_to]) authors = {account.id: account for account in AccountPrototype.from_query(AccountPrototype.get_list_by_id([template.author_id for template in templates]))} clans = {clan.id: clan for clan in ClanPrototype.from_query(ClanPrototype.get_list_by_id([author.clan_id for author in authors.itervalues()]))} return self.template('linguistics/templates/index.html', {'key': key, 'templates': templates, 'index_filter': index_filter, 'page_type': 'all-templates', 'paginator': paginator, 'authors': authors, 'clans': clans, 'LEXICON_KEY': keys.LEXICON_KEY} )
def index(self, page=1, author_id=None, order_by=ORDER_BY.CREATED_AT, tag_id=None): posts_query = models.Post.objects.filter( state__in=[relations.POST_STATE.NOT_MODERATED, relations.POST_STATE.ACCEPTED] ) is_filtering = False author_account = None if author_id is not None: author_id = int(author_id) author_account = AccountPrototype.get_by_id(author_id) if author_account: posts_query = posts_query.filter(author_id=author_account.id) is_filtering = True else: posts_query = models.Post.objects.none() if tag_id is not None: posts_query = posts_query.filter( id__in=models.Tagged.objects.filter(tag_id=tag_id).values_list("post_id", flat=True) ) if order_by is not None: if order_by == ORDER_BY.ALPHABET: posts_query = posts_query.order_by("caption") elif order_by == ORDER_BY.CREATED_AT: posts_query = posts_query.order_by("-created_at") elif order_by == ORDER_BY.RATING: posts_query = posts_query.order_by("-votes") elif order_by == ORDER_BY.MIGHT: posts_query = posts_query.order_by("-rating") else: order_by = ORDER_BY.CREATED_AT posts_query = posts_query.order_by("-created_at") url_builder = UrlBuilder( reverse("blogs:posts:"), arguments={"author_id": author_id, "order_by": order_by, "tag_id": tag_id} ) posts_count = posts_query.count() page = int(page) - 1 paginator = Paginator(page, posts_count, conf.settings.POSTS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) post_from, post_to = paginator.page_borders(page) posts = [prototypes.PostPrototype(post) for post in posts_query.select_related()[post_from:post_to]] votes = {} if self.account.is_authenticated: votes = dict( (vote.post_id, prototypes.VotePrototype(vote)) for vote in models.Vote.objects.filter( post_id__in=[post.id for post in posts], voter=self.account._model ) ) return self.template( "blogs/index.html", { "posts": posts, "page_type": "index", "votes": votes, "FORUM_TAGS_THREAD": conf.settings.FORUM_TAGS_THREAD, "order_by": order_by, "current_tag": models.Tag.objects.get(id=tag_id) if tag_id is not None else None, "ORDER_BY": ORDER_BY, "is_filtering": is_filtering, "current_page_number": page, "author_account": author_account, "paginator": paginator, "url_builder": url_builder, }, )
def index(self, page=1, owner=None, state=None, bill_type=None, voted=None, place=None): #pylint: disable=R0914 bills_query = Bill.objects.exclude(state=BILL_STATE.REMOVED) if owner is not None: bills_query = bills_query.filter(owner_id=owner.id) if state is not None: bills_query = bills_query.filter(state=state.value) if bill_type is not None: bills_query = bills_query.filter(type=bill_type.value) if place is not None: bills_query = bills_query.filter( actor__place_id=place.id).distinct() if not self.account.is_authenticated: voted = None if voted is not None: if voted.is_NO: bills_query = bills_query.filter(~models.Q( vote__owner=self.account._model)).distinct() elif voted.is_YES: bills_query = bills_query.filter( vote__owner=self.account._model).distinct() else: bills_query = bills_query.filter( vote__owner=self.account._model, vote__type=voted.vote_type).distinct() url_builder = UrlBuilder(reverse('game:bills:'), arguments={ 'owner': owner.id if owner else None, 'state': state.value if state else None, 'bill_type': bill_type.value if bill_type else None, 'voted': voted.value if voted else None, 'place': place.id if place else None }) IndexFilter = LoginedIndexFilter if self.account.is_authenticated else UnloginedIndexFilter #pylint: disable=C0103 index_filter = IndexFilter(url_builder=url_builder, values={ 'owner': owner.nick if owner else None, 'state': state.value if state else None, 'bill_type': bill_type.value if bill_type else None, 'voted': voted.value if voted else None, 'place': place.id if place else None }) bills_count = bills_query.count() page = int(page) - 1 paginator = Paginator(page, bills_count, bills_settings.BILLS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) bill_from, bill_to = paginator.page_borders(page) bills = [ BillPrototype(bill) for bill in bills_query.select_related().order_by('-updated_at') [bill_from:bill_to] ] votes = {} if self.account.is_authenticated: votes = dict((vote.bill_id, VotePrototype(vote)) for vote in Vote.objects.filter( bill_id__in=[bill.id for bill in bills], owner=self.account._model)) return self.template( 'bills/index.html', { 'bills': bills, 'votes': votes, 'page_type': 'index', 'BILLS_BY_ID': BILLS_BY_ID, 'paginator': paginator, 'index_filter': index_filter })
def show(self, page=1): # pylint: disable=R0914 ratings_updated_at_timestamp = settings.get(ratings_settings.SETTINGS_UPDATE_TIMESTEMP_KEY, None) ratings_query = RatingPlaces.objects.all().select_related() place_getter = None value_getter = None if self.rating_type.is_MIGHT: ratings_query = ratings_query.filter(account__ratingvalues__might__gt=0).order_by('might_place') place_getter = lambda places: places.might_place value_getter = lambda values: '%.2f' % values.might elif self.rating_type.is_BILLS: ratings_query = ratings_query.filter(account__ratingvalues__bills_count__gt=0).order_by('bills_count_place') place_getter = lambda places: places.bills_count_place value_getter = lambda values: values.bills_count elif self.rating_type.is_MAGIC_POWER: ratings_query = ratings_query.order_by('magic_power_place') place_getter = lambda places: places.magic_power_place value_getter = lambda values: values.magic_power elif self.rating_type.is_PHYSIC_POWER: ratings_query = ratings_query.order_by('physic_power_place') place_getter = lambda places: places.physic_power_place value_getter = lambda values: values.physic_power elif self.rating_type.is_LEVEL: ratings_query = ratings_query.order_by('level_place') place_getter = lambda places: places.level_place value_getter = lambda values: values.level elif self.rating_type.is_PHRASES: ratings_query = ratings_query.filter(account__ratingvalues__phrases_count__gt=0).order_by('phrases_count_place') place_getter = lambda places: places.phrases_count_place value_getter = lambda values: values.phrases_count elif self.rating_type.is_PVP_BATTLES_1x1_NUMBER: ratings_query = ratings_query.filter(account__ratingvalues__pvp_battles_1x1_number__gt=0).order_by('pvp_battles_1x1_number_place') place_getter = lambda places: places.pvp_battles_1x1_number_place value_getter = lambda values: values.pvp_battles_1x1_number elif self.rating_type.is_PVP_BATTLES_1x1_VICTORIES: ratings_query = ratings_query.filter(account__ratingvalues__pvp_battles_1x1_victories__gt=0).order_by('pvp_battles_1x1_victories_place') place_getter = lambda places: places.pvp_battles_1x1_victories_place value_getter = lambda values: '%.2f%%' % (values.pvp_battles_1x1_victories * 100) elif self.rating_type.is_REFERRALS_NUMBER: ratings_query = ratings_query.filter(account__ratingvalues__referrals_number__gt=0).order_by('referrals_number_place') place_getter = lambda places: places.referrals_number_place value_getter = lambda values: values.referrals_number elif self.rating_type.is_ACHIEVEMENTS_POINTS: ratings_query = ratings_query.filter(account__ratingvalues__achievements_points__gt=0).order_by('achievements_points_place') place_getter = lambda places: places.achievements_points_place value_getter = lambda values: values.achievements_points elif self.rating_type.is_HELP_COUNT: ratings_query = ratings_query.order_by('help_count_place') place_getter = lambda places: places.help_count_place value_getter = lambda values: values.help_count elif self.rating_type.is_GIFTS_RETURNED: ratings_query = ratings_query.filter(account__ratingvalues__gifts_returned__gt=0).order_by('gifts_returned_place') place_getter = lambda places: places.gifts_returned_place value_getter = lambda values: values.gifts_returned elif self.rating_type.is_POLITICS_POWER: ratings_query = ratings_query.filter(account__ratingvalues__politics_power__gt=0).order_by('politics_power_place') place_getter = lambda places: places.politics_power_place value_getter = lambda values: '%.2f%%' % (values.politics_power * 100) ratings_count = ratings_query.count() page = int(page) - 1 url_builder = UrlBuilder(reverse('game:ratings:show', args=[self.rating_type.value]), arguments={'page': page}) paginator = Paginator(page, ratings_count, ratings_settings.ACCOUNTS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) rating_from, rating_to = paginator.page_borders(page) ratings = [ RatingPlacesPrototype(rating_model) for rating_model in ratings_query[rating_from:rating_to] ] accounts_ids = [rating.account_id for rating in ratings] clans_ids = set(AccountPrototype._db_filter(id__in=accounts_ids).exclude(clan_id=None).values_list('clan_id', flat=True)) heroes = { hero.account_id: hero for hero in heroes_logic.load_heroes_by_account_ids(accounts_ids)} values = dict( (values_model.account_id, RatingValuesPrototype(values_model)) for values_model in RatingValues.objects.filter(account_id__in=accounts_ids)) clans = {clan.id:clan for clan in ClanPrototype.get_list_by_id(list(clans_ids))} return self.template('ratings/show.html', {'ratings': ratings, 'ratings_updated_at_timestamp': ratings_updated_at_timestamp, 'heroes': heroes, 'values': values, 'clans': clans, 'paginator': paginator, 'place_getter': place_getter, 'value_getter': value_getter, 'rating_type': self.rating_type, 'RATING_TYPE': RATING_TYPE, 'rating_from': rating_from, 'rating_to': rating_to})
def index(self, page=1, state=None, type=None, filter=None, contributor=None, order_by=relations.INDEX_ORDER_BY.UPDATED_AT): words_query = prototypes.WordPrototype._db_all().order_by('normal_form') if contributor is not None: entities_ids = prototypes.ContributionPrototype._db_filter(type=relations.CONTRIBUTION_TYPE.WORD, account_id=contributor.id).values_list('entity_id', flat=True) words_query = words_query.filter(models.Q(id__in=entities_ids)|models.Q(author_id=contributor.id)) if state: words_query = words_query.filter(state=state) if type: words_query = words_query.filter(type=type.utg_type) if filter: words_query = words_query.filter(normal_form__istartswith=filter.lower()) if order_by.is_UPDATED_AT: words_query = words_query.order_by('-updated_at') elif order_by.is_TEXT: words_query = words_query.order_by('normal_form') words_count = words_query.count() url_builder = UrlBuilder(reverse('linguistics:words:'), arguments={ 'state': state.value if state else None, 'type': type.value if type else None, 'contributor': contributor.id if contributor else None, 'order_by': order_by.value, 'filter': filter}) index_filter = WordsIndexFilter(url_builder=url_builder, values={'state': state.value if state else None, 'type': type.value if type else None, 'filter': filter, 'contributor': contributor.nick if contributor else None, 'order_by': order_by.value, 'count': words_count}) page = int(page) - 1 paginator = Paginator(page, words_count, linguistics_settings.WORDS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) words_from, words_to = paginator.page_borders(page) words = prototypes.WordPrototype.from_query(words_query[words_from:words_to]) authors = {account.id: account for account in AccountPrototype.from_query(AccountPrototype.get_list_by_id([word.author_id for word in words]))} clans = {clan.id: clan for clan in ClanPrototype.from_query(ClanPrototype.get_list_by_id([author.clan_id for author in authors.itervalues()]))} return self.template('linguistics/words/index.html', {'words': words, 'page_type': 'dictionary', 'paginator': paginator, 'authors': authors, 'clans': clans, 'ALLOWED_WORD_TYPE': relations.ALLOWED_WORD_TYPE, 'index_filter': index_filter} )
def index( self, key=None, state=None, filter=None, restriction=None, errors_status=None, page=1, contributor=None, order_by=relations.INDEX_ORDER_BY.UPDATED_AT, ): templates_query = prototypes.TemplatePrototype._db_all().order_by("raw_template") if contributor is not None: entities_ids = prototypes.ContributionPrototype._db_filter( type=relations.CONTRIBUTION_TYPE.TEMPLATE, account_id=contributor.id ).values_list("entity_id", flat=True) templates_query = templates_query.filter(models.Q(id__in=entities_ids) | models.Q(author_id=contributor.id)) if key: templates_query = templates_query.filter(key=key) if state: templates_query = templates_query.filter(state=state) if errors_status: templates_query = templates_query.filter(errors_status=errors_status) if restriction: templates_query = templates_query.filter(templaterestriction__restriction_id=restriction.id) if filter: templates_query = templates_query.filter(raw_template__icontains=filter) if order_by.is_UPDATED_AT: templates_query = templates_query.order_by("-updated_at") elif order_by.is_TEXT: templates_query = templates_query.order_by("raw_template") page = int(page) - 1 templates_count = templates_query.count() url_builder = UrlBuilder( reverse("linguistics:templates:"), arguments={ "state": state.value if state else None, "errors_status": errors_status.value if errors_status else None, "contributor": contributor.id if contributor else None, "order_by": order_by.value, "filter": filter, "restriction": restriction.id if restriction is not None else None, "key": key.value if key is not None else None, }, ) index_filter = TemplatesIndexFilter( url_builder=url_builder, values={ "state": state.value if state else None, "errors_status": errors_status.value if errors_status else None, "contributor": contributor.nick if contributor else None, "order_by": order_by.value, "filter": filter, "restriction": restriction.id if restriction is not None else None, "key": key.value if key is not None else None, "count": templates_query.count(), }, ) paginator = Paginator(page, templates_count, linguistics_settings.TEMPLATES_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) template_from, template_to = paginator.page_borders(page) templates = prototypes.TemplatePrototype.from_query(templates_query[template_from:template_to]) authors = { account.id: account for account in AccountPrototype.from_query( AccountPrototype.get_list_by_id([template.author_id for template in templates]) ) } clans = { clan.id: clan for clan in ClanPrototype.from_query( ClanPrototype.get_list_by_id([author.clan_id for author in authors.itervalues()]) ) } return self.template( "linguistics/templates/index.html", { "key": key, "templates": templates, "index_filter": index_filter, "page_type": "all-templates", "paginator": paginator, "authors": authors, "clans": clans, "LEXICON_KEY": keys.LEXICON_KEY, }, )
def show(self, page=1): # pylint: disable=R0914 ratings_updated_at_timestamp = settings.get( ratings_settings.SETTINGS_UPDATE_TIMESTEMP_KEY, None) ratings_query = RatingPlaces.objects.all().select_related() place_getter = None value_getter = None if self.rating_type.is_MIGHT: ratings_query = ratings_query.filter( account__ratingvalues__might__gt=0).order_by('might_place') place_getter = lambda places: places.might_place value_getter = lambda values: '%.2f' % values.might elif self.rating_type.is_BILLS: ratings_query = ratings_query.filter( account__ratingvalues__bills_count__gt=0).order_by( 'bills_count_place') place_getter = lambda places: places.bills_count_place value_getter = lambda values: values.bills_count elif self.rating_type.is_MAGIC_POWER: ratings_query = ratings_query.order_by('magic_power_place') place_getter = lambda places: places.magic_power_place value_getter = lambda values: values.magic_power elif self.rating_type.is_PHYSIC_POWER: ratings_query = ratings_query.order_by('physic_power_place') place_getter = lambda places: places.physic_power_place value_getter = lambda values: values.physic_power elif self.rating_type.is_LEVEL: ratings_query = ratings_query.order_by('level_place') place_getter = lambda places: places.level_place value_getter = lambda values: values.level elif self.rating_type.is_PHRASES: ratings_query = ratings_query.filter( account__ratingvalues__phrases_count__gt=0).order_by( 'phrases_count_place') place_getter = lambda places: places.phrases_count_place value_getter = lambda values: values.phrases_count elif self.rating_type.is_PVP_BATTLES_1x1_NUMBER: ratings_query = ratings_query.filter( account__ratingvalues__pvp_battles_1x1_number__gt=0).order_by( 'pvp_battles_1x1_number_place') place_getter = lambda places: places.pvp_battles_1x1_number_place value_getter = lambda values: values.pvp_battles_1x1_number elif self.rating_type.is_PVP_BATTLES_1x1_VICTORIES: ratings_query = ratings_query.filter( account__ratingvalues__pvp_battles_1x1_victories__gt=0 ).order_by('pvp_battles_1x1_victories_place') place_getter = lambda places: places.pvp_battles_1x1_victories_place value_getter = lambda values: '%.2f%%' % ( values.pvp_battles_1x1_victories * 100) elif self.rating_type.is_REFERRALS_NUMBER: ratings_query = ratings_query.filter( account__ratingvalues__referrals_number__gt=0).order_by( 'referrals_number_place') place_getter = lambda places: places.referrals_number_place value_getter = lambda values: values.referrals_number elif self.rating_type.is_ACHIEVEMENTS_POINTS: ratings_query = ratings_query.filter( account__ratingvalues__achievements_points__gt=0).order_by( 'achievements_points_place') place_getter = lambda places: places.achievements_points_place value_getter = lambda values: values.achievements_points elif self.rating_type.is_HELP_COUNT: ratings_query = ratings_query.order_by('help_count_place') place_getter = lambda places: places.help_count_place value_getter = lambda values: values.help_count elif self.rating_type.is_GIFTS_RETURNED: ratings_query = ratings_query.filter( account__ratingvalues__gifts_returned__gt=0).order_by( 'gifts_returned_place') place_getter = lambda places: places.gifts_returned_place value_getter = lambda values: values.gifts_returned elif self.rating_type.is_POLITICS_POWER: ratings_query = ratings_query.filter( account__ratingvalues__politics_power__gt=0).order_by( 'politics_power_place') place_getter = lambda places: places.politics_power_place value_getter = lambda values: '%.2f%%' % (values.politics_power * 100) ratings_count = ratings_query.count() page = int(page) - 1 url_builder = UrlBuilder(reverse('game:ratings:show', args=[self.rating_type.value]), arguments={'page': page}) paginator = Paginator(page, ratings_count, ratings_settings.ACCOUNTS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) rating_from, rating_to = paginator.page_borders(page) ratings = [ RatingPlacesPrototype(rating_model) for rating_model in ratings_query[rating_from:rating_to] ] accounts_ids = [rating.account_id for rating in ratings] clans_ids = set( AccountPrototype._db_filter(id__in=accounts_ids).exclude( clan_id=None).values_list('clan_id', flat=True)) heroes = { hero.account_id: hero for hero in heroes_logic.load_heroes_by_account_ids(accounts_ids) } values = dict( (values_model.account_id, RatingValuesPrototype(values_model)) for values_model in RatingValues.objects.filter( account_id__in=accounts_ids)) clans = { clan.id: clan for clan in ClanPrototype.get_list_by_id(list(clans_ids)) } return self.template( 'ratings/show.html', { 'ratings': ratings, 'ratings_updated_at_timestamp': ratings_updated_at_timestamp, 'heroes': heroes, 'values': values, 'clans': clans, 'paginator': paginator, 'place_getter': place_getter, 'value_getter': value_getter, 'rating_type': self.rating_type, 'RATING_TYPE': RATING_TYPE, 'rating_from': rating_from, 'rating_to': rating_to })
def paginator(self): url_builder = UrlBuilder(reverse('forum:threads:show', args=[self.id])) # +1 since first post does not counted return Paginator(1, self.posts_count+1, forum_settings.POSTS_ON_PAGE, url_builder)
def index(self, page=1, author_id=None, order_by=ORDER_BY.CREATED_AT): posts_query = models.Post.objects.filter(state__in=[ relations.POST_STATE.NOT_MODERATED, relations.POST_STATE.ACCEPTED ]) is_filtering = False author_account = None if author_id is not None: author_id = int(author_id) author_account = AccountPrototype.get_by_id(author_id) if author_account: posts_query = posts_query.filter(author_id=author_account.id) is_filtering = True else: posts_query = models.Post.objects.none() if order_by is not None: if order_by == ORDER_BY.ALPHABET: posts_query = posts_query.order_by('caption') elif order_by == ORDER_BY.CREATED_AT: posts_query = posts_query.order_by('-created_at') elif order_by == ORDER_BY.RATING: posts_query = posts_query.order_by('-votes') else: order_by = ORDER_BY.CREATED_AT posts_query = posts_query.order_by('-created_at') url_builder = UrlBuilder(reverse('blogs:posts:'), arguments={ 'author_id': author_id, 'order_by': order_by }) posts_count = posts_query.count() page = int(page) - 1 paginator = Paginator(page, posts_count, conf.settings.POSTS_ON_PAGE, url_builder) if paginator.wrong_page_number: return self.redirect(paginator.last_page_url, permanent=False) post_from, post_to = paginator.page_borders(page) posts = [ prototypes.PostPrototype(post) for post in posts_query.select_related()[post_from:post_to] ] votes = {} if self.account.is_authenticated(): votes = dict((vote.post_id, prototypes.VotePrototype(vote)) for vote in models.Vote.objects.filter( post_id__in=[post.id for post in posts], voter=self.account._model)) return self.template( 'blogs/index.html', { 'posts': posts, 'page_type': 'index', 'votes': votes, 'order_by': order_by, 'ORDER_BY': ORDER_BY, 'is_filtering': is_filtering, 'current_page_number': page, 'author_account': author_account, 'paginator': paginator, 'url_builder': url_builder })
def get_post_url(self, post): thread_posts_ids = list(PostPrototype._db_filter(thread_id=post.thread_id).order_by('created_at').values_list('id', flat=True)) page = Paginator.get_page_numbers(thread_posts_ids.index(post.id)+1, forum_settings.POSTS_ON_PAGE) return url('forum:threads:show', post.thread_id, page=page) + ('#m%d' % post.id)