public-health-ch/publichealth/home/models/snippets.py

136 lines
4.3 KiB
Python
Raw Normal View History

2017-04-08 12:57:00 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
2019-05-13 15:11:16 +00:00
from wagtail.snippets.models import register_snippet
2017-04-08 12:57:00 +00:00
2019-05-13 15:11:16 +00:00
from wagtail.core.models import Page
2019-10-17 15:42:27 +00:00
from wagtail.images.edit_handlers import ImageChooserPanel
2019-05-13 15:11:16 +00:00
from wagtail.admin.edit_handlers import FieldPanel, PageChooserPanel
2017-04-08 12:57:00 +00:00
2017-05-11 08:12:55 +00:00
from .forms import ContactForm
2017-04-08 12:57:00 +00:00
from ..util import TranslatedField
2017-05-05 10:07:50 +00:00
# List of supported social networks
SOCIAL_NETWORK_SUPPORTED = (
('twitter', 'Twitter'),
('facebook', 'Facebook'),
)
@register_snippet
class SocialContact(models.Model):
"""
Adds contact options through social networks
"""
network = models.CharField(max_length=16, default="twitter",
choices=SOCIAL_NETWORK_SUPPORTED)
profile = models.CharField(max_length=255, default="",
help_text="Name of the account, e.g. @myaccount, or full URL")
2019-06-14 20:02:15 +00:00
home_site = models.ForeignKey(
'wagtailcore.Site', null=True, blank=True,
on_delete=models.SET_NULL,
related_name='+')
2017-05-05 10:07:50 +00:00
panels = [
FieldPanel('network'),
FieldPanel('profile'),
2018-06-11 15:38:08 +00:00
FieldPanel('home_site'),
2017-05-05 10:07:50 +00:00
]
social_networks = dict(SOCIAL_NETWORK_SUPPORTED)
def network_title(self):
return self.social_networks[self.network]
def network_url(self):
if '://' in self.profile:
return self.profile
if self.network == 'twitter':
return "https://twitter.com/%s" % self.profile
elif self.network == 'facebook':
return "https://facebook.com/%s" % self.profile
return "#"
def __str__(self):
return "%s" % self.network
2017-04-08 12:57:00 +00:00
@register_snippet
class Contact(models.Model):
2017-05-05 10:07:50 +00:00
"""
Defines contact options for the organisation, usually shown in footer
"""
2017-04-08 12:57:00 +00:00
title = models.CharField(max_length=255, default="")
title_fr = models.CharField(max_length=255, default="")
2018-05-25 13:17:53 +00:00
title_en = models.CharField(max_length=255, default="")
2017-04-08 12:57:00 +00:00
trans_title = TranslatedField(
'title',
'title_fr',
2018-05-25 13:17:53 +00:00
'title_en',
2017-04-08 12:57:00 +00:00
)
2019-10-17 15:42:27 +00:00
2017-04-08 12:57:00 +00:00
address = models.TextField(default="", blank=True)
phone = models.CharField(max_length=40, blank=True, default="")
email = models.EmailField(max_length=100, blank=True, default="")
2017-04-08 12:57:00 +00:00
www = models.URLField(null=True, blank=True)
2017-05-11 08:12:55 +00:00
2019-10-21 18:24:16 +00:00
style = models.TextField(default="", blank=True)
2019-10-17 15:42:27 +00:00
color = models.CharField(max_length=40, blank=True, default="")
logo = models.ForeignKey(
'wagtailimages.Image',
null=True, blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
2017-05-10 14:40:06 +00:00
map_url = models.URLField(null=True, blank=True,
help_text="Optional link of address to mapping provider")
analytics = models.CharField(max_length=60, default="", blank=True,
help_text="Optional web analytics property code")
2017-04-08 12:57:00 +00:00
2017-05-11 08:12:55 +00:00
contact_form = models.ForeignKey(
'home.ContactForm',
null=True, blank=True,
on_delete=models.SET_NULL,
related_name='+',
)
2019-06-14 20:02:15 +00:00
home_site = models.ForeignKey(
'wagtailcore.Site',
null=True, blank=True,
on_delete=models.SET_NULL,
related_name='+',
)
2018-06-11 15:38:08 +00:00
2017-04-08 12:57:00 +00:00
panels = Page.content_panels + [
FieldPanel('title_fr'),
2018-05-25 13:17:53 +00:00
FieldPanel('title_en'),
2018-06-11 15:38:08 +00:00
FieldPanel('home_site'),
2017-04-08 12:57:00 +00:00
FieldPanel('address'),
FieldPanel('phone'),
FieldPanel('email'),
FieldPanel('www'),
2019-10-17 15:42:27 +00:00
ImageChooserPanel('logo'),
FieldPanel('color'),
2019-10-21 18:24:16 +00:00
FieldPanel('style'),
2017-05-10 14:40:06 +00:00
FieldPanel('map_url'),
FieldPanel('analytics'),
2017-05-11 08:12:55 +00:00
PageChooserPanel('contact_form', 'home.ContactForm'),
2017-04-08 12:57:00 +00:00
]
def phone_link(self):
return 'tel:%s' % self.phone.replace(' ', '')
def email_link(self):
return 'mailto:%s' % self.email
def www_domain(self):
return self.www.replace('http://', '').replace('https://', '')
2017-05-10 14:40:06 +00:00
def is_google_analytics(self):
return self.analytics.startswith('UA-')
def get_piwik_analytics(self):
# When formatted as "server|site_id", assume Piwik
if not '|' in self.analytics: return False
sa = self.analytics.split('|')
return { 'server': sa[0], 'site': sa[1] }
2017-04-11 15:32:31 +00:00
def trans_title_styled(self):
2017-04-11 21:38:59 +00:00
v = self.trans_title.split(' ')
2018-06-22 13:41:13 +00:00
if len(v) != 3: return self.trans_title
2017-04-11 15:32:31 +00:00
return "<strong>%s %s</strong> %s" % tuple(v)
2017-04-08 12:57:00 +00:00
def __str__(self):
return self.trans_title