Example #1
0
def petition_create(request):
    """
    Endpoint for creating a new petition.
    This requires the user be signed in.
    Note: This endpoint returns the ID of the petition
          created, in order for JavaScript to redirect
          to the correct petition.
    """
    # Build the user reference.
    user = request.user

    new_user_petitions = user.profile.petitions_created.filter(Q(status=0))
    if new_user_petitions.count() > 0:
        return HttpResponse(new_user_petitions.first().id)

    last_petition = Petition.objects.all().last()

    # Create a new blank petition.
    date = timezone.now()
    new_petition = Petition(
        title=PETITION_DEFAULT_TITLE,
        description=PETITION_DEFAULT_BODY,
        author=user,
        signatures=0,
        created_at=date,
        expires=date + timedelta(days=30),
        in_progress=False,
        id=last_petition.id + 1 if last_petition is not None else 0
    )
    new_petition.save()

    # Add the petition's ID to the user's created petitions list.
    petition_id = new_petition.id
    user.profile.petitions_created.add(petition_id)

    # Auto-sign the author to the petition.
    user.profile.petitions_signed.add(new_petition)
    user.save()
    new_petition.last_signed = timezone.now()
    new_petition.signatures = F('signatures') + 1
    new_petition.save()

    logger.info(
        "user " + user.email + " created a new petition called " + new_petition.title + " ID: " + str(new_petition.id))

    # Return the petition's ID to be used to redirect the user to the new petition.
    return HttpResponse(str(petition_id))
Example #2
0
    def generate_petitions(self, users, expired, unpub, removed, review, sigs,
                           num_petitions, resp):
        petitionlst = []
        self.stdout.write('Generating Petition objects')
        text = Text('en')
        try:
            with transaction.atomic():
                for x in range(0, num_petitions):
                    # Generate/Grab data
                    num_sentences = random.randint(1, 6)
                    title = text.sentence()[0:80]
                    description = text.text(num_sentences)
                    author = random.choice(users)
                    if sigs:
                        signatures = sigs.pop()
                    else:
                        signatures = random.randint(1, len(users))
                    created_at = timezone.now() - timedelta(
                        days=random.randint(0, 10))
                    status = 1
                    expires = created_at + timedelta(days=30)
                    last_signed = created_at + timedelta(
                        days=random.randint(0, 10))
                    petition = Petition()
                    if expired > 0:
                        created_at = timezone.now() - timedelta(days=31)
                        expires = created_at + timedelta(days=30)
                        last_signed = created_at + timedelta(
                            days=random.randint(1, 30))
                        expired -= 1
                    elif unpub > 0:
                        status = 0
                        unpub -= 1
                    elif removed > 0:
                        status = 2
                        removed -= 1
                    elif review > 0:
                        status = 3
                        review -= 1

                    petition.author = author
                    petition.title = title
                    petition.description = description
                    petition.signatures = signatures
                    petition.created_at = created_at
                    petition.status = status
                    petition.expires = expires
                    petition.last_signed = last_signed
                    if resp > 0:
                        petition.has_response = True
                        resp -= 1

                    petition.save()
                    petitionlst.append(petition)
            self.stdout.write(
                self.style.SUCCESS('Successfully created %s Petition objects' %
                                   num_petitions))
            return petitionlst
        except Exception as e:
            self.stdout.write(
                self.style.ERROR(
                    'Failed to generate %s Petition objects\n %s' %
                    (len(users), e)))
            print(traceback.format_exc())
            return None