From efc633591bbaea9427c91d05ae9bc71c5e09b026 Mon Sep 17 00:00:00 2001 From: Nick Ryzhkov Date: Thu, 26 Apr 2018 11:22:39 +0300 Subject: [PATCH] Append last online field to member model * last_online field * load message optimisation --- hornet/client.py | 4 ++++ hornet/models.py | 2 ++ 2 files changed, 6 insertions(+) diff --git a/hornet/client.py b/hornet/client.py index 90a5d7c..9a29c8b 100644 --- a/hornet/client.py +++ b/hornet/client.py @@ -59,6 +59,10 @@ def list_favorites(self, page_num, page_size): def list_message(self, member): logger.info("List messages with %s", member) + last_message = models.Message.objects.filter(member=member).order_by('-datetime').first() + if last_message and last_message.datetime > member.last_online: + logger.debug("Member was online before last message. Read from DB") + return models.Message.objects.order_by('-datetime').all() url = self.BASE_URL + "messages/" + member.network_id + "/conversation.json" logger.debug("Request url %s", url) response = self.session.get(url, params={"profile_id": member.network_id, diff --git a/hornet/models.py b/hornet/models.py index 883bfdb..802fa47 100644 --- a/hornet/models.py +++ b/hornet/models.py @@ -28,6 +28,7 @@ class Member(models.Model): username = models.CharField(max_length=FIELD_LENGTH, null=False, blank=False) age = models.IntegerField(null=True, blank=True) distance = models.FloatField(null=True, blank=True) + last_online = models.DateTimeField(null=True, blank=True) def __str__(self): return "member #{0.pk}:{0.network_id}: {0.name} (age {0.age}) at {0.distance}".format(self) @@ -47,6 +48,7 @@ def update(self, network_data): self.username = network_data['account']['username'] self.age = network_data['age'] self.distance = network_data.get('distance') + self.last_online = dateparse.parse_datetime(network_data['last_online']) @classmethod def get(cls, account, network_data):