import datetime import os from django.dispatch import receiver from django.contrib.gis.db import models from django.db.models.signals import post_save from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site from django.urls import reverse from django.utils.functional import cached_property from django.apps import apps from django.conf import settings from django.contrib.sitemaps import Sitemap from django import forms from django.template.defaultfilters import slugify from locations.models import Location, State from utils.util import render_images, render_products, parse_video, markdown_to_html, extract_main_image class Trip(models.Model): name = models.CharField(max_length=200) date = models.DateField() slug = models.SlugField(unique_for_date='date') def __str__(self): return self.name class Camp(models.Model): trip = models.ForeignKey(Trip, on_delete=models.SET_NULL, null=True, blank=True) name = models.CharField(max_length=200) slug = models.SlugField(blank=True, null=True) date_arrive = models.DateField('Arrival Date') date_depart = models.DateField('Departure Date', blank=True, null=True) distance_from_previous = models.FloatField(null=True, blank=True) link = models.CharField(max_length=200, null=True, blank=True) map_link = models.CharField(max_length=400, null=True, blank=True) point = models.PointField(null=True, blank=True) location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True) notes = models.TextField(null=True, blank=True) notes_html = models.TextField(null=True, blank=True) price = models.FloatField(null=True, blank=True) site_number = models.FloatField(null=True, blank=True) has_water = models.BooleanField(default=True) has_electric = models.BooleanField(default=True) has_dump = models.BooleanField(default=True) RATINGS = ( ('1', "1 Star"), ('2', "2 Stars"), ('3', "3 Stars"), ('4', "4 Stars"), ('5', "5 Stars"), ) rating = models.CharField(max_length=1, choices=RATINGS, null=True, blank=True) class Meta: ordering = ('date_arrive',) get_latest_by = 'date_arrive' def __str__(self): return self.name def get_featured_image_thumb(self): return self.featured_image.get_image_url_by_size("thumbnail") def get_content_type(self): return ContentType.objects.get(app_label="posts", model="post") @property def longitude(self): '''Get the site's longitude.''' if self.point: return self.point.x @property def latitude(self): '''Get the site's latitude.''' if self.point: return self.point.y @property def get_previous_published(self): return self.get_previous_by_date_arrive() @property def get_previous_admin_url(self): n = self.get_previous_by_date_arrive() return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[n.id] ) @property def get_next_published(self): return self.get_next_by_date_arrive() @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): created = self.pk is None if not self.slug: self.slug = slugify(self.name) self.notes_html = markdown_to_html(self.notes) super(Camp, self).save(*args, **kwargs) class CampIdea(models.Model): name = models.CharField(max_length=200) slug = models.SlugField(blank=True, null=True) point = models.PointField(null=True, blank=True) notes = models.TextField(null=True, blank=True) notes_html = models.TextField(null=True, blank=True) state = models.CharField(max_length=40) date_added = models.DateField(auto_now_add=True) class Meta: ordering = ('date_added',) get_latest_by = 'date_added' def __str__(self): return self.name def save(self, *args, **kwargs): created = self.pk is None if created: self.state = State.objects.filter(geometry__contains=self.point).get().name if not self.slug: self.slug = slugify(self.name) self.notes_html = markdown_to_html(self.notes) super(CampIdea, self).save(*args, **kwargs) @property def longitude(self): '''Get the site's longitude.''' if self.point: return self.point.x @property def latitude(self): '''Get the site's latitude.''' if self.point: return self.point.y