Esempio n. 1
0
    def save(self, *args, **kwargs):
        election = super(ElectionAdminForm, self).save(*args, **kwargs)

        # Questions/answers have a special formatting
        election.questions = self.cleaned_data['questions']
        election.last_modified_at_date = timezone.now()
        election.save()

        kwargs = dict(election_id=election.id,
                      is_secure=self.request.is_secure(),
                      site_id=Site.objects.get_current().id,
                      remote_addr=self.request.META.get('REMOTE_ADDR'),
                      user_id=self.request.user.id)

        if ("from_date" in self.cleaned_data):
            from_date = self.cleaned_data["from_date"]
            election.voting_starts_at_date = from_date
            election.save()
            transaction.commit()

            start_election.apply_async(
                kwargs=kwargs,
                task_id=election.task_id(start_election),
                eta=election.voting_starts_at_date)

        if "to_date" in self.cleaned_data:
            to_date = self.cleaned_data["to_date"]
            election.voting_extended_until_date = election.voting_ends_at_date = to_date
            election.save()
            transaction.commit()
            end_election.apply_async(kwargs=kwargs,
                                     task_id=election.task_id(end_election),
                                     eta=election.voting_ends_at_date)

        return election
    def save(self, *args, **kwargs):
        election = super(ElectionEditForm, self).save(*args, **kwargs)

        election.questions = self.cleaned_data['questions']
        election.last_modified_at_date = timezone.now()

        # save so that in case a task is triggered, it has the correct questions
        # and last modified date
        election.save()

        if ("from_date" in self.cleaned_data) and ("to_date"
                                                   in self.cleaned_data):
            from_date = self.cleaned_data["from_date"]
            to_date = self.cleaned_data["to_date"]
            election.voting_starts_at_date = from_date
            election.voting_extended_until_date = election.voting_ends_at_date = to_date
            election.save()
            transaction.commit()

            kwargs = dict(election_id=election.id,
                          is_secure=self.request.is_secure(),
                          site_id=Site.objects.get_current().id,
                          remote_addr=self.request.META.get('REMOTE_ADDR'),
                          user_id=self.request.user.id)
            start_election.apply_async(
                kwargs=kwargs,
                task_id=election.task_id(start_election),
                eta=election.voting_starts_at_date)
            end_election.apply_async(kwargs=kwargs,
                                     task_id=election.task_id(end_election),
                                     eta=election.voting_ends_at_date)

        return election
Esempio n. 3
0
    def save(self, *args, **kwargs):
        election = super(ElectionEditForm, self).save(*args, **kwargs)

        election.questions = self.cleaned_data['questions']
        election.last_modified_at_date = timezone.now()

        # save so that in case a task is triggered, it has the correct questions
        # and last modified date
        election.save()

        if ("from_date" in self.cleaned_data) and ("to_date" in self.cleaned_data):
            from_date = self.cleaned_data["from_date"]
            to_date = self.cleaned_data["to_date"]
            election.voting_starts_at_date = from_date
            election.voting_extended_until_date = election.voting_ends_at_date = to_date
            election.save()
            transaction.commit()

            kwargs=dict(
                election_id=election.id,
                is_secure=self.request.is_secure(),
                site_id=Site.objects.get_current().id,
                remote_addr=self.request.META.get('REMOTE_ADDR'),
                user_id=self.request.user.id
            )
            start_election.apply_async(kwargs=kwargs, task_id=election.task_id(start_election),
                eta=election.voting_starts_at_date)
            end_election.apply_async(kwargs=kwargs, task_id=election.task_id(end_election),
                eta=election.voting_ends_at_date)

        return election
Esempio n. 4
0
    def save(self, *args, **kwargs):
        election = super(ElectionAdminForm, self).save(*args, **kwargs)

        # Questions/answers have a special formatting
        election.questions = self.cleaned_data['questions']
        election.last_modified_at_date = datetime.datetime.now()
        election.save()

        kwargs=dict(
            election_id=election.id,
            is_secure=self.request.is_secure(),
            site_id=Site.objects.get_current().id,
            remote_addr=self.request.META.get('REMOTE_ADDR'),
            user_id=self.request.user.id
        )

        if ("from_date" in self.cleaned_data):
            from_date = self.cleaned_data["from_date"]
            election.voting_starts_at_date = from_date
            election.save()
            transaction.commit()

            start_election.apply_async(kwargs=kwargs, task_id=election.task_id(start_election),
                eta=election.voting_starts_at_date)

        if "to_date" in self.cleaned_data:
            to_date = self.cleaned_data["to_date"]
            election.voting_extended_until_date = election.voting_ends_at_date = to_date
            election.save()
            transaction.commit()
            end_election.apply_async(kwargs=kwargs, task_id=election.task_id(end_election),
                eta=election.voting_ends_at_date)

        return election
Esempio n. 5
0
    def start_action(self, request, election, **kwargs):
        '''
        Starts an election
        '''

        tkwargs = dict(election_id=election.id,
                       is_secure=request.is_secure(),
                       site_id=Site.objects.get_current().id,
                       remote_addr=request.META.get('REMOTE_ADDR'),
                       user_id=request.user.id)

        election.last_modified_at_date = timezone.now()
        election.voting_starts_at_date = election.last_modified_at_date
        if not election.frozen_at_date:
            election.frozen_at_date = election.last_modified_at_date
        election.save()
        transaction.commit()

        start_election.apply_async(kwargs=tkwargs,
                                   task_id=election.task_id(start_election))
        return self.create_response(request, dict(status="success"))
Esempio n. 6
0
    def start_action(self, request, election, **kwargs):
        '''
        Starts an election
        '''

        tkwargs=dict(
            election_id=election.id,
            is_secure=request.is_secure(),
            site_id=Site.objects.get_current().id,
            remote_addr=request.META.get('REMOTE_ADDR'),
            user_id=request.user.id
        )

        election.last_modified_at_date = datetime.datetime.now()
        election.voting_starts_at_date = election.last_modified_at_date
        if not election.frozen_at_date:
            election.frozen_at_date = election.last_modified_at_date
        election.save()
        transaction.commit()

        start_election.apply_async(kwargs=tkwargs, task_id=election.task_id(start_election))
        return self.create_response(request, dict(status="success"))
Esempio n. 7
0
    def save(self, *args, **kwargs):
        import markdown
        import html2text
        from agora_site.agora_core.templatetags.string_tags import urlify_markdown
        from django.template.defaultfilters import truncatewords_html

        election = super(CreateElectionForm, self).save(commit=False)
        election.agora = self.agora
        election.create_name()
        election.uuid = str(uuid.uuid4())
        election.security_policy = self.cleaned_data['security_policy']
        election.created_at_date = timezone.now()
        if not election.is_secure():
            election.pubkey_created_at_date = election.created_at_date
        election.creator = self.request.user

        description_plaintext = html2text.html2text(election.description[:140]).strip()
        short_md = markdown.markdown(urlify_markdown(description_plaintext),
                                     safe_mode="escape", enable_attributes=False)
        election.short_description = truncatewords_html(short_md, 25)[:140]

        election.url = self.request.build_absolute_uri(reverse('election-view',
            kwargs=dict(username=election.agora.creator.username, agoraname=election.agora.name,
                electionname=election.name)))
        election.questions = self.cleaned_data['questions']
        election.election_type = election.questions[0]['tally_type']
        election.comments_policy = self.agora.comments_policy

        if ("from_date" in self.cleaned_data) and ("to_date" in self.cleaned_data):
            from_date = self.cleaned_data["from_date"]
            to_date = self.cleaned_data["to_date"]
            election.voting_starts_at_date = from_date
            election.voting_extended_until_date = election.voting_ends_at_date = to_date


        # Anyone can create a voting for a given agora, but if you're not the
        # admin, it must be approved
        if election.creator in election.agora.admins.all():
            election.is_approved = True
            election.approved_at_date = timezone.now()
        else:
            election.is_approved = False

        election.save()

        # create related action
        verb = 'created' if election.is_approved else 'proposed'
        actstream_action.send(self.request.user, verb=verb, action_object=election,
            target=election.agora, ipaddr=self.request.META.get('REMOTE_ADDR'),
            geolocation=json.dumps(geolocate_ip(self.request.META.get('REMOTE_ADDR'))))

        # send email to admins
        context = get_base_email_context(self.request)
        context.update(dict(
            election=election,
            action_user_url='/%s' % election.creator.username,
        ))

        for admin in election.agora.admins.all():
            context['to'] = admin

            if not admin.has_perms('receive_email_updates'):
                continue

            translation.activate(admin.get_profile().lang_code)

            email = EmailMultiAlternatives(
                subject=_('Election %s created') % election.pretty_name,
                body=render_to_string('agora_core/emails/election_created.txt',
                    context),
                to=[admin.email])

            email.attach_alternative(
                render_to_string('agora_core/emails/election_created.html',
                    context), "text/html")
            email.send()
            translation.deactivate()

        follow(self.request.user, election, actor_only=False, request=self.request)

        # used for tasks
        kwargs=dict(
            election_id=election.id,
            is_secure=self.request.is_secure(),
            site_id=Site.objects.get_current().id,
            remote_addr=self.request.META.get('REMOTE_ADDR'),
            user_id=self.request.user.id
        )

        # make the election available to celery
        transaction.commit()

        # send email to admins
        send_election_created_mails.apply_async(kwargs=kwargs, task_id=election.task_id(send_election_created_mails))

        # schedule start and end of the election. note that if election is not
        # approved, the start and end of the election won't really happen
        if from_date and to_date:
            start_election.apply_async(kwargs=kwargs, task_id=election.task_id(start_election),
                eta=election.voting_starts_at_date)
            end_election.apply_async(kwargs=kwargs, task_id=election.task_id(end_election),
                eta=election.voting_ends_at_date)

        return election
    def save(self, *args, **kwargs):
        import markdown
        from agora_site.agora_core.templatetags.string_tags import urlify_markdown
        from django.template.defaultfilters import truncatewords_html

        election = super(CreateElectionForm, self).save(commit=False)
        election.agora = self.agora
        election.create_name()
        election.uuid = str(uuid.uuid4())
        election.created_at_date = timezone.now()
        election.creator = self.request.user

        short_md = markdown.markdown(urlify_markdown(
            election.description[:140]),
                                     safe_mode="escape",
                                     enable_attributes=False)
        election.short_description = truncatewords_html(short_md, 25)[:140]

        election.url = self.request.build_absolute_uri(
            reverse('election-view',
                    kwargs=dict(username=election.agora.creator.username,
                                agoraname=election.agora.name,
                                electionname=election.name)))
        election.questions = self.cleaned_data['questions']
        election.election_type = election.questions[0]['tally_type']
        election.comments_policy = self.agora.comments_policy

        if ("from_date" in self.cleaned_data) and ("to_date"
                                                   in self.cleaned_data):
            from_date = self.cleaned_data["from_date"]
            to_date = self.cleaned_data["to_date"]
            election.voting_starts_at_date = from_date
            election.voting_extended_until_date = election.voting_ends_at_date = to_date

        # Anyone can create a voting for a given agora, but if you're not the
        # admin, it must be approved
        if election.creator in election.agora.admins.all():
            election.is_approved = True
            election.approved_at_date = timezone.now()
        else:
            election.is_approved = False

        election.save()

        # create related action
        verb = 'created' if election.is_approved else 'proposed'
        actstream_action.send(self.request.user,
                              verb=verb,
                              action_object=election,
                              target=election.agora,
                              ipaddr=self.request.META.get('REMOTE_ADDR'),
                              geolocation=json.dumps(
                                  geolocate_ip(
                                      self.request.META.get('REMOTE_ADDR'))))

        # send email to admins
        context = get_base_email_context(self.request)
        context.update(
            dict(
                election=election,
                action_user_url='/%s' % election.creator.username,
            ))

        for admin in election.agora.admins.all():
            context['to'] = admin

            if not admin.has_perms('receive_email_updates'):
                continue

            translation.activate(admin.get_profile().lang_code)

            email = EmailMultiAlternatives(
                subject=_('Election %s created') % election.pretty_name,
                body=render_to_string('agora_core/emails/election_created.txt',
                                      context),
                to=[admin.email])

            email.attach_alternative(
                render_to_string('agora_core/emails/election_created.html',
                                 context), "text/html")
            email.send()
            translation.deactivate()

        follow(self.request.user,
               election,
               actor_only=False,
               request=self.request)

        # used for tasks
        kwargs = dict(election_id=election.id,
                      is_secure=self.request.is_secure(),
                      site_id=Site.objects.get_current().id,
                      remote_addr=self.request.META.get('REMOTE_ADDR'),
                      user_id=self.request.user.id)

        # send email to admins
        send_election_created_mails.apply_async(
            kwargs=kwargs,
            task_id=election.task_id(send_election_created_mails))

        # schedule start and end of the election. note that if election is not
        # approved, the start and end of the election won't really happen
        if from_date and to_date:
            start_election.apply_async(
                kwargs=kwargs,
                task_id=election.task_id(start_election),
                eta=election.voting_starts_at_date)
            end_election.apply_async(kwargs=kwargs,
                                     task_id=election.task_id(end_election),
                                     eta=election.voting_ends_at_date)

        return election