class Meta: model = Project fields = [ 'title', 'looking_for_members', 'aws_resources', 'description', 'bounty', 'technologies', 'team', 'image', 'inspiration', 'what_it_does', 'how_it_was_built', 'challenges', 'accomplishments', 'learned', 'whats_next' ] team = AutoCompleteSelectMultipleField( lookup_class=UserLookup, widget=AutoCompleteSelectMultipleWidget(lookup_class=UserLookup, limit=10), label="Select a team member") bounty = AutoCompleteSelectField(lookup_class=BountyLookup, widget=AutoCompleteSelectWidget( lookup_class=BountyLookup, limit=10), label="Select a team member") widgets = { 'team': AutoCompleteSelectMultipleWidget(lookup_class=UserLookup, limit=10), 'bounty': AutoCompleteSelectWidget(lookup_class=BountyLookup, limit=10) }
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['project_manager'].widget = AutoCompleteSelectMultipleWidget( lookup_class=HAWCUserLookup) self.fields['team_members'].widget = AutoCompleteSelectMultipleWidget( lookup_class=HAWCUserLookup) self.fields['reviewers'].widget = AutoCompleteSelectMultipleWidget( lookup_class=HAWCUserLookup) self.helper = self.setHelper()
def get_form(self, *args, **kwargs): form = super(ProjectCreate, self).get_form(*args, **kwargs) form.fields['team'].widget = AutoCompleteSelectMultipleWidget(lookup_class=UserLookup, limit=10) form.fields['team'].widget.update_query_parameters({'hackathon': self._get_current_hackathon()}) return form
class CampaignAdminForm(forms.ModelForm): managers = AutoCompleteSelectMultipleField( lookup_class=OwnerLookup, widget=AutoCompleteSelectMultipleWidget(lookup_class=OwnerLookup), required=True, ) class Meta(object): model = models.Campaign fields = ( 'managers', 'name', 'description', 'details', 'license', 'paypal_receiver', 'status', 'type', 'email', 'do_watermark', 'use_add_ask', 'charitable', )
class EditManagersForm(forms.ModelForm): managers = AutoCompleteSelectMultipleField( OwnerLookup, label='Campaign Managers', widget=AutoCompleteSelectMultipleWidget(OwnerLookup), required=True, error_messages = {'required': "You must have at least one manager for a campaign."}, ) class Meta: model = Campaign fields = ('id', 'managers') widgets = { 'id': forms.HiddenInput }
class OpenCampaignForm(forms.ModelForm): managers = AutoCompleteSelectMultipleField( OwnerLookup, label='Campaign Managers', widget=AutoCompleteSelectMultipleWidget(OwnerLookup), required=True, error_messages = {'required': "You must have at least one manager for a campaign."}, ) userid = forms.IntegerField( required = True, widget = forms.HiddenInput ) class Meta: model = Campaign fields = 'name', 'work', 'managers', 'type' widgets = { 'work': forms.HiddenInput, "name": forms.HiddenInput, }
class CbArticleAdminForm(forms.ModelForm): def __init__(self, *args, **kwargs): # self.request = kwargs.pop("request") super(CbArticleAdminForm, self).__init__(*args, **kwargs) CATEGORIES = [("", "Select category")] for c in CbCategory.objects.only("name"): CATEGORIES.append((c.id, c.name)) self.fields["category"].choices = CATEGORIES self.fields['attached_tags'].widget.attrs['readonly'] = True if self.instance.pk: tags = [] for t in self.instance.article_tags.all(): tags.append(t.tag.name) self.fields["attached_tags"].initial = ", ".join(tags) users = [("", "Select user")] # self.fields["owner"] = self.request.user.id for c in User.objects.filter(is_superuser=True, is_staff=True, is_active=True): users.append((c.id, c)) self.fields["user"].choices = users title = forms.CharField(label="Title *",max_length=255,widget=forms.TextInput(attrs={"style":"width: 300px;","autocomplete":"off"})) # category = forms.Select(choices=CbCategory.objects.only("name")) category = forms.ChoiceField(label="Category *") image = forms.ImageField(required=False,label="Image (max size 2MB, recommended dimension 1200 x 435) ") content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 600,"class":"tinymce"}),label="Content *") # user = forms.Select(choices=User.objects.filter(is_superuser=True,is_staff=True)) user = forms.Select(choices=[]) meta_data = forms.CharField(required=False,widget=forms.Textarea(attrs={"class": "mceNoEditor"})) is_visible = forms.BooleanField(required=False) attached_tags = forms.CharField(widget=forms.Textarea, required=False) tag = forms.CharField( label='Type tag name', widget=AutoCompleteSelectMultipleWidget(TagLookup), required=False, ) def clean_category(self): if not CbCategory.objects.filter(pk = self.cleaned_data.get("category")).exists(): raise forms.ValidationError("Category does not exist") return self.cleaned_data.get("category") else: return CbCategory.objects.get(pk=self.cleaned_data.get("category")) def clean_image(self): image = self.cleaned_data.get('image', False) if image: img = Image.open(image) w, h = img.size # validate dimensions max_width = 1200 max_height = 435 if w > max_width or h > max_height: raise forms.ValidationError( _('Please use an image that is smaller or equal to ' '%s x %s pixels.' % (max_width, max_height))) print(image.name) # validate content type fileName, fileExtension = os.path.splitext(image.name) print(fileExtension) if not fileExtension in ['.jpeg', '.pjpeg', '.png', '.jpg']: raise forms.ValidationError(_('Please use a JPEG or PNG image.')) # validate file size if len(image) > (2 * 1024 * 1024): raise forms.ValidationError(_('Image file too large ( maximum 2mb )')) # else: # raise forms.ValidationError(_("Couldn't read uploaded image")) return image class Meta: model = CbArticle fields = ("title","category","image","content","user","is_visible","tag","attached_tags")
class CbTopicAdminForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(CbTopicAdminForm, self).__init__(*args, **kwargs) CATEGORIES = [("", "Select category")] for c in CbCategory.objects.only("name"): CATEGORIES.append((c.id, c.name)) self.fields["category"].choices = CATEGORIES users = [("", "Select user")] # self.fields["owner"] = self.request.user.id for c in User.objects.filter(is_superuser=True, is_staff=True, is_active=True): users.append((c.id, c.get_full_name())) self.fields["owner"].choices = users category = forms.Select(choices=[]) title = forms.CharField(max_length=255, widget=forms.TextInput, label="Title *") image = forms.ImageField(required=False, label="Image (max size 2MB, 500 x 280) ") description = forms.CharField(widget=forms.Textarea, max_length=1024, label="Description *") owner = forms.Select(choices=[]) meta_data = forms.CharField(required=False, widget=forms.Textarea) is_visible = forms.BooleanField(required=False) tag = forms.CharField( label='Type tag name', widget=AutoCompleteSelectMultipleWidget(TagLookup), required=False, ) def clean_title(self): category = self.cleaned_data.get("category") title = self.cleaned_data.get("title") if not self.instance.id: if CbTopic.objects.filter(category=category.id, title=title): raise forms.ValidationError( _("A category cannot have duplicate topic")) else: if CbTopic.objects.filter(~Q(pk=self.instance.id), category=category.id, title=title): raise forms.ValidationError( _("A category cannot have duplicate topic")) return title def clean_image(self): image = self.cleaned_data.get('image', False) if image: img = Image.open(image) w, h = img.size # validate dimensions max_width = 500 max_height = 280 if w > max_width or h > max_height: raise forms.ValidationError( _('Please use an image that is smaller or equal to ' '%s x %s pixels.' % (max_width, max_height))) print(image.name) # validate content type fileName, fileExtension = os.path.splitext(image.name) print(fileExtension) if not fileExtension in ['.jpeg', '.pjpeg', '.png', '.jpg']: raise forms.ValidationError( _('Please use a JPEG or PNG image.')) # validate file size if len(image) > (2 * 1024 * 1024): raise forms.ValidationError( _('Image file too large ( maximum 2mb )')) # else: # raise forms.ValidationError(_("Couldn't read uploaded image")) return image class Meta: model = CbTopic fields = ("category", "title", "image", "description", "owner", "tag", "is_visible")
class CbQuestionAdminForm(forms.ModelForm): def __init__(self, *args, **kwargs): # self.request = kwargs.pop("request") super(CbQuestionAdminForm, self).__init__(*args, **kwargs) CATEGORIES = [("", "Select category")] for c in CbCategory.objects.only("name"): CATEGORIES.append((c.id, c.name)) self.fields["category"].choices = CATEGORIES self.fields['attached_tags'].widget.attrs['readonly'] = True if self.instance.pk: tags = [] for t in self.instance.question_tags.all(): tags.append(t.tag.name) self.fields["attached_tags"].initial = ", ".join(tags) users = [("", "Select user")] # self.fields["owner"] = self.request.user.id for c in User.objects.filter(is_superuser=True, is_staff=True, is_active=True): users.append((c.id, c.get_full_name())) self.fields["owner"].choices = users category = forms.ChoiceField(label="Category *") # topic = forms.Select(choices=CbTopic.objects.only("title")) topic = AutoCompleteSelectField(lookup_class=TopicLookup, widget=AutoComboboxSelectWidget, label="Topic *") # title = forms.CharField(widget=forms.TextInput,max_length=1024,label="Title *") title = forms.CharField(widget=forms.Textarea, max_length=1024, label="Title *") description = forms.CharField(widget=TinyMCE(attrs={ 'cols': 80, 'rows': 600, "class": "tinymce" }), label="Description *") owner = forms.Select(choices=[]) is_deleted = forms.BooleanField(required=False) tag = forms.CharField( label='Type tag name', widget=AutoCompleteSelectMultipleWidget(TagLookup), required=False, ) attached_tags = forms.CharField(widget=forms.Textarea, required=False) def clean_category(self): if not CbCategory.objects.filter( pk=self.cleaned_data.get("category")).exists(): raise forms.ValidationError("Category does not exist") return self.cleaned_data.get("category") else: return CbCategory.objects.get(pk=self.cleaned_data.get("category")) class Meta: model = CbQuestion fields = ("category", "topic", "title", "description", "owner", "tag", "is_deleted", "attached_tags") class Media: js = ("main/js/admin-question-form-chain-select.js", )
class CbCategoryForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(CbCategoryForm, self).__init__(*args, **kwargs) users = [("", "Select user")] # self.fields["owner"] = self.request.user.id for c in User.objects.filter(is_superuser=True, is_staff=True, is_active=True): users.append((c.id, c.get_full_name())) self.fields["owner"].choices = users name = forms.CharField(max_length=255, label="Name *") image = forms.ImageField(required=False, label="Image (max size 2MB) ") description = forms.CharField(max_length=1024, required=False, widget=forms.Textarea) meta_data = forms.CharField(required=False, widget=forms.Textarea) # owner = forms.ModelMultipleChoiceField(queryset=User.objects.filter(is_superuser=True,is_staff=True)) owner = forms.Select(choices=[]) is_visible = forms.BooleanField(required=False) tag = forms.CharField( label='Type tag name', widget=AutoCompleteSelectMultipleWidget(TagLookup), required=False, ) # def clean_owner(self): # value = self.cleaned_data['owner'] # if len(value) > 1: # raise forms.ValidationError("You can't select more than 1 owner.") # return value[0] def clean_image(self): image = self.cleaned_data.get('image', False) if image: img = Image.open(image) w, h = img.size # validate dimensions max_width = 500 max_height = 280 # if w > max_width or h > max_height: # raise forms.ValidationError( # _('Please use an image that is smaller or equal to ' # '%s x %s pixels.' % (max_width, max_height))) # print(image.name) # validate content type fileName, fileExtension = os.path.splitext(image.name) print(fileExtension) if not fileExtension in ['.jpeg', '.pjpeg', '.png', '.jpg']: raise forms.ValidationError( _('Please use a JPEG or PNG image.')) # validate file size if len(image) > (2 * 1024 * 1024): raise forms.ValidationError( _('Image file too large ( maximum 2mb )')) # else: # raise forms.ValidationError(_("Couldn't read uploaded image")) return image class Meta: model = CbCategory fields = ("name", "image", "description", "meta_data", "owner", "tag", "is_visible")