[extensions-web/wip/api/v1: 2/8] extensions: api: added updated property for extensions.




commit 6789f148426041dbefadae1c77b7fdb941ff6477
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Sun Nov 22 12:31:51 2020 +0400

    extensions: api: added updated property for extensions.

 sweettooth/extensions/documents.py                 |  1 +
 .../management/commands/define_updated_field.py    | 14 ++++++
 .../migrations/0011_auto_20200523_1243.py          | 52 ++++++++++++++++++++++
 sweettooth/extensions/models.py                    | 13 ++++++
 sweettooth/extensions/serializers.py               |  1 +
 sweettooth/extensions/views.py                     |  2 +-
 6 files changed, 82 insertions(+), 1 deletion(-)
---
diff --git a/sweettooth/extensions/documents.py b/sweettooth/extensions/documents.py
index a1ee7ee..b1f7db2 100644
--- a/sweettooth/extensions/documents.py
+++ b/sweettooth/extensions/documents.py
@@ -33,6 +33,7 @@ class ExtensionDocument(Document):
             'name',
             'description',
             'created',
+            'updated',
             'downloads',
             'popularity',
         ]
diff --git a/sweettooth/extensions/management/commands/define_updated_field.py 
b/sweettooth/extensions/management/commands/define_updated_field.py
new file mode 100644
index 0000000..3930b31
--- /dev/null
+++ b/sweettooth/extensions/management/commands/define_updated_field.py
@@ -0,0 +1,14 @@
+import os
+from django.core.management.base import BaseCommand, CommandError
+from sweettooth.extensions.models import ExtensionVersion
+
+class Command(BaseCommand):
+    args = ''
+    help = 'Replaces updated field of all extensions'
+
+    def handle(self, *args, **options):
+        for version in ExtensionVersion.objects.all():
+            # We don't have better choise than mtime now
+            version.created = os.path.getmtime(version.source.storage.path(version.source.name))
+
+        self.stdout.write('Successfully regenerated all metadata.json files\n')
diff --git a/sweettooth/extensions/migrations/0011_auto_20200523_1243.py 
b/sweettooth/extensions/migrations/0011_auto_20200523_1243.py
new file mode 100644
index 0000000..5ff8155
--- /dev/null
+++ b/sweettooth/extensions/migrations/0011_auto_20200523_1243.py
@@ -0,0 +1,52 @@
+# Generated by Django 2.2.12 on 2020-05-23 12:43
+
+import os
+
+from datetime import datetime
+
+from django.db import migrations, models
+
+def populate_data(apps, schema_editor):
+    Extension = apps.get_model("extensions", "Extension")
+    ExtensionVersion = apps.get_model("extensions", "ExtensionVersion")
+
+    Extension.objects.all().update(updated=models.F('created'))
+
+    for version in ExtensionVersion.objects.all():
+        # We don't have better choise than mtime now
+        try:
+            version.created = datetime.fromtimestamp(
+                os.path.getmtime(
+                    version.source.storage.path(version.source.name)
+                )
+            )
+            version.save()
+        except FileNotFoundError:
+            pass
+
+        if version.created and version.extension.updated < version.created:
+            version.extension.updated = version.created
+            version.extension.save()
+
+def revert_data(apps, schema_editor):
+    pass
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extensions', '0010_auto_20200511_1019'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='extension',
+            name='updated',
+            field=models.DateTimeField(default=None, null=True),
+        ),
+        migrations.AddField(
+            model_name='extensionversion',
+            name='created',
+            field=models.DateTimeField(auto_now_add=True, null=True),
+        ),
+        migrations.RunPython(populate_data, revert_data),
+    ]
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index a5a2e00..532d58b 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -113,6 +113,7 @@ class Extension(models.Model):
     description = models.TextField(blank=True)
     url = HttpURLField(blank=True)
     created = models.DateTimeField(auto_now_add=True)
+    updated = models.DateTimeField(default=None, null=True)
     downloads = models.PositiveIntegerField(default=0)
     popularity = models.IntegerField(default=0)
 
@@ -142,6 +143,17 @@ class Extension(models.Model):
             raise ValidationError("Your extension has an invalid UUID")
 
     def save(self, *args, **kwargs):
+        versions = self.visible_versions.order_by('-version')
+        self.updated = (
+            versions[0].created
+            if len(versions) > 0
+            else (
+                self.created
+                if self.created
+                else datetime.now()
+            )
+        )
+
         super().save(*args, **kwargs)
 
     def get_absolute_url(self):
@@ -344,6 +356,7 @@ class ExtensionVersion(models.Model):
     extra_json_fields = models.TextField()
     status = models.PositiveIntegerField(choices=STATUSES.items())
     shell_versions = models.ManyToManyField(ShellVersion)
+    created = models.DateTimeField(auto_now_add=True, null=True)
 
     class Meta:
         unique_together = ('extension', 'version'),
diff --git a/sweettooth/extensions/serializers.py b/sweettooth/extensions/serializers.py
index 6adf4e0..69173d3 100644
--- a/sweettooth/extensions/serializers.py
+++ b/sweettooth/extensions/serializers.py
@@ -42,6 +42,7 @@ class ExtensionSerializer(serializers.ModelSerializer):
             'creator',
             'description',
             'created',
+            'updated',
             'downloads',
             'popularity',
             'screenshot',
diff --git a/sweettooth/extensions/views.py b/sweettooth/extensions/views.py
index 3d06154..d74729c 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -63,7 +63,7 @@ class ExtensionsViewSet(mixins.ListModelMixin,
     serializer_class = serializers.ExtensionSerializer
     pagination_class = ExtensionsPagination
     filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
-    ordering_fields = ['created', 'downloads', 'popularity', '?']
+    ordering_fields = ['created', 'updated', 'downloads', 'popularity', '?']
     page_size = 25
     page_size_query_param = 'page_size'
     max_page_size = 100


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]