1from django import forms
2from django.contrib.contenttypes.models import ContentType
3from django.contrib.postgres.forms import SimpleArrayField
4from django.utils.safestring import mark_safe
5
6from extras.models import *
7from extras.utils import FeatureQuery
8from utilities.forms import CSVContentTypeField, CSVModelForm, CSVMultipleContentTypeField, SlugField
9
10__all__ = (
11    'CustomFieldCSVForm',
12    'CustomLinkCSVForm',
13    'ExportTemplateCSVForm',
14    'TagCSVForm',
15    'WebhookCSVForm',
16)
17
18
19class CustomFieldCSVForm(CSVModelForm):
20    content_types = CSVMultipleContentTypeField(
21        queryset=ContentType.objects.all(),
22        limit_choices_to=FeatureQuery('custom_fields'),
23        help_text="One or more assigned object types"
24    )
25    choices = SimpleArrayField(
26        base_field=forms.CharField(),
27        required=False,
28        help_text='Comma-separated list of field choices'
29    )
30
31    class Meta:
32        model = CustomField
33        fields = (
34            'name', 'label', 'type', 'content_types', 'required', 'description', 'weight', 'filter_logic', 'default',
35            'choices', 'weight',
36        )
37
38
39class CustomLinkCSVForm(CSVModelForm):
40    content_type = CSVContentTypeField(
41        queryset=ContentType.objects.all(),
42        limit_choices_to=FeatureQuery('custom_links'),
43        help_text="Assigned object type"
44    )
45
46    class Meta:
47        model = CustomLink
48        fields = (
49            'name', 'content_type', 'weight', 'group_name', 'button_class', 'new_window', 'link_text', 'link_url',
50        )
51
52
53class ExportTemplateCSVForm(CSVModelForm):
54    content_type = CSVContentTypeField(
55        queryset=ContentType.objects.all(),
56        limit_choices_to=FeatureQuery('export_templates'),
57        help_text="Assigned object type"
58    )
59
60    class Meta:
61        model = ExportTemplate
62        fields = (
63            'name', 'content_type', 'description', 'mime_type', 'file_extension', 'as_attachment', 'template_code',
64        )
65
66
67class WebhookCSVForm(CSVModelForm):
68    content_types = CSVMultipleContentTypeField(
69        queryset=ContentType.objects.all(),
70        limit_choices_to=FeatureQuery('webhooks'),
71        help_text="One or more assigned object types"
72    )
73
74    class Meta:
75        model = Webhook
76        fields = (
77            'name', 'enabled', 'content_types', 'type_create', 'type_update', 'type_delete', 'payload_url',
78            'http_method', 'http_content_type', 'additional_headers', 'body_template', 'secret', 'ssl_verification',
79            'ca_file_path'
80        )
81
82
83class TagCSVForm(CSVModelForm):
84    slug = SlugField()
85
86    class Meta:
87        model = Tag
88        fields = ('name', 'slug', 'color', 'description')
89        help_texts = {
90            'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
91        }
92