コード例 #1
0
    def update(self, answer=None):
        need(IPollQuestionResources)
        # This code is too complicated and would need
        # simplification. This is all the logic that was in the
        # template before.
        now = datetime.now()
        locale_opts = {
            'size': 'full',
            'locale': get_locale_info(self.request),
            'display_time': False
        }
        self.question = self.content.get_question()
        self.has_voted = self.content.has_voted(self.request)
        self.show_results_not_ready = False

        if self.is_preview:
            # Preview: show poll and results
            self.show_poll_not_ready = False
            self.show_poll = True
            self.show_results = True
            self.show_outdated = False
        else:
            # Public view: show corresponding section depending of the
            # current time
            start_date = self.content.question_start_datetime()
            end_date = self.content.question_end_datetime()
            self.show_poll_not_ready = start_date is not None and start_date > now
            if self.show_poll_not_ready:
                self.poll_start_date = get_formatted_date(
                    start_date, **locale_opts)
            self.show_poll = ((start_date is not None and start_date < now)
                              and (end_date is None or end_date > now))
            start_date = self.content.result_start_datetime()
            end_date = self.content.result_end_datetime()
            self.show_outdated = end_date is not None and end_date < now
            self.show_results = ((start_date is not None and start_date < now)
                                 and (end_date is None or end_date > now))

        if self.show_poll:
            # We want to show the poll.
            if self.has_voted:
                # The user already voted, don't show the poll, even if
                # the dates are ok.
                self.show_poll = False
            else:
                if answer is not None:
                    # User just voted, register the code, and don't
                    # show the poll.
                    answer = str(answer, 'utf-8')
                    self.content.vote(self.request, answer)
                    self.has_voted = True
                    self.show_poll = False
                else:
                    # The user didn't vote yet. Really show poll.
                    self.answers = []
                    for index, answer in enumerate(self.content.get_answers()):
                        self.answers.append({
                            'title': answer,
                            'id': 'answer-%02d' % index
                        })

        if self.show_results:
            all_votes = self.content.get_votes()
            self.total_votes = sum(all_votes)
            self.results = []
            for answer, votes in zip(self.content.get_answers(), all_votes):
                percentage = 0
                if self.total_votes:
                    percentage = round((votes * 100.0) / self.total_votes)
                self.results.append({
                    'answer': answer,
                    'votes': votes,
                    'percentage': percentage
                })
        elif self.has_voted:
            # The user voted, but cannot see the results yet. Tell him
            # when he will be able to see them.

            # The result start and end date and the last fetch into
            # start_date, and end_date. Preview code doesn't fetch
            # them, but in preview the results are always shown.
            self.show_results_not_ready = True
            self.show_results_start_date = get_formatted_date(
                start_date, **locale_opts)
            self.show_results_end_date = None
            if end_date is not None:
                self.show_results_end_date = get_formatted_date(
                    end_date, **locale_opts)
コード例 #2
0
 def update(self, view):
     self.locale = localdatetime.get_locale_info(self.request)
コード例 #3
0
    def update(self, answer=None):
        need(IPollQuestionResources)
        # This code is too complicated and would need
        # simplification. This is all the logic that was in the
        # template before.
        now = datetime.now()
        locale_opts = {'size': 'full',
                       'locale': get_locale_info(self.request),
                       'display_time': False}
        self.question = self.content.get_question()
        self.has_voted = self.content.has_voted(self.request)
        self.show_results_not_ready = False

        if self.is_preview:
            # Preview: show poll and results
            self.show_poll_not_ready = False
            self.show_poll = True
            self.show_results = True
            self.show_outdated = False
        else:
            # Public view: show corresponding section depending of the
            # current time
            start_date = self.content.question_start_datetime()
            end_date = self.content.question_end_datetime()
            self.show_poll_not_ready = start_date is not None and start_date > now
            if self.show_poll_not_ready:
                self.poll_start_date = get_formatted_date(
                    start_date, **locale_opts)
            self.show_poll = (
                (start_date is not None and start_date < now) and
                (end_date is None or end_date > now))
            start_date = self.content.result_start_datetime()
            end_date = self.content.result_end_datetime()
            self.show_outdated = end_date is not None and end_date < now
            self.show_results = (
                (start_date is not None and start_date < now) and
                (end_date is None or end_date > now))

        if self.show_poll:
            # We want to show the poll.
            if self.has_voted:
                # The user already voted, don't show the poll, even if
                # the dates are ok.
                self.show_poll = False
            else:
                if answer is not None:
                    # User just voted, register the code, and don't
                    # show the poll.
                    answer = unicode(answer, 'utf-8')
                    self.content.vote(self.request, answer)
                    self.has_voted = True
                    self.show_poll = False
                else:
                    # The user didn't vote yet. Really show poll.
                    self.answers = []
                    for index, answer in enumerate(self.content.get_answers()):
                        self.answers.append({'title': answer,
                                             'id': 'answer-%02d' % index})

        if self.show_results:
            all_votes = self.content.get_votes()
            self.total_votes = sum(all_votes)
            self.results = []
            for answer, votes in zip(self.content.get_answers(), all_votes):
                percentage = 0
                if self.total_votes:
                    percentage = round((votes * 100.0)/ self.total_votes)
                self.results.append({'answer': answer,
                                     'votes': votes,
                                     'percentage': percentage})
        elif self.has_voted:
            # The user voted, but cannot see the results yet. Tell him
            # when he will be able to see them.

            # The result start and end date and the last fetch into
            # start_date, and end_date. Preview code doesn't fetch
            # them, but in preview the results are always shown.
            self.show_results_not_ready = True
            self.show_results_start_date = get_formatted_date(
                start_date, **locale_opts)
            self.show_results_end_date = None
            if end_date is not None:
                self.show_results_end_date = get_formatted_date(
                    end_date, **locale_opts)
コード例 #4
0
 def update(self, view):
     self.locale = localdatetime.get_locale_info(self.request)