from django.contrib.auth.models import AbstractUser from django.db import models from map.fields import CoordinateField class BaseModel(models.Model): class Meta: abstract = True created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Friend(AbstractUser): def get_display_name(self): display_name = super().get_full_name() if not display_name: display_name = self.username return display_name @property def _relatives_set(self): relative_set = set() for group in self.location_sharing_groups.all(): for relative in group.friends.all(): relative_set.add(relative) return relative_set @property def relatives(self): return list(self._relatives_set) @property def relatives_with_self(self): relatives = self._relatives_set relatives.add(self) return list(relatives) class FriendLocation(BaseModel): latitude = CoordinateField() longitude = CoordinateField() start_date = models.DateField('from', blank=True, null=True, help_text='Format : YYYY-MM-DD, example : 2018-05-31') end_date = models.DateField('until', blank=True, null=True, help_text='Format : YYYY-MM-DD, example : 2018-05-31') friend = models.OneToOneField(Friend, on_delete=models.CASCADE, related_name='location') @property def latitude_str(self): return str(self.latitude) @property def longitude_str(self): return str(self.longitude) def __str__(self): result = f'{self.friend.get_display_name()}' if self.start_date: result += f' from {self.start_date}' if self.end_date: result += f' until {self.end_date}' return result @property def safe_html(self): html = f'{self.friend.get_display_name()}' if self.start_date: html += f' from {self.start_date}' if self.end_date: html += f' until {self.end_date}' return html class LocationSharingGroup(models.Model): name = models.CharField(max_length=250) friends = models.ManyToManyField(Friend, related_name='location_sharing_groups', blank=True)