1
Fork 0
This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
theorangeone.net-legacy/project/projects/models.py

48 lines
1.3 KiB
Python
Raw Permalink Normal View History

2016-11-25 23:12:40 +00:00
from django.db import models
from django.core.exceptions import ValidationError
2016-12-15 22:00:42 +00:00
from project.common.blocks import build_stream_field
2016-11-25 23:12:40 +00:00
from wagtail.wagtailcore.models import Page
from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel
2016-11-28 22:20:42 +00:00
from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
2016-11-25 23:12:40 +00:00
from wagtail.wagtailsearch import index
from project.common.models import Entity
ALLOWED_DOMAINS = [
'github'
]
def validate_url(value):
for part in ALLOWED_DOMAINS:
if part in value.lower():
return True
raise ValidationError('Invalid domain, should be in {}'.format(ALLOWED_DOMAINS))
class ProjectPage(Entity):
2016-11-28 22:20:42 +00:00
body = build_stream_field()
2016-11-28 20:25:40 +00:00
project_url = models.URLField(validators=[validate_url], blank=True)
download_url = models.URLField(validators=[validate_url], blank=True)
2016-11-25 23:12:40 +00:00
asset = models.ForeignKey(
'wagtaildocs.Document',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
2016-11-28 20:21:56 +00:00
def get_download_url(self):
return self.download_url or self.asset.url
2016-11-25 23:12:40 +00:00
search_fields = Page.search_fields + [
index.SearchField('body'),
]
content_panels = Page.content_panels + [
2016-11-28 22:20:42 +00:00
StreamFieldPanel('body'),
2016-11-25 23:12:40 +00:00
FieldPanel('download_url'),
2016-11-28 20:21:56 +00:00
FieldPanel('project_url'),
2016-11-25 23:12:40 +00:00
DocumentChooserPanel('asset')
]