From e4d9fdfacca33dc871995ab931c0fa450b15be8f Mon Sep 17 00:00:00 2001
From: Jarmil <jarmil.halamicek@seznam.cz>
Date: Tue, 20 Oct 2020 05:25:35 +0200
Subject: [PATCH] Napojeni na medailonky

---
 .../migrations/0011_personpage_profile_id.py  | 27 ++++++++++++++++
 shared/models.py                              | 31 +++++++++++++------
 2 files changed, 49 insertions(+), 9 deletions(-)
 create mode 100644 shared/migrations/0011_personpage_profile_id.py

diff --git a/shared/migrations/0011_personpage_profile_id.py b/shared/migrations/0011_personpage_profile_id.py
new file mode 100644
index 00000000..3af17dd3
--- /dev/null
+++ b/shared/migrations/0011_personpage_profile_id.py
@@ -0,0 +1,27 @@
+# Generated by Django 3.1.1 on 2020-10-20 03:14
+
+import django.core.validators
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("shared", "0010_article_author"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="personpage",
+            name="profile_id",
+            field=models.IntegerField(
+                blank=True,
+                default=0,
+                validators=[
+                    django.core.validators.MinValueValidator(0),
+                    django.core.validators.MaxValueValidator(2),
+                ],
+                verbose_name="ÄŚĂ­slo medailonku (0..2) z lide.pirati.cz",
+            ),
+        ),
+    ]
diff --git a/shared/models.py b/shared/models.py
index 0435c2c7..82ead2be 100644
--- a/shared/models.py
+++ b/shared/models.py
@@ -2,6 +2,7 @@ import random
 
 import requests
 import requests_cache
+from django.core.validators import MaxValueValidator, MinValueValidator
 from django.db import models
 from modelcluster.contrib.taggit import ClusterTaggableManager
 from modelcluster.fields import ParentalKey
@@ -81,11 +82,18 @@ class PersonPage(SharedSubpageMixin, MetadataPageMixin, Page):
 
     # ve formatu iapi: https://iapi.pirati.cz/v1/user?username=john.doe
     pirate_id = models.TextField("Pirate ID osoby", blank=False)
+    profile_id = models.IntegerField(
+        "ÄŚĂ­slo medailonku (0..2) z lide.pirati.cz",
+        blank=True,
+        default=0,
+        validators=[MinValueValidator(0), MaxValueValidator(2)],
+    )
 
     perex = models.TextField("Perex osoby", blank=True)
 
     content_panels = Page.content_panels + [
         FieldPanel("pirate_id"),
+        FieldPanel("profile_id"),
         FieldPanel("perex"),
     ]
 
@@ -95,33 +103,38 @@ class PersonPage(SharedSubpageMixin, MetadataPageMixin, Page):
             "https://iapi.pirati.cz/v1/user?username=%s" % self.pirate_id
         ).json()
 
-    def _graph(self):
-        """ Vrati data o osobe z graph.pirati.cz (forum) jako json. Cached via requests_cache """
-        return requests.get("https://graph.pirati.cz/user/%s" % self.pirate_id).json()
-
     @property
     def name(self):
         return self._iapi().get("displayname")
 
     @property
     def email(self):
-        return self._graph().get("email")
+        try:
+            return self._iapi()["profiles"][self.profile_id]["email"]
+        except (IndexError, KeyError):
+            return None
 
     @property
     def phone(self):
-        return None  # TODO
+        try:
+            return self._iapi()["profiles"][self.profile_id]["phone"]
+        except (IndexError, KeyError):
+            return None
 
     @property
     def longtext(self):  # medailonek
-        return "Lorem ipsum dolor amet"  # TODO
+        return ""  # TODO
 
     @property
     def portrait(self):  # profilova fotka
-        return "https://lide.pirati.cz/media/person/61/profile/1312/2.jpg"  # TODO
+        try:
+            return self._iapi()["profiles"][self.profile_id]["photo"]
+        except (IndexError, KeyError):
+            return None
 
     @property
     def facebook(self):
-        return "https://www.facebook.com/ales.fuksa.9"  # TODO
+        return None  # TODO
 
     class Meta:
         verbose_name = "Člověk"  # to zni hrde
-- 
GitLab