def photoset_add(request, form_class=PhotoSetAddForm, template_name="photos/photo-set/add.html"): """ Add a photo set """ # if no permission; permission exception if not has_perm(request.user, 'photos.add_photoset'): raise Http403 if request.method == "POST": if request.POST["action"] == "add": form = form_class(request.POST, user=request.user) if form.is_valid(): photo_set = form.save(commit=False) photo_set.author = request.user # update all permissions and save the model photo_set = update_perms_and_save(request, form, photo_set) checklist_update('add-album') messages.add_message(request, messages.SUCCESS, 'Successfully added photo set!') return HttpResponseRedirect( reverse('photos_batch_add', kwargs={'photoset_id': photo_set.id})) else: form = form_class(user=request.user) return render_to_response(template_name, { "photoset_form": form, }, context_instance=RequestContext(request))
def theme_picker(request, template_name="theme_editor/theme_picker.html"): if not request.user.profile.is_superuser: raise Http403 themes = [] for theme in theme_choice_list(): theme_info = ThemeInfo(theme) themes.append(theme_info) if request.method == "POST": selected_theme = request.POST.get('theme') call_command('set_theme', selected_theme) checklist_update('choose-theme') messages.add_message( request, messages.SUCCESS, "Your theme has been changed to %s." % selected_theme.title()) return redirect('home') current_theme = get_setting('module', 'theme_editor', 'theme') themes = sorted(themes, key=lambda theme: theme.create_dt) return render_to_response(template_name, { 'themes': themes, 'current_theme': current_theme, 'theme_choices': theme_choice_list(), }, context_instance=RequestContext(request))
def theme_picker(request, template_name="theme_editor/theme_picker.html"): if not request.user.profile.is_superuser: raise Http403 themes = [] for theme in theme_choice_list(): theme_info = ThemeInfo(theme) themes.append(theme_info) if request.method == "POST": selected_theme = request.POST.get("theme") call_command("set_theme", selected_theme) checklist_update("choose-theme") msg_string = "Your theme has been changed to %s." % selected_theme.title() messages.add_message(request, messages.SUCCESS, _(msg_string)) return redirect("home") current_theme = get_setting("module", "theme_editor", "theme") themes = sorted(themes, key=lambda theme: theme.create_dt) return render_to_response( template_name, {"themes": themes, "current_theme": current_theme, "theme_choices": theme_choice_list()}, context_instance=RequestContext(request), )
def photoset_add(request, form_class=PhotoSetAddForm, template_name="photos/photo-set/add.html"): """ Add a photo set """ # if no permission; permission exception if not has_perm(request.user,'photos.add_photoset'): raise Http403 if request.method == "POST": if request.POST["action"] == "add": form = form_class(request.POST, user=request.user) if form.is_valid(): photo_set = form.save(commit=False) photo_set.author = request.user # update all permissions and save the model photo_set = update_perms_and_save(request, form, photo_set) checklist_update('add-album') messages.add_message(request, messages.SUCCESS, 'Successfully added photo set!') return HttpResponseRedirect(reverse('photos_batch_add', kwargs={'photoset_id':photo_set.id})) else: form = form_class(user=request.user) return render_to_response(template_name, { "photoset_form": form, }, context_instance=RequestContext(request))
def save_settings_form(self): """ Save the updated settings in the database Setting's save will trigger a cache update. If the field type is 'file' a file entry will be created. """ for setting in self.settings: old_value = setting.get_value() try: field_value = self.cleaned_data[setting.name] if setting.input_type == "file": if field_value: # save a file object and set the value at that file object's id. from tendenci.core.files.models import File as TendenciFile uploaded_file = TendenciFile() uploaded_file.owner = self.user uploaded_file.owner_username = self.user.username uploaded_file.creator = self.user uploaded_file.creator_username = self.user.username uploaded_file.content_type = ContentType.objects.get( app_label="site_settings", model="setting") uploaded_file.file.save(field_value.name, File(field_value)) uploaded_file.save() field_value = uploaded_file.pk else: #retain the old file if no file is set field_value = setting.get_value() # update value if changed and save if old_value != field_value: setting.set_value(field_value) setting.save() # save updates the cash automatically # update the django site value in the contrib backend if setting.name == "siteurl" and setting.scope == "site": if field_value: django_site = Site.objects.get(pk=1) django_site.domain = field_value.replace("http://", "") django_site.name = field_value.replace("http://", "") django_site.save() # update checklist for theme logo if setting.name == 'logo' and setting.scope_category == 'theme': checklist_update('upload-logo') # update checklist for contact form if setting.name == 'contact_form' and setting.scope == "site": checklist_update('update-contact') except KeyError: pass
def save_settings_form(self): """ Save the updated settings in the database Setting's save will trigger a cache update. If the field type is 'file' a file entry will be created. """ for setting in self.settings: old_value = setting.get_value() try: field_value = self.cleaned_data[setting.name] if setting.input_type == "file": if field_value: # save a file object and set the value at that file object's id. from tendenci.core.files.models import File as TendenciFile uploaded_file = TendenciFile() uploaded_file.owner = self.user uploaded_file.owner_username = self.user.username uploaded_file.creator = self.user uploaded_file.creator_username = self.user.username uploaded_file.content_type = ContentType.objects.get(app_label="site_settings", model="setting") uploaded_file.file.save(field_value.name, File(field_value)) uploaded_file.save() field_value = uploaded_file.pk else: # retain the old file if no file is set field_value = setting.get_value() # update value if changed and save if old_value != field_value: setting.set_value(field_value) setting.save() # save updates the cash automatically # update the django site value in the contrib backend if setting.name == "siteurl" and setting.scope == "site": if field_value: django_site = Site.objects.get(pk=1) django_site.domain = field_value.replace("http://", "") django_site.name = field_value.replace("http://", "") django_site.save() # update checklist for theme logo if setting.name == "logo" and setting.scope_category == "theme": checklist_update("upload-logo") # update checklist for contact form if setting.name == "contact_form" and setting.scope == "site": checklist_update("update-contact") except KeyError: pass
def theme_color(request): if not request.user.profile.is_superuser: raise Http403 if request.is_ajax() and request.method == "POST": if request.POST.get("colors", None): color_setting = Setting.objects.get(scope="module", scope_category="theme", name="colorvars") color_setting.set_value(request.POST.get("colors")) color_setting.save() checklist_update("customize-color") message = _("Successfully updated theme colors.") response = json.dumps({"message": message}) return HttpResponse(response, mimetype="application/json") raise Http404
def theme_color(request): if not request.user.profile.is_superuser: raise Http403 if request.is_ajax() and request.method == 'POST': if request.POST.get('colors', None): color_setting = Setting.objects.get(scope='module', scope_category='theme', name='colorvars') color_setting.set_value(request.POST.get('colors')) color_setting.save() checklist_update('customize-color') message = 'Successfully updated theme colors.' response = json.dumps({'message': message}) return HttpResponse(response, mimetype="application/json") raise Http404
def edit_items(request, id, template_name="navs/nav_items.html"): nav = get_object_or_404(Nav, id=id) if not has_perm(request.user, 'navs.change_nav', nav): raise Http403 ItemFormSet = modelformset_factory(NavItem, form=ItemForm, extra=0, can_delete=True) page_select = PageSelectForm() if request.method == "POST": formset = ItemFormSet(request.POST) if formset.is_valid(): #delete old nav items nav.navitem_set.all().delete() items = formset.save(commit=False) # update or create nav items for item in items: item.nav = nav item.save() cache_nav(nav) messages.add_message(request, messages.SUCCESS, 'Successfully updated %s' % nav) if nav.pk == 1: # the main nav has id 1 in the npo defaults fixture checklist_update('update-nav') EventLog.objects.log(instance=nav) redirect_to = request.REQUEST.get('next', '') if redirect_to: return HttpResponseRedirect(redirect_to) else: return redirect('navs.detail', id=nav.id) else: formset = ItemFormSet(queryset=nav.navitem_set.all().order_by('position')) return render_to_response( template_name, {'page_select':page_select, 'formset':formset, 'current_nav':nav}, context_instance=RequestContext(request), )
def add(request, form_class=StoryForm, template_name="stories/add.html"): if has_perm(request.user, 'stories.add_story'): if request.method == "POST": form = form_class(request.POST, request.FILES, user=request.user) if form.is_valid(): story = form.save(commit=False) story = update_perms_and_save(request, form, story) # save photo photo = form.cleaned_data['photo_upload'] if photo: story.save(photo=photo) assign_files_perms(story, files=[story.image]) if 'rotator' in story.tags: checklist_update('add-story') messages.add_message( request, messages.SUCCESS, _('Successfully added %(str)s' % {'str': unicode(story)})) return HttpResponseRedirect(reverse('story', args=[story.pk])) else: from pprint import pprint pprint(form.errors.items()) else: form = form_class(user=request.user) tags = request.GET.get('tags', '') if tags: form.fields['tags'].initial = tags else: raise Http403 return render_to_response(template_name, {'form': form}, context_instance=RequestContext(request))
def add(request, form_class=StoryForm, template_name="stories/add.html"): if has_perm(request.user,'stories.add_story'): if request.method == "POST": form = form_class(request.POST, request.FILES, user=request.user) if form.is_valid(): story = form.save(commit=False) story = update_perms_and_save(request, form, story) # save photo photo = form.cleaned_data['photo_upload'] if photo: story.save(photo=photo) assign_files_perms(story, files=[story.image]) if 'rotator' in story.tags: checklist_update('add-story') messages.add_message(request, messages.SUCCESS, _('Successfully added %(str)s' % {'str':story})) return HttpResponseRedirect(reverse('story', args=[story.pk])) else: from pprint import pprint pprint(form.errors.items()) else: form = form_class(user=request.user) tags = request.GET.get('tags', '') if tags: form.fields['tags'].initial = tags else: raise Http403 return render_to_response(template_name, {'form':form}, context_instance=RequestContext(request))
def theme_picker(request, template_name="theme_editor/theme_picker.html"): if not request.user.profile.is_superuser: raise Http403 themes = [] for theme in theme_choice_list(): theme_info = ThemeInfo(theme) themes.append(theme_info) if request.method == "POST": selected_theme = request.POST.get('theme') call_command('set_theme', selected_theme) checklist_update('choose-theme') messages.add_message(request, messages.SUCCESS, "Your theme has been changed to %s." % selected_theme.title()) return redirect('home') current_theme = get_setting('module', 'theme_editor', 'theme') themes = sorted(themes, key=lambda theme: theme.create_dt) return render_to_response(template_name, { 'themes': themes, 'current_theme': current_theme, 'theme_choices': theme_choice_list(), }, context_instance=RequestContext(request))
def add(request, form_class=PageForm, meta_form_class=MetaForm, category_form_class=CategoryForm, template_name="pages/add.html"): if not has_perm(request.user, 'pages.add_page'): raise Http403 content_type = get_object_or_404(ContentType, app_label='pages', model='page') if request.method == "POST": form = form_class(request.POST, request.FILES, user=request.user) metaform = meta_form_class(request.POST, prefix='meta') categoryform = category_form_class(content_type, request.POST, prefix='category') if form.is_valid() and metaform.is_valid() and categoryform.is_valid(): page = form.save() # handle header image f = form.cleaned_data['header_image'] if f: header = HeaderImage() header.content_type = ContentType.objects.get_for_model(Page) header.object_id = page.id header.creator = request.user header.creator_username = request.user.username header.owner = request.user header.owner_username = request.user.username filename = "%s-%s" % (page.slug, f.name) f.file.seek(0) header.file.save(filename, f) page.header_image = header #save meta meta = metaform.save() page.meta = meta ## update the category and subcategory page.update_category_subcategory( categoryform.cleaned_data['category'], categoryform.cleaned_data['sub_category'] ) # add all permissions page = update_perms_and_save(request, form, page) messages.add_message(request, messages.SUCCESS, _('Successfully added %(p)s' % {'p': unicode(page)})) checklist_update('add-page') if not request.user.profile.is_superuser: # send notification to administrators recipients = get_notice_recipients('module', 'pages', 'pagerecipients') if recipients: if notification: extra_context = { 'object': page, 'request': request, } notification.send_emails(recipients, 'page_added', extra_context) if page.status and page.status_detail == 'active': return HttpResponseRedirect(reverse('page', args=[page.slug])) return HttpResponseRedirect(reverse('page.search')) else: initial_category_form_data = { 'app_label': 'pages', 'model': 'page', 'pk': 0, } form = form_class(user=request.user) metaform = meta_form_class(prefix='meta') categoryform = category_form_class(content_type, initial=initial_category_form_data, prefix='category') return render_to_response(template_name, { 'form': form, 'metaform': metaform, 'categoryform': categoryform, }, context_instance=RequestContext(request))
def edit(request, id, form_class=PageForm, meta_form_class=MetaForm, category_form_class=CategoryForm, template_name="pages/edit.html"): page = get_object_or_404(Page, pk=id) if not has_perm(request.user, 'pages.change_page', page): raise Http403 content_type = get_object_or_404(ContentType, app_label='pages', model='page') if request.method == "POST": form = form_class(request.POST, request.FILES, instance=page, user=request.user) metaform = meta_form_class(request.POST, instance=page.meta, prefix='meta') categoryform = category_form_class(content_type, request.POST, prefix='category') if form.is_valid() and metaform.is_valid() and categoryform.is_valid(): page = form.save() # handle header image f = form.cleaned_data['header_image'] if f: header = HeaderImage() header.content_type = ContentType.objects.get_for_model(Page) header.object_id = page.id header.creator = request.user header.creator_username = request.user.username header.owner = request.user header.owner_username = request.user.username filename = "%s-%s" % (page.slug, f.name) f.file.seek(0) header.file.save(filename, f) page.header_image = header #save meta meta = metaform.save() page.meta = meta ## update the category and subcategory page.update_category_subcategory( categoryform.cleaned_data['category'], categoryform.cleaned_data['sub_category'] ) # update all permissions page = update_perms_and_save(request, form, page) if page.pk == 1: # the about page has id 1 in the npo defaults fixture checklist_update('update-about') messages.add_message(request, messages.SUCCESS, _('Successfully updated %(p)s' % {'p': unicode(page)})) if not request.user.profile.is_superuser: # send notification to administrators recipients = get_notice_recipients('module', 'pages', 'pagerecipients') if recipients: if notification: extra_context = { 'object': page, 'request': request, } notification.send_emails(recipients, 'page_edited', extra_context) return HttpResponseRedirect(reverse('page', args=[page.slug])) else: form = form_class(instance=page, user=request.user) metaform = meta_form_class(instance=page.meta, prefix='meta') #setup categories category = Category.objects.get_for_object(page, 'category') sub_category = Category.objects.get_for_object(page, 'sub_category') initial_category_form_data = { 'app_label': 'pages', 'model': 'page', 'pk': page.pk, 'category': getattr(category, 'name', '0'), 'sub_category': getattr(sub_category, 'name', '0') } categoryform = category_form_class(content_type, initial=initial_category_form_data, prefix='category') return render_to_response(template_name, { 'page': page, 'form': form, 'metaform': metaform, 'categoryform': categoryform, }, context_instance=RequestContext(request))
def save(self, *args, **kwargs): # If this is the current contact form, update checklist if str(self.pk) == get_setting('site', 'global', 'contact_form'): checklist_update('update-contact') super(Form, self).save(*args, **kwargs)