[extensions-web/wip/api/v1: 16/19] extensions: api: added updated property for extensions.




commit dda6ec013b1b38d48287c1197934f089192e5d57
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/0008_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 a1ee7ee7..b1f7db2d 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 00000000..3930b316
--- /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/0008_auto_20200523_1243.py 
b/sweettooth/extensions/migrations/0008_auto_20200523_1243.py
new file mode 100644
index 00000000..f4194984
--- /dev/null
+++ b/sweettooth/extensions/migrations/0008_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', '0007_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(default=None, null=True),
+        ),
+        migrations.RunPython(populate_data, revert_data),
+    ]
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 07213a34..7dce75a1 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -111,6 +111,7 @@ class Extension(models.Model):
     description = models.TextField(blank=True)
     url = models.URLField(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)
 
@@ -140,6 +141,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):
@@ -336,6 +348,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 6adf4e00..69173d3e 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 38e43416..be87609d 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -62,7 +62,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]