Example #1
0
    def apply(self):
        if not self.state.is_VOTING:
            raise exceptions.ApplyBillInWrongStateError(bill_id=self.id)

        if not self.approved_by_moderator:
            raise exceptions.ApplyUnapprovedBillError(bill_id=self.id)

        if self.time_before_voting_end != datetime.timedelta(seconds=0):
            raise exceptions.ApplyBillBeforeVoteWasEndedError(bill_id=self.id)

        self.recalculate_votes()

        self._model.min_votes_percents_required = bills_settings.MIN_VOTES_PERCENT

        results_text = u'Итоги голосования: %d «за», %d «против» (итого %.1f%% «за»), %d «воздержалось».' % (
            self.votes_for, self.votes_against,
            round(self.votes_for_percents, 3) * 100, self.votes_refrained)

        self._model.voting_end_at = datetime.datetime.now()

        self.applyed_at_turn = TimePrototype.get_current_turn_number()

        if self.is_percents_barier_not_passed:
            self.state = BILL_STATE.REJECTED
            self.save()

            PostPrototype.create(ThreadPrototype(self._model.forum_thread),
                                 get_system_user(),
                                 u'Законопроект отклонён.\n\n%s' %
                                 results_text,
                                 technical=True)

            signals.bill_processed.send(self.__class__, bill=self)
            return False

        self.data.apply(self)

        self.state = BILL_STATE.ACCEPTED

        with achievements_storage.verify(
                type=ACHIEVEMENT_TYPE.POLITICS_ACCEPTED_BILLS,
                object=self.owner):
            self.save()

        PostPrototype.create(
            ThreadPrototype(self._model.forum_thread),
            get_system_user(),
            u'Законопроект принят. Изменения вступят в силу в ближайшее время.\n\n%s'
            % results_text,
            technical=True)

        for actor in self.data.actors:
            if isinstance(actor, PlacePrototype):
                actor.stability_modifiers.append(
                    (u'закон №%d' % self.id, -self.type.stability))

        logic.initiate_actual_bills_update(self._model.owner_id)

        signals.bill_processed.send(self.__class__, bill=self)
        return True
Example #2
0
    def update(self, form):

        Vote.objects.filter(bill_id=self.id).delete()

        VotePrototype.create(self.owner, self, VOTE_TYPE.FOR)

        self.data.initialize_with_user_data(form)

        self._model.updated_at = datetime.datetime.now()
        self._model.caption = form.c.caption
        self._model.rationale = form.c.rationale
        self._model.approved_by_moderator = False
        self._model.chronicle_on_accepted = form.c.chronicle_on_accepted

        self.recalculate_votes()

        self.save()

        ActorPrototype.update_actors(self, self.data.actors)

        thread = ThreadPrototype(self._model.forum_thread)
        thread.caption = form.c.caption
        thread.save()

        text = u'[url="%s%s"]Законопроект[/url] был отредактирован, все голоса сброшены.' % (
            project_settings.SITE_URL,
            reverse('game:bills:show', args=[self.id]))

        PostPrototype.create(thread,
                             get_system_user(),
                             self.bill_info_text(text),
                             technical=True)

        signals.bill_edited.send(self.__class__, bill=self)
Example #3
0
    def update(self, form):

        Vote.objects.filter(bill_id=self.id).delete()

        VotePrototype.create(self.owner, self, VOTE_TYPE.FOR)

        self._initialize_with_form(form)

        self.recalculate_votes()

        self.save()

        ActorPrototype.update_actors(self, self.actors)

        thread = ThreadPrototype(self._model.forum_thread)
        thread.caption = form.c.caption
        thread.save()

        text = '[url="%s%s"]Запись[/url] была отредактирована, все голоса сброшены.' % (
            project_settings.SITE_URL,
            reverse('game:bills:show', args=[self.id]))

        PostPrototype.create(thread,
                             get_system_user(),
                             self.bill_info_text(text),
                             technical=True)

        signals.bill_edited.send(self.__class__, bill=self)
Example #4
0
    def feed(self):
        feed = Atom1Feed(u'Сказка: Форум',
                         self.request.build_absolute_uri('/'),
                         u'Новые темы на форуме мморпг «Сказка»',
                         language=u'ru',
                         feed_url=self.request.build_absolute_uri(
                             reverse('forum:feed')))

        threads = [
            ThreadPrototype(model=thread)
            for thread in Thread.objects.filter(subcategory__restricted=False).
            order_by('-created_at')[:forum_settings.FEED_ITEMS_NUMBER]
        ]

        for thread in threads:

            if datetime.datetime.now(
            ) - thread.created_at < datetime.timedelta(
                    seconds=forum_settings.FEED_ITEMS_DELAY):
                continue

            post = PostPrototype(model=Post.objects.filter(
                thread_id=thread.id).order_by('created_at')[0])

            url = self.request.build_absolute_uri(
                reverse('forum:threads:show', args=[thread.id]))

            feed.add_item(title=thread.caption,
                          link=url,
                          description=post.safe_html,
                          pubdate=thread.created_at,
                          unique_id=url)

        return self.atom(feed.writeString('utf-8'))
Example #5
0
    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)
            })
Example #6
0
    def remove(self, initiator):
        self.set_remove_initiator(initiator)
        self.state = BILL_STATE.REMOVED
        self.save()

        thread = ThreadPrototype(self._model.forum_thread)
        thread.caption = thread.caption + u' [удалён]'
        thread.save()

        PostPrototype.create(thread,
                             get_system_user(),
                             u'Законопроект был удалён',
                             technical=True)

        signals.bill_removed.send(self.__class__, bill=self)
Example #7
0
    def stop(self):
        if not self.state.is_VOTING:
            raise exceptions.StopBillInWrongStateError(bill_id=self.id)

        results_text = u'Итоги голосования: %d «за», %d «против» (итого %.1f%% «за»), %d «воздержалось».' % (self.votes_for,
                                                                                                             self.votes_against,
                                                                                                             round(self.votes_for_percents, 3)*100,
                                                                                                             self.votes_refrained)

        with transaction.atomic():
            self.state = BILL_STATE.STOPPED
            self.save()

            PostPrototype.create(ThreadPrototype(self._model.forum_thread),
                                 get_system_user(),
                                 u'Законопроект потерял смысл, голосование остановлено. %s' % results_text,
                                 technical=True)

            signals.bill_stopped.send(self.__class__, bill=self)
Example #8
0
    def end(self):
        if not self.state.is_ACCEPTED:
            raise exceptions.EndBillInWrongStateError(bill_id=self.id)

        if self.ended_at is not None:
            raise exceptions.EndBillAlreadyEndedError(bill_id=self.id)

        results_text = u'Срок действия [url="%s%s"]закона[/url] истёк.' % (project_settings.SITE_URL,
                                                                           reverse('game:bills:show', args=[self.id]) )

        self._model.ended_at = datetime.datetime.now()

        self.data.end(self)

        self.save()

        PostPrototype.create(ThreadPrototype(self._model.forum_thread),
                             get_system_user(),
                             results_text,
                             technical=True)

        signals.bill_ended.send(self.__class__, bill=self)
        return True
Example #9
0
    def apply(self):
        if not self.state.is_VOTING:
            raise exceptions.ApplyBillInWrongStateError(bill_id=self.id)

        if not self.approved_by_moderator:
            raise exceptions.ApplyUnapprovedBillError(bill_id=self.id)

        if self.time_before_voting_end != datetime.timedelta(seconds=0):
            raise exceptions.ApplyBillBeforeVoteWasEndedError(bill_id=self.id)

        self.recalculate_votes()

        self._model.min_votes_percents_required = bills_settings.MIN_VOTES_PERCENT

        results_text = 'Итоги голосования: %d «за», %d «против» (итого %.1f%% «за»), %d «воздержалось».' % (
            self.votes_for, self.votes_against,
            round(self.votes_for_percents, 3) * 100, self.votes_refrained)

        self._model.voting_end_at = datetime.datetime.now()

        self.applyed_at_turn = turn.number()

        with transaction.atomic():

            if self.is_percents_barier_not_passed:
                self.state = BILL_STATE.REJECTED
                self.save()

                PostPrototype.create(ThreadPrototype(self._model.forum_thread),
                                     get_system_user(),
                                     'Запись отклонена.\n\n%s' % results_text,
                                     technical=True)

                signals.bill_processed.send(self.__class__, bill=self)
                return False

            self.data.apply(self)

            self.state = BILL_STATE.ACCEPTED

            with achievements_storage.verify(
                    type=ACHIEVEMENT_TYPE.POLITICS_ACCEPTED_BILLS,
                    object=self.owner):
                self.save()

            PostPrototype.create(
                ThreadPrototype(self._model.forum_thread),
                get_system_user(),
                'Запись одобрена. Изменения вступят в силу в ближайшее время.\n\n%s'
                % results_text,
                technical=True)

            for actor in self.actors:
                if isinstance(actor, places_objects.Place):
                    actor.effects.add(
                        effects.Effect(
                            name='запись №{}'.format(self.id),
                            attribute=places_relations.ATTRIBUTE.STABILITY,
                            value=-self.type.stability))

        logic.initiate_actual_bills_update(self._model.owner_id)

        signals.bill_processed.send(self.__class__, bill=self)
        return True