[extensions-web/wip/api/v1] extensions: added rating and rated fields



commit da03788235583c7bc334e6d0ea7509ea76156ba2
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Sun Mar 14 20:04:03 2021 +0400

    extensions: added rating and rated fields

 .../extensions/migrations/0012_extension_rating.py | 59 ++++++++++++++++++++++
 sweettooth/extensions/models.py                    |  2 +
 sweettooth/extensions/serializers.py               |  2 +
 3 files changed, 63 insertions(+)
---
diff --git a/sweettooth/extensions/migrations/0012_extension_rating.py 
b/sweettooth/extensions/migrations/0012_extension_rating.py
new file mode 100644
index 00000000..31752770
--- /dev/null
+++ b/sweettooth/extensions/migrations/0012_extension_rating.py
@@ -0,0 +1,59 @@
+"""
+    GNOME Shell extensions repository
+    Copyright (C) 2021  Yuri Konotopov <ykonotopov gnome org>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+"""
+
+from django.db import migrations, models
+
+
+def populate_data(apps, schema_editor):
+    Extension = apps.get_model("extensions", "Extension")
+    RatingComment = apps.get_model("ratings", "RatingComment")
+    comments = (RatingComment.objects
+                             .filter(rating__gt=0)
+                             .values('object_pk')
+                             .annotate(
+                                rating_sum=models.Sum('rating'),
+                                rated=models.Count('object_pk'))
+                             .order_by('object_pk'))  # https://code.djangoproject.com/ticket/32546
+
+    for comment in comments:
+        if comment.get('rated') and comment.get('rating_sum'):
+            try:
+                extension = Extension.objects.get(pk=comment.get('object_pk'))
+            except Extension.DoesNotExist:
+                continue
+
+            extension.rated = comment.get('rated')
+            extension.rating = comment.get('rating_sum') / extension.rated
+
+        extension.save()
+
+
+def revert_data(apps, schema_editor):
+    pass
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ('extensions', '0011_extension_recommended'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='extension',
+            name='rated',
+            field=models.IntegerField(default=0),
+        ),
+        migrations.AddField(
+            model_name='extension',
+            name='rating',
+            field=models.FloatField(default=0),
+        ),
+        migrations.RunPython(populate_data, revert_data),
+    ]
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index f83a70c4..c1ee4812 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -117,6 +117,8 @@ class Extension(models.Model):
     downloads = models.PositiveIntegerField(default=0)
     popularity = models.IntegerField(default=0)
     recommended = models.BooleanField(default=False)
+    rating = models.FloatField(default=0)
+    rated = models.IntegerField(default=0)
 
     class Meta:
         permissions = (
diff --git a/sweettooth/extensions/serializers.py b/sweettooth/extensions/serializers.py
index 69173d3e..c69230de 100644
--- a/sweettooth/extensions/serializers.py
+++ b/sweettooth/extensions/serializers.py
@@ -47,4 +47,6 @@ class ExtensionSerializer(serializers.ModelSerializer):
             'popularity',
             'screenshot',
             'icon',
+            'rating',
+            'rated',
         ]


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