@@ -1,73 +1,73 | |||
|
1 | 1 | from django import forms |
|
2 | 2 | from .models import ABSConfiguration, ABSBeam |
|
3 | 3 | from .widgets import UpDataWidget, DownDataWidget, EditUpDataWidget, EditDownDataWidget |
|
4 | 4 | from apps.main.models import Configuration |
|
5 | 5 | import os |
|
6 | 6 | |
|
7 | 7 | class ABSConfigurationForm(forms.ModelForm): |
|
8 | 8 | def __init__(self, *args, **kwargs): |
|
9 | 9 | super(ABSConfigurationForm, self).__init__(*args, **kwargs) |
|
10 | 10 | |
|
11 | 11 | class Meta: |
|
12 | 12 | model = ABSConfiguration |
|
13 | exclude = ('type', 'status', 'parameters', 'active_beam', 'module_status', 'module_messages') | |
|
13 | exclude = ('type', 'status', 'parameters', 'active_beam', 'module_status', 'module_messages', 'module_mode') | |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | class ABSBeamAddForm(forms.Form): |
|
17 | 17 | |
|
18 | 18 | up_data = forms.CharField(widget=UpDataWidget, label='') |
|
19 | 19 | down_data = forms.CharField(widget=DownDataWidget, label='') |
|
20 | 20 | |
|
21 | 21 | def __init__(self, *args, **kwargs): |
|
22 | 22 | super(ABSBeamAddForm, self).__init__(*args, **kwargs) |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | |
|
26 | 26 | class ABSBeamEditForm(forms.Form): |
|
27 | 27 | |
|
28 | 28 | up_data = forms.CharField(widget=EditUpDataWidget, label='') |
|
29 | 29 | down_data = forms.CharField(widget=EditDownDataWidget, label='') |
|
30 | 30 | |
|
31 | 31 | def __init__(self, *args, **kwargs): |
|
32 | 32 | super(ABSBeamEditForm, self).__init__(*args, **kwargs) |
|
33 | 33 | |
|
34 | 34 | if 'initial' in kwargs: |
|
35 | 35 | if 'beam' in self.initial: |
|
36 | 36 | self.fields['up_data'].initial = self.initial['beam'] |
|
37 | 37 | self.fields['down_data'].initial = self.initial['beam'] |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | class ExtFileField(forms.FileField): |
|
41 | 41 | """ |
|
42 | 42 | Same as forms.FileField, but you can specify a file extension whitelist. |
|
43 | 43 | |
|
44 | 44 | >>> from django.core.files.uploadedfile import SimpleUploadedFile |
|
45 | 45 | >>> |
|
46 | 46 | >>> t = ExtFileField(ext_whitelist=(".pdf", ".txt")) |
|
47 | 47 | >>> |
|
48 | 48 | >>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content')) |
|
49 | 49 | >>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content')) |
|
50 | 50 | >>> |
|
51 | 51 | >>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content')) |
|
52 | 52 | Traceback (most recent call last): |
|
53 | 53 | ... |
|
54 | 54 | ValidationError: [u'Not allowed filetype!'] |
|
55 | 55 | """ |
|
56 | 56 | def __init__(self, *args, **kwargs): |
|
57 | 57 | extensions = kwargs.pop("extensions") |
|
58 | 58 | self.extensions = [i.lower() for i in extensions] |
|
59 | 59 | |
|
60 | 60 | super(ExtFileField, self).__init__(*args, **kwargs) |
|
61 | 61 | |
|
62 | 62 | def clean(self, *args, **kwargs): |
|
63 | 63 | data = super(ExtFileField, self).clean(*args, **kwargs) |
|
64 | 64 | filename = data.name |
|
65 | 65 | ext = os.path.splitext(filename)[1] |
|
66 | 66 | ext = ext.lower() |
|
67 | 67 | if ext not in self.extensions: |
|
68 | 68 | raise forms.ValidationError('Not allowed file type: %s' % ext) |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | class ABSImportForm(forms.Form): |
|
72 | 72 | |
|
73 | 73 | file_name = ExtFileField(extensions=['.json']) |
General Comments 0
You need to be logged in to leave comments.
Login now