2019-03-02 12:04:27 +01:00
|
|
|
from django.contrib.auth.models import AbstractUser
|
|
|
|
from django.db import models
|
|
|
|
|
2019-03-02 12:47:44 +01:00
|
|
|
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)
|
|
|
|
|
2019-03-02 12:04:27 +01:00
|
|
|
|
|
|
|
class Friend(AbstractUser):
|
2019-03-03 19:56:14 +01:00
|
|
|
shares_location_to = models.ManyToManyField('Friend', related_name='is_shared_location_by', blank=True)
|
2019-03-03 19:50:09 +01:00
|
|
|
|
2019-03-02 15:37:18 +01:00
|
|
|
def get_display_name(self):
|
|
|
|
display_name = super().get_full_name()
|
|
|
|
if not display_name:
|
|
|
|
display_name = self.username
|
|
|
|
|
|
|
|
return display_name
|
2019-03-02 12:04:27 +01:00
|
|
|
|
2019-03-03 20:14:00 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.get_display_name()
|
|
|
|
|
2019-03-02 12:04:27 +01:00
|
|
|
|
2019-03-02 12:47:44 +01:00
|
|
|
class FriendLocation(BaseModel):
|
|
|
|
latitude = CoordinateField()
|
|
|
|
longitude = CoordinateField()
|
2019-03-03 13:10:32 +01:00
|
|
|
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')
|
2019-03-02 15:56:46 +01:00
|
|
|
friend = models.OneToOneField(Friend, on_delete=models.CASCADE, related_name='location')
|
2019-03-02 12:47:44 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def latitude_str(self):
|
|
|
|
return str(self.latitude)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def longitude_str(self):
|
|
|
|
return str(self.longitude)
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-03-02 15:54:24 +01:00
|
|
|
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
|
2019-03-02 15:37:18 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def safe_html(self):
|
|
|
|
html = f'<strong>{self.friend.get_display_name()}</strong>'
|
|
|
|
if self.start_date:
|
|
|
|
html += f' from {self.start_date}'
|
|
|
|
if self.end_date:
|
|
|
|
html += f' until {self.end_date}'
|
|
|
|
return html
|