Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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))
Ejemplo n.º 3
0
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_resp(request=request,
                          template_name=template_name,
                          context={
                              "photoset_form": form,
                          })
Ejemplo n.º 4
0
 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')
     if not self.group:
         self.group_id = get_default_group()
     super(Form, self).save(*args, **kwargs)
Ejemplo n.º 5
0
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_choices():
        theme_info = ThemeInfo(theme)
        themes.append(theme_info)

    if request.method == "POST":
        selected_theme = request.POST.get('theme')
        if not is_valid_theme(selected_theme):
            raise Http403
        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')

    active_theme = get_active_theme()
    themes = sorted(themes, key=lambda theme: theme.create_dt)

    return render_to_resp(request=request,
                          template_name=template_name,
                          context={
                              'themes': themes,
                              'current_theme': active_theme,
                              'theme_choices': theme_choices(),
                          })
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
 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')
     if not self.group:
         self.group_id = get_default_group()
     super(Form, self).save(*args, **kwargs)
Ejemplo n.º 8
0
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.apps.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)
                    if urlparse(field_value).scheme == "":
                        # prefix http:// if no scheme
                        field_value = 'http://%s' % field_value
                    netloc = urlparse(field_value).netloc
                    django_site.domain = netloc
                    django_site.name = netloc
                    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
Ejemplo n.º 9
0
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.apps.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)
                    if urlparse(field_value).scheme == "":
                        # prefix http:// if no scheme
                        field_value = 'http://%s' % field_value
                    netloc = urlparse(field_value).netloc
                    django_site.domain = netloc
                    django_site.name = netloc
                    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
Ejemplo n.º 10
0
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, content_type="application/json")

    raise Http404
Ejemplo n.º 11
0
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, content_type="application/json")

    raise Http404
Ejemplo n.º 12
0
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': str(story)}))

                return HttpResponseRedirect(reverse('story', args=[story.pk]))
            else:
                from pprint import pprint
                pprint(list(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_resp(request=request,
                          template_name=template_name,
                          context={'form': form})
Ejemplo n.º 13
0
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))
Ejemplo n.º 14
0
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,)
        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,)

    return render_to_response(template_name,
        {
            'page': page,
            'form': form,
            'metaform': metaform,
            'categoryform': categoryform,
        },
        context_instance=RequestContext(request))
Ejemplo n.º 15
0
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,)
        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,)
    return render_to_response(template_name,
            {
                'form': form,
                'metaform': metaform,
                'categoryform': categoryform,
            },
            context_instance=RequestContext(request))
Ejemplo n.º 16
0
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),
    )
Ejemplo n.º 17
0
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),
    )
Ejemplo n.º 18
0
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,
        )
        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,
        )
    return render_to_response(template_name, {
        'form': form,
        'metaform': metaform,
        'categoryform': categoryform,
    },
                              context_instance=RequestContext(request))
Ejemplo n.º 19
0
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,)
        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,)

    return render_to_response(
        template_name,
        {
            'page': page,
            'form': form,
            # 'metaform': metaform,
            # 'categoryform': categoryform,
        },
        context_instance=RequestContext(request))