Skip to content

Commit

Permalink
Fixes #182 : add creation_date and last_modification_date to Petition…
Browse files Browse the repository at this point in the history
… model
  • Loading branch information
fallen committed Sep 2, 2020
1 parent 63c1645 commit 3c23125
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pytition/petition/migrations/0010_auto_20200902_1853.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 2.2.16 on 2020-09-02 16:53

from django.db import migrations, models
from django.utils import timezone

def populate_dates(apps, schema_editor):
Petition = apps.get_model('petition', 'Petition')
for petition in Petition.objects.all().iterator():
petition.creation_date = timezone.now()
petition.last_modification_date = timezone.now()
petition.save()

class Migration(migrations.Migration):

dependencies = [
('petition', '0009_auto_20200210_0948'),
]

operations = [
migrations.AddField(
model_name='petition',
name='creation_date',
field=models.DateTimeField(null=True, blank=True),
),
migrations.AddField(
model_name='petition',
name='last_modification_date',
field=models.DateTimeField(null=True, blank=True),
),
migrations.RunPython(populate_dates),
migrations.AlterField(
model_name='petition',
name='creation_date',
field=models.DateTimeField(blank=True),
),
migrations.AlterField(
model_name='petition',
name='last_modification_date',
field=models.DateTimeField(blank=True),
),
]
11 changes: 11 additions & 0 deletions pytition/petition/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.contrib.auth.hashers import get_hasher
from django.db import transaction
from django.urls import reverse
from django.utils import timezone

from tinymce import models as tinymce_models
from colorfield.fields import ColorField
Expand Down Expand Up @@ -204,6 +205,8 @@ class Petition(models.Model):
salt = models.TextField(blank=True)
paper_signatures = models.IntegerField(default=0)
paper_signatures_enabled = models.BooleanField(default=False)
creation_date = models.DateTimeField(blank=True)
last_modification_date = models.DateTimeField(blank=True)

def transfer_to(self, user=None, org=None):
if user is None and org is None:
Expand Down Expand Up @@ -411,6 +414,7 @@ def save(self, *args, **kwargs):
if not self.salt:
hasher = get_hasher()
self.salt = hasher.salt().decode('utf-8')
self.last_modification_date = timezone.now()
super(Petition, self).save(*args, **kwargs)


Expand Down Expand Up @@ -610,11 +614,18 @@ def create_user_profile(sender, instance, created, **kwargs):
def save_user_profile(sender, instance, **kwargs):
instance.pytitionuser.save()

@receiver(pre_save, sender=Petition)
def pre_save_petition(sender, instance, **kwargs):
if not instance.creation_date:
instance.creation_date = timezone.now()

@receiver(post_save, sender=Petition)
def save_petition(sender, instance, **kwargs):
if instance.slugmodel_set.count() == 0:
instance.slugify()
if kwargs['created']:
instance.creation_date = timezone.now()
instance.save()

@receiver(post_delete, sender=PytitionUser)
def post_delete_user(sender, instance, *args, **kwargs):
Expand Down

0 comments on commit 3c23125

Please sign in to comment.