def __init__(self, *args, **kwargs): super(PageForm, self).__init__(*args, **kwargs) if self.instance.header_image: self.fields[ 'header_image'].help_text = '<input name="remove_photo" id="id_remove_photo" type="checkbox"/> %s: <a target="_blank" href="/files/%s/">%s</a>' % ( _('Remove current image'), self.instance.header_image.pk, basename(self.instance.header_image.file.name)) else: self.fields.pop('remove_photo') if self.instance.pk: self.fields['content'].widget.mce_attrs[ 'app_instance_id'] = self.instance.pk else: self.fields['content'].widget.mce_attrs['app_instance_id'] = 0 self.fields['google_profile'].help_text = mark_safe( GOOGLE_PLUS_HELP_TEXT) if not self.user.profile.is_superuser: if 'syndicate' in self.fields: self.fields.pop('syndicate') if 'status_detail' in self.fields: self.fields.pop('status_detail') self.fields['template'].choices = [('default.html', _('Default')) ] + get_template_list()
def __init__(self, *args, **kwargs): super(PageForm, self).__init__(*args, **kwargs) if self.instance.header_image: self.fields["header_image"].help_text = ( '<input name="remove_photo" id="id_remove_photo" type="checkbox"/> %s: <a target="_blank" href="/files/%s/">%s</a>' % ( _("Remove current image"), self.instance.header_image.pk, basename(self.instance.header_image.file.name), ) ) else: self.fields.pop("remove_photo") if self.instance.pk: self.fields["content"].widget.mce_attrs["app_instance_id"] = self.instance.pk else: self.fields["content"].widget.mce_attrs["app_instance_id"] = 0 template_choices = [("default.html", _("Default"))] template_choices += get_template_list() self.fields["template"].choices = template_choices self.fields["google_profile"].help_text = mark_safe(GOOGLE_PLUS_HELP_TEXT) if not self.user.profile.is_superuser: if "syndicate" in self.fields: self.fields.pop("syndicate") if "status_detail" in self.fields: self.fields.pop("status_detail")
def create_new_template(request, form_class=AddTemplateForm): """ Create a new blank template for a given template name """ form = form_class(request.POST or None) ret_dict = {'created': False, 'err': ''} if form.is_valid(): template_name = form.cleaned_data['template_name'].strip() template_full_name = 'default-%s.html' % template_name existing_templates = [t[0] for t in get_template_list()] if not template_full_name in existing_templates: # create a new template and assign default content use_s3_storage = getattr(settings, 'USE_S3_STORAGE', '') if use_s3_storage: theme_dir = settings.ORIGINAL_THEMES_DIR else: theme_dir = settings.THEMES_DIR template_dir = os.path.join(theme_dir, get_setting('module', 'theme_editor', 'theme'), 'templates') template_full_path = os.path.join(template_dir, template_full_name) # grab the content from the new-default-template.html # first check if there is a customized one on the site default_template_name = 'new-default-template.html' default_template_path = os.path.join(template_dir, 'theme_editor', default_template_name) if not os.path.isfile(default_template_path): # no customized one found, use the default one default_template_path = os.path.join( os.path.abspath(os.path.dirname(__file__)), 'templates/theme_editor', default_template_name) if os.path.isfile(default_template_path): default_content = open(default_template_path).read() else: default_content = '' with open(template_full_path, 'w') as f: f.write(default_content) if use_s3_storage: # django default_storage not set for theme, that's why we cannot use it save_file_to_s3(template_full_path) ret_dict['created'] = True ret_dict['template_name'] = template_full_name else: ret_dict['err'] = _('Template "%(name)s" already exists' % {'name':template_full_name}) return HttpResponse(json.dumps(ret_dict))
def create_new_template(request, form_class=AddTemplateForm): """ Create a new blank template for a given template name """ selected_theme = request.GET.get("theme_edit", get_theme()) if not is_valid_theme(selected_theme): raise Http404(_('Specified theme does not exist')) if is_theme_read_only(selected_theme): raise Http403 form = form_class(request.POST or None) ret_dict = {'created': False, 'err': ''} if form.is_valid(): template_name = form.cleaned_data['template_name'].strip() template_full_name = 'default-%s.html' % template_name existing_templates = [t[0] for t in get_template_list()] if template_full_name not in existing_templates: # create a new template and assign default content theme_root = get_theme_root(selected_theme) template_dir = os.path.join(theme_root, 'templates') template_full_path = os.path.join(template_dir, template_full_name) # grab the content from the new-default-template.html # first check if there is a customized one on the site default_template_name = 'new-default-template.html' default_template_path = os.path.join(template_dir, 'theme_editor', default_template_name) if not os.path.isfile(default_template_path): # no customized one found, use the default one default_template_path = os.path.join( os.path.abspath(os.path.dirname(__file__)), 'templates/theme_editor', default_template_name) if os.path.isfile(default_template_path): default_content = open(default_template_path).read() else: default_content = '' with open(template_full_path, 'w') as f: f.write(default_content) if settings.USE_S3_STORAGE: s3_path = os.path.join(settings.THEME_S3_PATH, selected_theme, 'templates', template_full_name) save_file_to_s3(template_full_path, dest_path=s3_path, public=False) ret_dict['created'] = True ret_dict['template_name'] = template_full_name else: ret_dict['err'] = _('Template "%(name)s" already exists' % {'name': template_full_name}) return HttpResponse(json.dumps(ret_dict))
def __init__(self, *args, **kwargs): super(PageAdminForm, self).__init__(*args, **kwargs) if self.instance.pk: self.fields["content"].widget.mce_attrs["app_instance_id"] = self.instance.pk if self.instance.meta: self.fields["meta_title"].initial = self.instance.meta.title self.fields["meta_description"].initial = self.instance.meta.description self.fields["meta_keywords"].initial = self.instance.meta.keywords self.fields["meta_canonical_url"].initial = self.instance.meta.canonical_url else: self.fields["content"].widget.mce_attrs["app_instance_id"] = 0 template_choices = [("default.html", _("Default"))] template_choices += get_template_list() self.fields["template"].choices = template_choices
def __init__(self, *args, **kwargs): super(PageAdminForm, self).__init__(*args, **kwargs) if self.instance.pk: self.fields['content'].widget.mce_attrs['app_instance_id'] = self.instance.pk if self.instance.meta: self.fields['meta_title'].initial = self.instance.meta.title self.fields['meta_description'].initial = self.instance.meta.description self.fields['meta_keywords'].initial = self.instance.meta.keywords self.fields['meta_canonical_url'].initial = self.instance.meta.canonical_url else: self.fields['content'].widget.mce_attrs['app_instance_id'] = 0 template_choices = [('default.html',_('Default'))] template_choices += get_template_list() self.fields['template'].choices = template_choices
def __init__(self, *args, **kwargs): super(PageForm, self).__init__(*args, **kwargs) if self.instance.header_image: self.fields['header_image'].help_text = '<input name="remove_photo" id="id_remove_photo" type="checkbox"/> %s: <a target="_blank" href="/files/%s/">%s</a>' % (_('Remove current image'), self.instance.header_image.pk, basename(self.instance.header_image.file.name)) else: self.fields.pop('remove_photo') if self.instance.pk: self.fields['content'].widget.mce_attrs['app_instance_id'] = self.instance.pk else: self.fields['content'].widget.mce_attrs['app_instance_id'] = 0 self.fields['google_profile'].help_text = mark_safe(GOOGLE_PLUS_HELP_TEXT) if not self.user.profile.is_superuser: if 'syndicate' in self.fields: self.fields.pop('syndicate') if 'status_detail' in self.fields: self.fields.pop('status_detail') self.fields['template'].choices = [('default.html',_('Default'))] + get_template_list()
from tendenci.apps.base.fields import EmailVerificationField, PriceField from tendenci.apps.base.forms import FormControlWidgetMixin from tendenci.apps.base.utils import currency_check from tendenci.apps.payments.fields import PaymentMethodModelMultipleChoiceField from tendenci.apps.recurring_payments.fields import BillingCycleField from tendenci.apps.recurring_payments.widgets import BillingCycleWidget, BillingDateSelectWidget from tendenci.apps.forms_builder.forms.models import FormEntry, FieldEntry, Field, Form, Pricing from tendenci.apps.forms_builder.forms.settings import FIELD_MAX_LENGTH template_choices = [ ('', _('None')), ('default.html', _('Default')), ('forms/base.html', _('Forms Base')) ] template_choices += get_template_list() #fs = FileSystemStorage(location=UPLOAD_ROOT) FIELD_FNAME_LENGTH = 30 FIELD_LNAME_LENGTH = 30 FIELD_NAME_LENGTH = 50 FIELD_PHONE_LENGTH = 50 THIS_YEAR = datetime.today().year class FormForForm(FormControlWidgetMixin, forms.ModelForm): class Meta: model = FormEntry exclude = ("form", "entry_time", "entry_path", "payment_method", "pricing", "creator")
class FormAdminForm(TendenciBaseForm): status_detail = forms.ChoiceField(choices=( ('draft', _('Draft')), ('published', _('Published')), )) intro = forms.CharField(required=False, widget=TinyMCE(attrs={'style': 'width:100%'}, mce_attrs={ 'storme_app_label': Form._meta.app_label, 'storme_model': Form._meta.model_name.lower() })) response = forms.CharField( required=False, label=_('Confirmation Text'), widget=TinyMCE(attrs={'style': 'width:100%'}, mce_attrs={ 'storme_app_label': Form._meta.app_label, 'storme_model': Form._meta.model_name.lower() }), help_text= _("Optionally, after submission, display a page with this text. Alternately, specify a Completion URL." )) email_text = forms.CharField(required=False, label=_('Email Text to Submitter'), widget=TinyMCE( attrs={'style': 'width:100%'}, mce_attrs={ 'storme_app_label': Form._meta.app_label, 'storme_model': Form._meta.model_name.lower() })) template_choices = [('', _('None')), ('default.html', _('Default')), ('forms/base.html', _('Forms Base'))] template_choices += get_template_list() template = forms.ChoiceField(choices=template_choices, required=False) group = forms.ChoiceField(required=True, choices=[]) class Meta: model = Form fields = ( 'title', 'slug', 'intro', 'response', 'group', 'template', 'send_email', # removed per ed's request, added back per Aaron's request 2011-10-14 'email_text', 'subject_template', 'completion_url', 'email_from', 'email_copies', 'user_perms', 'member_perms', 'group_perms', 'allow_anonymous_view', 'status_detail', 'custom_payment', 'recurring_payment', 'payment_methods', 'intro_position', 'fields_position', 'pricing_position', 'intro_name', 'fields_name', 'pricing_name', ) def __init__(self, *args, **kwargs): super(FormAdminForm, self).__init__(*args, **kwargs) if self.instance.pk: self.fields['intro'].widget.mce_attrs[ 'app_instance_id'] = self.instance.pk self.fields['response'].widget.mce_attrs[ 'app_instance_id'] = self.instance.pk if self.instance.intro_name: self.fields['intro'].label = self.instance.intro_name else: self.fields['intro'].widget.mce_attrs['app_instance_id'] = 0 self.fields['response'].widget.mce_attrs['app_instance_id'] = 0 self.fields['group'].initial = Group.objects.get_initial_group_id() default_groups = Group.objects.filter(status=True, status_detail="active") self.fields['group'].choices = default_groups.values_list('pk', 'name') position_fields = [ 'intro_position', 'fields_position', 'pricing_position' ] for field in position_fields: self.fields[field].widget.attrs['class'] = 'position_field' def clean_group(self): group_id = self.cleaned_data['group'] try: return Group.objects.get(pk=group_id) except Group.DoesNotExist: raise forms.ValidationError(_('Invalid group selected.')) def clean_slug(self): slug = slugify(self.cleaned_data['slug']) i = 0 while True: if i > 0: if i > 1: slug = slug.rsplit("-", 1)[0] slug = "%s-%s" % (slug, i) match = Form.objects.filter(slug=slug) if self.instance: match = match.exclude(pk=self.instance.pk) if not match: break i += 1 return slug
class PageAdminForm(TendenciBaseForm): content = forms.CharField(required=False, widget=TinyMCE(attrs={'style': 'width:100%'}, mce_attrs={ 'storme_app_label': Page._meta.app_label, 'storme_model': Page._meta.model_name.lower() })) syndicate = forms.BooleanField(label=_('Include in RSS Feed'), required=False, initial=True) status_detail = forms.ChoiceField(choices=( ('active', _('Active')), ('inactive', _('Inactive')), ('pending', _('Pending')), )) template_choices = [('default.html', _('Default'))] template_choices += get_template_list() template = forms.ChoiceField(choices=template_choices) meta_title = forms.CharField(required=False) meta_description = forms.CharField( required=False, widget=forms.widgets.Textarea(attrs={'style': 'width:100%'})) meta_keywords = forms.CharField( required=False, widget=forms.widgets.Textarea(attrs={'style': 'width:100%'})) meta_canonical_url = forms.CharField(required=False) class Meta: model = Page fields = ( 'title', 'slug', 'content', 'group', 'tags', 'template', 'meta_title', 'meta_description', 'meta_keywords', 'meta_canonical_url', 'allow_anonymous_view', 'user_perms', 'group_perms', 'member_perms', 'syndicate', 'status_detail', ) def __init__(self, *args, **kwargs): super(PageAdminForm, self).__init__(*args, **kwargs) if self.instance.pk: self.fields['content'].widget.mce_attrs[ 'app_instance_id'] = self.instance.pk if self.instance.meta: self.fields['meta_title'].initial = self.instance.meta.title self.fields[ 'meta_description'].initial = self.instance.meta.description self.fields[ 'meta_keywords'].initial = self.instance.meta.keywords self.fields[ 'meta_canonical_url'].initial = self.instance.meta.canonical_url else: self.fields['content'].widget.mce_attrs['app_instance_id'] = 0 def clean_syndicate(self): """ clean method for syndicate added due to the update done on the field BooleanField -> NullBooleanField NOTE: BooleanField is converted to NullBooleanField because some Boolean data has value of None than False. This was updated on Django 1.6. BooleanField cannot have a value of None. """ data = self.cleaned_data.get('syndicate', False) if data: return True else: return False def clean(self): cleaned_data = super(PageAdminForm, self).clean() slug = cleaned_data.get('slug') # Check if duplicate slug from different page (i.e. different guids) # Case 1: Page is edited if self.instance: guid = self.instance.guid if Page.objects.filter(slug=slug).exclude(guid=guid).exists(): self._errors['slug'] = self.error_class( [_('Duplicate value for slug.')]) del cleaned_data['slug'] # Case 2: Add new Page else: if Page.objects.filter(slug=slug).exists(): self._errors['slug'] = self.error_class( [_('Duplicate value for slug.')]) del cleaned_data['slug'] return cleaned_data
class PageForm(TendenciBaseForm): # header_image = forms.ImageField(required=False) # remove_photo = forms.BooleanField(label=_('Remove the current header image'), required=False) content = forms.CharField(required=False, widget=TinyMCE(attrs={'style': 'width:100%'}, mce_attrs={ 'storme_app_label': Page._meta.app_label, 'storme_model': Page._meta.model_name.lower() }), label=_("content")) # contributor_type = forms.ChoiceField(choices=CONTRIBUTOR_CHOICES, # initial=Page.CONTRIBUTOR_AUTHOR, # widget=forms.RadioSelect()) # syndicate = forms.BooleanField(label=_('Include in RSS Feed'), required=False, initial=True) status_detail = forms.ChoiceField(choices=(('active', _('Active')), ('inactive', _('Inactive')), ('pending', _('Pending'))), label=_('status detail')) tags = forms.CharField( required=False, help_text=mark_safe('<a href="/tags/" target="_blank">%s</a>' % _('Open All Tags list in a new window')), label=_('tags')) template_choices = [('default.html', _('Default'))] template_choices += get_template_list() template = forms.ChoiceField(choices=template_choices) class Meta: model = Page fields = ( 'title', 'slug', 'content', 'tags', 'template', # 'group', # 'contributor_type', # 'google_profile', 'allow_anonymous_view', # 'syndicate', 'user_perms', 'group_perms', 'member_perms', 'status_detail', ) fieldsets = [ ( _('Page Information'), { 'fields': [ 'title', 'slug', 'content', 'tags', # 'header_image', 'template', # 'group' ], 'legend': '' }), # (_('Contributor'), { # 'fields': ['contributor_type', # 'google_profile'], # 'classes': ['boxy-grey'], # }), (_('Permissions'), { 'fields': [ 'allow_anonymous_view', 'user_perms', 'member_perms', 'group_perms', ], 'classes': ['permissions'], }), ( _('Administrator Only'), { 'fields': [ # 'syndicate', 'status_detail' ], 'classes': ['admin-only'], }) ] # def clean_syndicate(self): # """ # clean method for syndicate added due to the update # done on the field BooleanField -> NullBooleanField # NOTE: BooleanField is converted to NullBooleanField because # some Boolean data has value of None than False. This was updated # on Django 1.6. BooleanField cannot have a value of None. # """ # data = self.cleaned_data.get('syndicate', False) # if data: # return True # else: # return False def clean(self): cleaned_data = super(PageForm, self).clean() slug = cleaned_data.get('slug') # Check if duplicate slug from different page (i.e. different guids) # Case 1: Page is edited if self.instance: guid = self.instance.guid if Page.objects.filter(slug=slug).exclude(guid=guid).exists(): self._errors['slug'] = self.error_class( [_('Duplicate value for slug.')]) del cleaned_data['slug'] # Case 2: Add new Page else: if Page.objects.filter(slug=slug).exists(): self._errors['slug'] = self.error_class( [_('Duplicate value for slug.')]) del cleaned_data['slug'] return cleaned_data # def clean_header_image(self): # header_image = self.cleaned_data['header_image'] # if header_image: # extension = splitext(header_image.name)[1] # # # check the extension # if extension.lower() not in ALLOWED_IMG_EXT: # raise forms.ValidationError(_('The header image must be of jpg, gif, or png image type.')) # # # check the image header_image # image_type = '.%s' % imghdr.what('', header_image.read()) # if image_type not in ALLOWED_IMG_EXT: # raise forms.ValidationError(_('The header image is an invalid image. Try uploading another image.')) # # max_upload_size = get_max_file_upload_size() # if header_image.size > max_upload_size: # raise forms.ValidationError(_('Please keep filesize under %(max_upload_size)s. Current filesize %(header_image)s') % { # 'max_upload_size': filesizeformat(max_upload_size), # 'header_image': filesizeformat(header_image.size)}) # # return header_image def __init__(self, *args, **kwargs): super(PageForm, self).__init__(*args, **kwargs) # if self.instance.header_image: # self.fields['header_image'].help_text = u'<input name="remove_photo" id="id_remove_photo" type="checkbox"/> %s: <a target="_blank" href="/files/%s/">%s</a>' % (_('Remove current image'), self.instance.header_image.pk, basename(self.instance.header_image.file.name)) # else: # self.fields.pop('remove_photo') if self.instance.pk: self.fields['content'].widget.mce_attrs[ 'app_instance_id'] = self.instance.pk else: self.fields['content'].widget.mce_attrs['app_instance_id'] = 0 # self.fields['google_profile'].help_text = mark_safe(GOOGLE_PLUS_HELP_TEXT) if not self.user.profile.is_superuser: # if 'syndicate' in self.fields: self.fields.pop('syndicate') if 'status_detail' in self.fields: self.fields.pop('status_detail') self.fields['title'].label = _('Title') self.fields['template'].label = _('Template') def save(self, *args, **kwargs): page = super(PageForm, self).save(*args, **kwargs) # if self.cleaned_data.get('remove_photo'): # page.header_image = None return page