summaryrefslogtreecommitdiff
path: root/app/planner/models.py
blob: df162c768a2ef4f2e416a1266202259e8d737b59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
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)