2020-03-07 06:56:44 +00:00
|
|
|
''' base model with default fields '''
|
2020-02-18 00:50:44 +00:00
|
|
|
from django.db import models
|
2020-05-13 01:56:28 +00:00
|
|
|
from django.dispatch import receiver
|
2020-02-18 00:50:44 +00:00
|
|
|
|
|
|
|
from fedireads.settings import DOMAIN
|
|
|
|
|
|
|
|
class FedireadsModel(models.Model):
|
2020-03-28 02:52:05 +00:00
|
|
|
''' fields and functions for every model '''
|
2020-02-18 00:50:44 +00:00
|
|
|
created_date = models.DateTimeField(auto_now_add=True)
|
2020-02-21 01:33:50 +00:00
|
|
|
updated_date = models.DateTimeField(auto_now=True)
|
2020-05-13 01:56:28 +00:00
|
|
|
remote_id = models.CharField(max_length=255, null=True)
|
2020-02-18 00:50:44 +00:00
|
|
|
|
2020-05-13 01:56:28 +00:00
|
|
|
def get_remote_id(self):
|
|
|
|
''' generate a url that resolves to the local object '''
|
2020-02-18 00:50:44 +00:00
|
|
|
base_path = 'https://%s' % DOMAIN
|
2020-02-21 02:01:50 +00:00
|
|
|
if hasattr(self, 'user'):
|
2020-05-13 01:56:28 +00:00
|
|
|
base_path = self.user.remote_id
|
2020-02-18 01:53:40 +00:00
|
|
|
model_name = type(self).__name__.lower()
|
2020-02-18 00:50:44 +00:00
|
|
|
return '%s/%s/%d' % (base_path, model_name, self.id)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2020-05-13 01:56:28 +00:00
|
|
|
|
|
|
|
|
2020-05-14 01:23:54 +00:00
|
|
|
@receiver(models.signals.post_save)
|
2020-05-13 01:56:28 +00:00
|
|
|
def execute_after_save(sender, instance, created, *args, **kwargs):
|
|
|
|
''' set the remote_id after save (when the id is available) '''
|
2020-05-14 01:23:54 +00:00
|
|
|
if not created or not hasattr(instance, 'get_remote_id'):
|
2020-05-13 01:56:28 +00:00
|
|
|
return
|
2020-05-14 18:28:45 +00:00
|
|
|
if not instance.remote_id:
|
|
|
|
instance.remote_id = instance.get_remote_id()
|
|
|
|
instance.save()
|