Parcourir la source

Add an expiration date to contribs

And auto-set it to 1 year after contrib submission.

This commit do not enforce removal on expiration.

ref #27
Jocelyn Delalande il y a 7 ans
Parent
commit
eaf7398e66

+ 20 - 0
wifiwithme/apps/contribmap/migrations/0015_contrib_expiration_date.py

@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.13 on 2017-07-31 14:51
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('contribmap', '0014_auto_20160515_1050'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='contrib',
+            name='expiration_date',
+            field=models.DateTimeField(null=True),
+        ),
+    ]

+ 24 - 0
wifiwithme/apps/contribmap/migrations/0016_set_expiration_date.py

@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.13 on 2017-08-01 15:51
+from __future__ import unicode_literals
+
+from django.db import migrations
+from contribmap.utils import add_one_year
+
+
+
+def add_missing_expiration_date(apps, schema_editor):
+    Contrib = apps.get_model('contribmap', 'contrib')
+    for contrib in Contrib.objects.filter(expiration_date=None):
+        contrib.expiration_date = add_one_year(contrib.date)
+        contrib.save()
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('contribmap', '0015_contrib_expiration_date'),
+    ]
+
+    operations = [
+        migrations.RunPython(add_missing_expiration_date),
+    ]

+ 20 - 0
wifiwithme/apps/contribmap/migrations/0017_auto_20170801_1610.py

@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.9.13 on 2017-08-01 16:10
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('contribmap', '0016_set_expiration_date'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='contrib',
+            name='expiration_date',
+            field=models.DateTimeField(blank=True, null=True),
+        ),
+    ]

+ 27 - 1
wifiwithme/apps/contribmap/models.py

@@ -3,9 +3,10 @@
 from __future__ import unicode_literals
 
 from django.db import models
+from django.utils import timezone
 
 from .fields import CommaSeparatedCharField
-from .utils import ANGLES, merge_intervals
+from .utils import add_one_year, ANGLES, merge_intervals
 
 
 class Contrib(models.Model):
@@ -66,6 +67,9 @@ class Contrib(models.Model):
         'commentaire public',
         default=False)
     date = models.DateTimeField(auto_now_add=True)
+    expiration_date = models.DateTimeField(
+        "date d'expiration",
+        null=True, blank=True)
 
     STATUS_TOSTUDY = 'TOSTUDY'
     STATUS_TOCONNECT = 'TOCONNECT'
@@ -122,6 +126,28 @@ class Contrib(models.Model):
         angles.sort(key=lambda i: i[0])  # sort by x
         return merge_intervals(angles)
 
+    def get_postponed_expiration_date(self, from_date):
+        """ Computes the new expiration date
+
+        :param from_date: reference datetime frow where we add our extra delay.
+        """
+        return add_one_year(from_date)
+
+    def clean(self):
+        # usefull only for data imported from bottle version
+        if not self.date:
+            self.date = timezone.now()
+        if not self.expiration_date:
+            self.expiration_date = self.get_postponed_expiration_date(
+                self.date)
+
+    def save(self, *args, **kwargs):
+        if not self.pk:  # New instance
+            self.date = timezone.now()
+            self.expiration_date = self.get_postponed_expiration_date(
+                self.date)
+        super().save(*args, **kwargs)
+
     def is_public(self):
         return self.privacy_coordinates
 

+ 17 - 0
wifiwithme/apps/contribmap/utils.py

@@ -1,3 +1,6 @@
+import datetime
+from django.utils import timezone
+
 ANGLES = {
     'N': (-23, 22),
     'NO': (292, 337),
@@ -35,3 +38,17 @@ def merge_intervals(l, wrap=360):
         result[-1][1] = max(result[-1][1], first[1] + wrap)
         result.pop(0)
     return result
+
+
+def add_one_year(date):
+    new_day, new_month, new_year = date.day, date.month, date.year + 1
+
+    try:
+        new_date = timezone.make_aware(
+            datetime.datetime(new_year, new_month, new_day))
+    except ValueError:  # Hello 29/3
+        new_day -= 1
+        new_date = timezone.make_aware(
+            datetime.datetime(new_year, new_month, new_day))
+
+    return new_date