Esempio n. 1
0
    def test_merge_conflict(self):
        p = Page()
        p.content = '<p>old content</p>'
        p.name = 'Front Page'
        p.save()

        a = PageForm(instance=p)
        b = PageForm(instance=p)
        b_post = b.initial
        b_post['content'] = '<p>b content</p>'
        b = PageForm(b_post, instance=p)
        self.failUnless(b.is_valid())
        b.save()

        p = Page.objects.get(pk=p.pk)
        a_post = a.initial
        a_post['content'] = '<p>a content</p>'
        a = PageForm(a_post, instance=p)
        self.failIf(a.is_valid())
        self.failUnless(PageForm.conflict_error in str(a.errors))

        a_post = a.data
        a = PageForm(a_post, instance=p)
        self.failUnless(a.is_valid())
        a.save()
        p = Page.objects.get(pk=p.pk)
        self.failUnless('Edit conflict!' in p.content)
Esempio n. 2
0
def add_page(request, category_name_url):
    context = RequestContext(request)
    cat_list = get_category_list()
    category_name = decode_url(category_name_url)
    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            page = form.save(commit=False)

            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExit:
                return render_to_response('rango/add_category.html', {}, context)

            page.views = 0
            form.save(commit=True)
            page.save()
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    return render_to_response('rango/add_page.html', {'category_name_url': category_name_url, 'category_name': category_name, 'cat_list':cat_list, 'form': form}, context)
Esempio n. 3
0
def edit_page(request, id):
    langs = []
    for lang in settings.LANGUAGES:
        langs.append({
            'code': lang[0],
            'title': _(lang[1])
        })

    page = get_object_or_404(Page, id=id)

    if request.method == 'POST':
        page_form = PageForm(request.POST, prefix='page_form', instance=page)

        if page_form.is_valid():
            page_form.save()
            return redirect('pages:administration:pages_list')

    else:
        page_form = PageForm(prefix='page_form', instance=page)

    return render(request, 'pages/administration/edit_page.html', {
        'page': page,
        'langs': langs,
        'page_form': page_form,
    })
Esempio n. 4
0
def add_page(request):
    context = RequestContext(request)

    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            form.save(commit=True)
            return index(request)
        else:
            print form.errors
    else:
        form = PageForm()

    return render_to_response('rango/add_page.html', {'form': form}, context)
Esempio n. 5
0
def add_page(request, category_name_slug):

    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
                cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                # probably better to use a redirect here.
                return category(request, category_name_slug)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict = {'form':form, 'category': cat}

    return render(request, 'rango/add_page.html', context_dict)
def add_page(request, category_name_url):
    context = RequestContext(request)
    context_dict = {}

    category_name = decode_url(category_name_url)
    if request.method == 'POST':
        form = PageForm(data=request.POST)

        if form.is_valid():
            page = form.save(commit=False)
            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                return render_to_response('rango/add_page.html', context_dict,
                                          context)
            page.views = 0
            page.save()

            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict['category_name_url'] = category_name_url
    context_dict['category_name'] = category_name
    context_dict['form'] = form

    return render_to_response('rango/add_page.html', context_dict, context)
Esempio n. 7
0
def page_edit_static(request, id):
    shop = request.shop

    page = get_object_or_404(Page, pk=id)
    page_name = page.name
    page_name_link = page.name_link
    page_title = page.title
    page_body = page.body
    page_meta_content = page.meta_content

    static_pages = Page.objects.filter(shop=shop)
    dynamic_pages = DynamicPageContent.objects.filter(shop=shop)
    form = PageForm(shop, request.POST or None, instance=page)
    if request.method == "POST":
        if form.is_valid():
            new_page = form.save(commit=False)
            new_page.save()
            version = PageVersion(page=page)
            version.name = page_name
            version.name_link = page_name_link
            version.title = page_title
            version.body = page_body
            version.meta_content = page_meta_content
            version.save()
            request.flash['message'] = unicode(_("Page successfully edited."))
            request.flash['severity'] = "success"
            return HttpResponseRedirect(reverse('page_edit_static', args=[id]))

    return render_to_response(
        'store_admin/web_store/pages_edit_static_page.html', {
            'form': form,
            'page': page,
            'static_pages': static_pages,
            'dynamic_pages': dynamic_pages
        }, RequestContext(request))
Esempio n. 8
0
def add_page(request, category_name_url):
	print 'add_page called'
	context = RequestContext(request)

	category_name = decode_url(category_name_url)

	if request.method == 'POST':
		form = PageForm(request.POST)

		if form.is_valid():
			page = form.save(commit = False)

			cat = Category.objects.get(name = category_name)
			page.category = cat

			page.views = 0

			page.save()

			return category(request, category_name_url)

		else:
			print form.errors

	else:
		form = PageForm()

	return render_to_response('rango/add_page.html',
		{'category_name_url': category_name_url,
		'category_name': category_name, 'form':form},
		context)
Esempio n. 9
0
def add_page(request, category_name_slug):
    """
    view for adding news pages
    -it displays the form
    - validates the form
    - updates the database
    -return to success or failure page
    """
    try:
        category = Category.objects.get(slug= category_name_slug)
    except Category.DoesNotExist:
        category = None

    form = PageForm()
    if request.method=="POST":
        form = PageForm(request.POST)
        #check if form is valid
        if form.is_valid():
            if category:
                #create a page object by saving the form           
                page = form.save(commit=False) #don't save the page object yet into the database
                page.category = category
                page.views = 0
                #no save the page object into the database
                page.save()
                #return to the succes page, in this case the index page which also shows the top 5 pages
                return show_category(request, category_name_slug)
        else:
            #form contain invalid date, so print errors
            print form.errors
    context_dict = {'category':category, 'form':form}
    #render the form with error messages if any
    return render(request, 'news/add_page.html', context_dict)
Esempio n. 10
0
def page_edit_static(request, id):
    shop = request.shop

    page = get_object_or_404(Page, pk=id)
    page_name = page.name
    page_name_link = page.name_link
    page_title = page.title
    page_body = page.body
    page_meta_content = page.meta_content

    static_pages = Page.objects.filter(shop=shop)
    dynamic_pages = DynamicPageContent.objects.filter(shop=shop)
    form = PageForm(shop, request.POST or None, instance=page)
    if request.method == "POST":
        if form.is_valid():
            new_page = form.save(commit=False)
            new_page.save()
            version = PageVersion(page=page)
            version.name = page_name
            version.name_link = page_name_link
            version.title = page_title
            version.body = page_body
            version.meta_content = page_meta_content
            version.save()
            request.flash["message"] = unicode(_("Page successfully edited."))
            request.flash["severity"] = "success"
            return HttpResponseRedirect(reverse("page_edit_static", args=[id]))

    return render_to_response(
        "store_admin/web_store/pages_edit_static_page.html",
        {"form": form, "page": page, "static_pages": static_pages, "dynamic_pages": dynamic_pages},
        RequestContext(request),
    )
Esempio n. 11
0
def add_page(request, category_name_slug):
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                # probably better to use a redirect here
                return HttpResponseRedirect('/rango/')
                # return category(request, category_name_slug)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict = {'form': form, 'category': cat, 'category_name_slug': category_name_slug}

    return render(request, 'rango/add_page.html', context_dict)
Esempio n. 12
0
def add_page(request, category_name_slug):
	try:
		cat =  Category.objects.get(slug=category_name_slug)
	except Category.DoesNotExist:
		cat = None 

	if request.method == 'POST':
		form =  PageForm(request.POST)

		if form.is_valid():
			if cat:
				page = form.save(commit=False)
				page.category = cat
				page.views = 0 
				page.save()
				return category(request, category_name_slug)
			else:
				print form.errors
		else:
			print form.errors
	else:
		form = PageForm()

	context_dict = {'form':form, 'category':cat, 'slug':category_name_slug }
	return render(request, 'add_page.html', context_dict)
def page_edit_static(request, id):
    shop = request.shop
    page = get_object_or_404(Page, pk=id)
    static_pages = Page.objects.filter(shop=shop)
    dynamic_pages = DynamicPageContent.objects.filter(shop=shop)
    form = PageForm(shop, request.POST or None, instance=page)
    if request.method == "POST":
        if form.is_valid():
            form.save()
            request.flash['message'] = unicode(_("Page successfully edited."))
            request.flash['severity'] = "success"
            return HttpResponseRedirect(reverse('page_edit_static', args=[id]))
        
    return render_to_response('store_admin/web_store/pages_edit_static_page.html', {
                                       'form': form,
                                       'page': page,
                                       'static_pages': static_pages, 
                                       'dynamic_pages': dynamic_pages
                                       },
                                      RequestContext(request))
Esempio n. 14
0
def page_add(request, response_format='html'):
    "Static Page add"

    if request.POST:
        form = PageForm(request.POST)
        if form.is_valid():
            page = form.save()
            return HttpResponseRedirect(reverse('core_admin_page_view', args=[page.id]))
    else:
        form = PageForm()

    return render_to_response('core/administration/page_add',
                              {'form': form},
                              context_instance=RequestContext(request), response_format=response_format)
Esempio n. 15
0
def page_edit(request, page_id, response_format='html'):
    "Static Page edit"

    page = get_object_or_404(Page, pk=page_id)
    if request.POST:
        form = PageForm(request.POST, instance=page)
        if form.is_valid():
            page = form.save()
            return HttpResponseRedirect(reverse('core_admin_page_view', args=[page.id]))
    else:
        form = PageForm(instance=page)

    return render_to_response('core/administration/page_edit',
                              {'page': page, 'form': form},
                              context_instance=RequestContext(request), response_format=response_format)
Esempio n. 16
0
def add_page(request, category_name_url):
    context = RequestContext(request)

    category_name = decode_url(category_name_url)
    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            # This time we cannot commit straight away.
            # Not all fields are automatically populated!
            page = form.save(commit=False)

            # Retrieve the associated Category object so we can add it.
            # Wrap the code in a try block - check if the category actually exists!
            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                # If we get here, the category does not exist.
                # We render the add_page.html template without a context dictionary.
                # This will trigger the red text to appear in the template!
                return render_to_response('rango/add_page.html', {'category_name': category_name}, context)

            # Also, create a default value for the number of views.
            page.views = 0

            # With this, we can then save our new model instance.
            page.save()
            action.send(request.user, verb='added page', action_object=page, target=cat)
            # Now that the page is saved, display the category instead.
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        try:
            cat = Category.objects.get(name=category_name)
        except Category.DoesNotExist:
            # If we get here, the category does not exist.
            # We render the add_page.html template without a context dictionary.
            # This will trigger the red text to appear in the template!
            return render_to_response('rango/add_page.html', {'category_name': category_name}, context)
        form = PageForm()

    return render_to_response('rango/add_page.html',
                              {'category_name_url': category_name_url,
                               'category_name': category_name, 'form': form},
                              context)
Esempio n. 17
0
def page_create(request):
    shop = request.shop
    static_pages = Page.objects.filter(shop=shop)
    dynamic_pages = DynamicPageContent.objects.filter(shop=shop)
    form = PageForm(shop, request.POST or None)
    if form.is_valid():
        page = form.save(commit=False)
        page.shop = shop
        page.save()
        request.flash["message"] = unicode(_("Page successfully saved."))
        request.flash["severity"] = "success"
        return HttpResponseRedirect(reverse("web_store_pages"))

    return render_to_response(
        "store_admin/web_store/pages_page_create.html",
        {"form": form, "static_pages": static_pages, "dynamic_pages": dynamic_pages},
        RequestContext(request),
    )
Esempio n. 18
0
def add_page(request, category_name_url):
    context = RequestContext(request)
    cat_list = get_category_list()
    context_dict = {}
    context_dict['cat_list'] = cat_list

    category_name = decode_url(category_name_url)
    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            # This time we cannot commit straight away.
            # Not all fields are automatically populated!
            page = form.save(commit=False)

            # Retrieve the associated Category object so we can add it.
            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                return render_to_response('rango/add_page.html',
                                          context_dict,
                                          context)

            # Also, create a default value for the number of views.
            page.views = 0

            # With this, we can then save our new model instance.
            page.save()

            # Now that the page is saved, display the category instead.
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict['category_name_url'] = category_name_url
    context_dict['category_name'] = category_name
    context_dict['form'] = form

    return render_to_response('rango/add_page.html',
                              context_dict,
                              context)
Esempio n. 19
0
def create(request):
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            page = form.save()
            #page = get_object_or_404(Page, id=form.pk)

            view_page_groups_ids = form.cleaned_data['view_page_groups']

            assign_perm_for_groups_id('pages.view_page', page, view_page_groups_ids)

            return HttpResponseRedirect(reverse('administration_pages_index'))
    else:
        form = PageForm()

    return direct_to_template(request, 'pages/administration/pages_create.html',
                              {'form': form,
                               'groups'
                               'active_module': 'pages', })
Esempio n. 20
0
def page_create(request):
    shop = request.shop
    static_pages = Page.objects.filter(shop=shop)
    dynamic_pages = DynamicPageContent.objects.filter(shop=shop)
    form = PageForm(shop, request.POST or None)
    if form.is_valid():
        page = form.save(commit=False)
        page.shop = shop
        page.save()
        request.flash['message'] = unicode(_("Page successfully saved."))
        request.flash['severity'] = "success"
        return HttpResponseRedirect(reverse('web_store_pages'))

    return render_to_response(
        'store_admin/web_store/pages_page_create.html', {
            'form': form,
            'static_pages': static_pages,
            'dynamic_pages': dynamic_pages,
        }, RequestContext(request))
Esempio n. 21
0
def add_page(request, category_name_url):
    """
    Add page to category if it doesn't exist,
    :param request
    :param category_name_url
    """
    category_name = Category._encode_url_name(category_name_url,
                                              Category._url_decode_rules_dict)
    # category_name = "ooops"
    cat = None
    if request.method == "POST":
        form = PageForm(request.POST)

        if form.is_valid():
            # Cannot commit straight away, not all fields automatically populated
            page = form.save(commit=False)

            # Retrieve the associated Category object so we can add it
            # Handle exception for Model.DoesNotExist, Go back and render the add category form
            try:
                cat = Category.objects.get(url=category_name_url)
                page.category = cat
            except Category.DoesNotExist:
                # Category does not exist, Go back and the render the add_category form
                return _process_request(request, {}, "rango/add_category.html")
            # set default value for number of views, and save new model instance
            page.views = 0
            page.save()

            # Now that the page is saved, display the category instead.
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict = {
        "category_name_url": category_name_url,
        "category_name": category_name,
        "form": form,
    }
    return _process_request(request, context_dict, "rango/add_page.html")
Esempio n. 22
0
def add_page(request, category_slug):
    context_dict = {}
    cat = Category.objects.get(slug=category_slug)
    context_dict['category'] = cat
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                return category(request, category_slug)
        else:
            print form.errors
    else:
        form = PageForm()
        context_dict['form'] = form

    return render(request, 'rango/add_page.html', context_dict)
Esempio n. 23
0
def create_page(request, parent=None):
    if parent:
        parent = get_object_or_404(Page, id=parent)

    if request.method == 'POST':
        page_form = PageForm(request.POST, prefix='page_form')

        if page_form.is_valid():
            page = page_form.save(commit=False)
            if parent:
                page.parent = parent
            page.save()
            return redirect('pages:administration:create_page_content', page_id=page.id)
    else:
        page_form = PageForm(prefix='page_form')

    return render(request, 'pages/administration/create_page.html', {
        'parent': parent,
        'page_form': page_form,
     })
Esempio n. 24
0
def page_create(request):
    shop = request.shop
    static_pages = Page.objects.filter(shop=shop)
    dynamic_pages = DynamicPageContent.objects.filter(shop=shop)
    form = PageForm(shop, request.POST or None)
    if form.is_valid():
        page = form.save(commit = False)
        page.shop = shop
        page.save()
        request.flash['message'] = unicode(_("Page successfully saved."))
        request.flash['severity'] = "success"
        return HttpResponseRedirect(reverse('web_store_pages'))
    
    return render_to_response('store_admin/web_store/pages_page_create.html',
                              {
                               'form': form,
                               'static_pages': static_pages,
                               'dynamic_pages': dynamic_pages,
                               },
                              RequestContext(request))    
Esempio n. 25
0
def add_page(request, category_slug):
    context_dict = {}
    cat = Category.objects.get(slug=category_slug)
    context_dict["category"] = cat
    if request.method == "POST":
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.category = cat
                page.views = 0
                page.save()
                return category(request, category_slug)
        else:
            print form.errors
    else:
        form = PageForm()
        context_dict["form"] = form

    return render(request, "rango/add_page.html", context_dict)
Esempio n. 26
0
def add_page(request, category_name_url):
    context = RequestContext(request)
    cat_list = get_category_list()
    context_dict = {}
    context_dict['cat_list'] = cat_list

    category_name = decode_url(category_name_url)
    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            # This time we cannot commit straight away.
            # Not all fields are automatically populated!
            page = form.save(commit=False)

            # Retrieve the associated Category object so we can add it.
            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                return render_to_response('rango/add_page.html', context_dict,
                                          context)

            # Also, create a default value for the number of views.
            page.views = 0

            # With this, we can then save our new model instance.
            page.save()

            # Now that the page is saved, display the category instead.
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict['category_name_url'] = category_name_url
    context_dict['category_name'] = category_name
    context_dict['form'] = form

    return render_to_response('rango/add_page.html', context_dict, context)
Esempio n. 27
0
def add_page(request, category_name_url):
    """
    Add page to category if it doesn't exist,
    :param request
    :param category_name_url
    """
    category_name = Category._encode_url_name(category_name_url, Category._url_decode_rules_dict)
    # category_name = "ooops"
    cat = None
    if request.method == "POST":
        form = PageForm(request.POST)

        if form.is_valid():
            # Cannot commit straight away, not all fields automatically populated
            page = form.save(commit=False)

            # Retrieve the associated Category object so we can add it
            # Handle exception for Model.DoesNotExist, Go back and render the add category form
            try:
                cat = Category.objects.get(url=category_name_url)
                page.category = cat
            except Category.DoesNotExist:
                # Category does not exist, Go back and the render the add_category form
                return _process_request(request, {}, "rango/add_category.html")
            # set default value for number of views, and save new model instance
            page.views = 0
            page.save()

            # Now that the page is saved, display the category instead.
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict = {
        "category_name_url": category_name_url,
        "category_name": category_name,
        "form": form,
    }
    return _process_request(request, context_dict, "rango/add_page.html")
def add_page(request,category_name_url):
	try:
		cat = Category.objects.get(slug=category_name_url)
	except Category.DoesNotExist:
		cat = None	
	if request.method == 'POST':
		form	=	PageForm(request.POST)

		if form.is_valid():
			if cat:
				page = form.save(commit=False)
				page.Category = cat
				page.views = 0
				page.save()
			return category(request,category_name_url)
		else:
			print form.errors
	else:
		form	=	PageForm()

		return render(request,'add_page.html',{'form':form})
Esempio n. 29
0
def add_page(request, category_name_url):

    context = RequestContext(request)
    cat_list = get_category_list()
    context_dict = {}
    context_dict['cat_list'] = cat_list

    category_name = decode_url(category_name_url)

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():

            page = form.save(commit=False)

            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                return render_to_response( 'rango/add_page.html',
                                          context_dict,
                                          context)

            page.views = 0
            page.first_visit = datetime.datetime.now()
            page.save()
            # probably better to use a redirect here.
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict['category_name_url']= category_name_url
    context_dict['category_name'] =  category_name
    context_dict['form'] = form

    return render_to_response( 'rango/add_page.html',
                               context_dict,
                               context)
Esempio n. 30
0
def edit(request, page_id):

    page = get_object_or_404(Page, id=page_id)

    old_page_groups_ids = get_group_ids_for_object_perm(u'view_page', page)

    if request.method == 'POST':
        form = PageForm(request.POST, instance=page)

        if form.is_valid():
            page = form.save()
            new_page_groups_ids = form.cleaned_data['view_page_groups']
            edit_group_perms_for_object('view_page', page, old_page_groups_ids, new_page_groups_ids)
            return HttpResponseRedirect(reverse('administration_pages_index'))
    else:
        init = model_to_dict(page)
        init['view_page_groups'] = old_page_groups_ids
        form = PageForm(init, instance=page)
    return direct_to_template(request, 'pages/administration/pages_edit.html',
                              {'form': form,
                               'page': page,
                               'active_module': 'pages'})
Esempio n. 31
0
def add_page(request, category_name_url):
    context = RequestContext(request)

    category_name = decode_url(category_name_url)
    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            # This time we cannot commit straight away.
            # Not all fields are automatically populated!
            page = form.save(commit=False)

            # Retrieve the associated Category object so we can add it.
            # Wrap the code in a try block - check if the category actually exists!
            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:
                # If we get here, the category does not exist.
                # Go back and render the add category form as a way of saying the category does not exist.
                return render_to_response('rango/add_category.html', {}, context)

            # Also, create a default value for the number of views.
            page.views = 0

            # With this, we can then save our new model instance.
            page.save()

            # Now that the page is saved, display the category instead.
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    return render_to_response( 'rango/add_page.html',
            {'category_name_url': category_name_url,
             'category_name': category_name, 'form': form},
             context)
Esempio n. 32
0
def add_page(request, category_name_slug):
    try:
        category = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        category = None

    form = PageForm()
    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if category:
                page = form.save(commit=False)
                page.category = category
                page.views = 0
                page.save()
                # probably better to use a redirect here.
            return show_category(request, category_name_slug)
        else:
            print(form.errors)

    context_dict = {'form':form, 'category': category}

    return render(request, 'rango/add_page.html',context=context_dict)
Esempio n. 33
0
def add_page(request, category_name_slug):
    print category_name_slug
    try:
        cat = Category.objects.get(slug=category_name_slug)
    except Category.DoesNotExist:
        cat = None

    if request.method == 'POST':
        form = PageForm(request.POST)
        if form.is_valid():
            if cat:
                page = form.save(commit=False)
                page.views = 0
                page.category = cat
                page.save()
                return category(request, category_name_slug)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dic = {'form': form, 'category': cat, 'slug': category_name_slug}
    print context_dic
    return render(request, 'blog/add_page.html', context_dic)
Esempio n. 34
0
def create(request, pk=None):
    # make the instance
    try:
        if pk is not None:
            instance = get_object_or_404(Page, pk=pk)
        else:
            instance = None
    except ValueError:
        raise Http404
    # make the formset
    PageImageFormSet = inlineformset_factory(Page,PageImage,form=PageImageForm, extra=3)
    # process post
    if request.method == 'POST':
        form = PageForm(request.POST, instance=instance)
        pageimage_formset = PageImageFormSet(request.POST, request.FILES, instance=instance, prefix="pageimage")
        
        if form.is_valid():
            page = form.save()
            # process the images
            if pageimage_formset.is_valid():
                # if the instance was none - remake the form to bind object page to it
                if instance is None:
                    pageimage_formset = PageImageFormSet(request.POST, request.FILES, instance=page, prefix="pageimage")
                pageimages = pageimage_formset.save()
            # all good, return
            messages.success(request, u'%s has been saved' % page.title)
            return HttpResponseRedirect(page.get_absolute_url())
    
    if request.method == 'GET':
        form = PageForm(instance=instance)
        pageimage_formset = PageImageFormSet(instance=instance, prefix="pageimage")
    
    return TemplateResponse(request, 'pages/page_create.html', {
        'form' : form,
        'pageimage_formset' : pageimage_formset
    })