Skip to content

Commit 3c23125

Browse files
committed
Fixes #182 : add creation_date and last_modification_date to Petition model
1 parent 63c1645 commit 3c23125

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Generated by Django 2.2.16 on 2020-09-02 16:53
2+
3+
from django.db import migrations, models
4+
from django.utils import timezone
5+
6+
def populate_dates(apps, schema_editor):
7+
Petition = apps.get_model('petition', 'Petition')
8+
for petition in Petition.objects.all().iterator():
9+
petition.creation_date = timezone.now()
10+
petition.last_modification_date = timezone.now()
11+
petition.save()
12+
13+
class Migration(migrations.Migration):
14+
15+
dependencies = [
16+
('petition', '0009_auto_20200210_0948'),
17+
]
18+
19+
operations = [
20+
migrations.AddField(
21+
model_name='petition',
22+
name='creation_date',
23+
field=models.DateTimeField(null=True, blank=True),
24+
),
25+
migrations.AddField(
26+
model_name='petition',
27+
name='last_modification_date',
28+
field=models.DateTimeField(null=True, blank=True),
29+
),
30+
migrations.RunPython(populate_dates),
31+
migrations.AlterField(
32+
model_name='petition',
33+
name='creation_date',
34+
field=models.DateTimeField(blank=True),
35+
),
36+
migrations.AlterField(
37+
model_name='petition',
38+
name='last_modification_date',
39+
field=models.DateTimeField(blank=True),
40+
),
41+
]

pytition/petition/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from django.contrib.auth.hashers import get_hasher
1111
from django.db import transaction
1212
from django.urls import reverse
13+
from django.utils import timezone
1314

1415
from tinymce import models as tinymce_models
1516
from colorfield.fields import ColorField
@@ -204,6 +205,8 @@ class Petition(models.Model):
204205
salt = models.TextField(blank=True)
205206
paper_signatures = models.IntegerField(default=0)
206207
paper_signatures_enabled = models.BooleanField(default=False)
208+
creation_date = models.DateTimeField(blank=True)
209+
last_modification_date = models.DateTimeField(blank=True)
207210

208211
def transfer_to(self, user=None, org=None):
209212
if user is None and org is None:
@@ -411,6 +414,7 @@ def save(self, *args, **kwargs):
411414
if not self.salt:
412415
hasher = get_hasher()
413416
self.salt = hasher.salt().decode('utf-8')
417+
self.last_modification_date = timezone.now()
414418
super(Petition, self).save(*args, **kwargs)
415419

416420

@@ -610,11 +614,18 @@ def create_user_profile(sender, instance, created, **kwargs):
610614
def save_user_profile(sender, instance, **kwargs):
611615
instance.pytitionuser.save()
612616

617+
@receiver(pre_save, sender=Petition)
618+
def pre_save_petition(sender, instance, **kwargs):
619+
if not instance.creation_date:
620+
instance.creation_date = timezone.now()
613621

614622
@receiver(post_save, sender=Petition)
615623
def save_petition(sender, instance, **kwargs):
616624
if instance.slugmodel_set.count() == 0:
617625
instance.slugify()
626+
if kwargs['created']:
627+
instance.creation_date = timezone.now()
628+
instance.save()
618629

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

0 commit comments

Comments
 (0)