from django.db import models from django.template.defaultfilters import slugify from taggit.managers import TaggableManager from locations.models import Location from utils.widgets import markdown_to_html class Person(models.Model): first_name = models.CharField(max_length=200) last_name = models.CharField(max_length=200) email = models.EmailField(max_length=120, null=True, blank=True) street = models.CharField(max_length=355, null=True, blank=True) city = models.CharField(max_length=200, null=True, blank=True) state = models.CharField(max_length=200, null=True, blank=True) postal_code = models.CharField(max_length=20, null=True, blank=True) country = models.CharField(max_length=200, null=True) phone = models.CharField(max_length=22, blank=True, null=True) slug = models.CharField(max_length=50, blank=True) body_markdown = models.TextField(null=True, blank=True) body_html = models.TextField(null=True, blank=True) tags = TaggableManager(blank=True) location_met = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True) next_contact_date = models.DateField(null=True, blank=True) class Meta: ordering = ('-last_name',) def __str__(self): return "%s %s" %(self.first_name, self.last_name) def get_absolute_url(self): return reverse("people:detail", kwargs={"slug": self.slug}) @property def get_previous_admin_url(self): n = self.get_previous_by_pub_date() return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[n.id] ) @property def get_next_admin_url(self): model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name) try: return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[self.get_next_by_pub_date().pk] ) except model.DoesNotExist: return '' def save(self, *args, **kwargs): if not self.slug: self.slug = slugify(("%s-%s")[:50] %(self.last_name, self.first_name)) if self.body_markdown: self.body_html = markdown_to_html(self.body_markdown) super(Person, self).save()