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




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

    extensions: added rating and rated fields

 .../extensions/migrations/0014_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/0014_extension_rating.py 
b/sweettooth/extensions/migrations/0014_extension_rating.py
new file mode 100644
index 0000000..f4f0e49
--- /dev/null
+++ b/sweettooth/extensions/migrations/0014_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', '0013_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 bcd0ee7..0db1345 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -131,6 +131,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 69173d3..c69230d 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]