[extensions-web/wip/api/v1: 22/28] extensions: api: added updated property for extensions.
- From: Yuri Konotopov <ykonotopov src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [extensions-web/wip/api/v1: 22/28] extensions: api: added updated property for extensions.
- Date: Sun, 29 May 2022 13:20:38 +0000 (UTC)
commit 1f050a3aae54209a05dc37687221d7e1ef3eb3d1
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/0012_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/0012_auto_20200523_1243.py
b/sweettooth/extensions/migrations/0012_auto_20200523_1243.py
new file mode 100644
index 0000000..4e4974b
--- /dev/null
+++ b/sweettooth/extensions/migrations/0012_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', '0011_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 c167627..b420edd 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -127,6 +127,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)
@@ -156,6 +157,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):
@@ -367,6 +379,7 @@ class ExtensionVersion(models.Model):
status = models.PositiveIntegerField(choices=STATUSES.items())
shell_versions = models.ManyToManyField(ShellVersion)
session_modes = models.ManyToManyField(SessionMode)
+ 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 7dbb28f..c151b1a 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]