1
0
Fork 0
mirror of https://github.com/Findus23/acronomy.git synced 2024-09-19 15:33:45 +02:00
acronomy/acros/forms.py

88 lines
2.1 KiB
Python
Raw Permalink Normal View History

2020-06-01 11:03:21 +02:00
from django.core.exceptions import ValidationError
from django.forms import ModelForm, TextInput, CharField
2020-07-08 22:01:43 +02:00
from acros.models import Acronym, Tag, WikipediaLink, PaperReference, Weblink
2020-06-01 20:26:00 +02:00
from acros.utils.tags import parse_tags, edit_string_for_tags
2020-06-01 11:03:21 +02:00
class TagWidget(TextInput):
def format_value(self, value):
if value is not None and not isinstance(value, str):
value = edit_string_for_tags(value)
return super().format_value(value)
2020-06-08 20:33:29 +02:00
def __init__(self, *args, **kwargs):
super(TagWidget, self).__init__(*args, **kwargs)
attrs = {"class": "taginput"}
self.attrs.update(attrs)
2020-06-01 11:03:21 +02:00
class TagField(CharField):
"""
based on https://github.com/jazzband/django-taggit/blob/master/taggit/models.py
"""
widget = TagWidget
def clean(self, value):
value = super().clean(value)
try:
tag_strings = parse_tags(value)
tag_ids = []
for tag in tag_strings:
try:
to = Tag.objects.get(name__iexact=tag)
except Tag.DoesNotExist:
to = Tag(name=tag)
to.save()
tag_ids.append(to.pk)
return tag_ids
except ValueError:
raise ValidationError(
"Please provide a comma-separated list of tags."
)
class EditForm(ModelForm):
tags = TagField()
class Meta:
model = Acronym
2020-07-18 19:21:01 +02:00
fields = ['name', 'full_name', "description_md", "stub", "tags"]
2020-06-01 11:03:21 +02:00
2020-07-19 21:38:09 +02:00
class EditLetterForm(ModelForm):
class Meta:
model = Acronym
fields = ["acro_letters"]
2020-06-01 11:03:21 +02:00
class AddForm(ModelForm):
tags = TagField()
class Meta:
model = Acronym
2020-07-18 19:21:01 +02:00
fields = ['name', 'full_name', "description_md", "stub", "tags"]
2020-06-08 18:45:38 +02:00
2020-06-08 20:33:29 +02:00
2020-07-08 22:01:43 +02:00
class WikipediaForm(ModelForm):
class Meta:
model = WikipediaLink
fields = ['title']
2020-06-08 18:45:38 +02:00
2020-07-08 22:01:43 +02:00
class PaperForm(ModelForm):
2020-06-08 18:45:38 +02:00
class Meta:
2020-07-08 22:01:43 +02:00
model = PaperReference
2020-06-08 18:45:38 +02:00
2020-07-08 22:01:43 +02:00
fields = ['bibcode']
class WeblinkForm(ModelForm):
class Meta:
model = Weblink
fields = ['url']