I would like to hear some opinions from more experienced mezzaniners.
I'm building a store for a friend and trying to make it sort of single page.
So for my homepage I want to include contact form. Instead of hardcoding it
I want to be able to edit it from admin interface.

So this is what I came up with:

models.py

class HomePage(Page, RichText):
    """
    Landing page
    """
    featured_shop = models.ForeignKey("shop.Category",
                                      blank=True,
                                      null=True,
                                      )
    contact_form = models.OneToOneField("forms.Form",
                                        default=None,
                                        blank=True,
                                        null=True,
                                        )


page_processors.py

def create_contact_form(page, request):
    """
    Create contact form if it is added to the homepage
    """
    # get contact form created in the admin interface
    form = FormForForm(page.homepage.contact_form, RequestContext(request),
                       request.POST or None)
    if form.is_valid():
        url = page.get_absolute_url() + "?sent=1"
        if is_spam(request, form, url):
            return redirect(url)
        attachments = []
        for f in form.files.values():
            f.seek(0)
            attachments.append(f.name, f.read())
        entry = form.save()
        subject = page.homepage.contact_form.email_subject
        if not subject:
            subject = "%s - %s" % (page.homepage.contact_form.title, 
entry.entry_time)
        fields = [(v.label, format_value(form.cleaned_data[k]))
                  for (k, v) in form.fields.items()]
        context = {
            "fields": fields,
            "message": page.homepage.contact_form.email_message,
            "request": request,
        }
        email_from = page.homepage.contact_form.email_from or 
settings.DEFAULT_FROM_EMAIL
        email_to = form.email_to()
        if email_to and page.homepage.contact_form.send_email:
            send_mail_template(subject, "email/form_response", email_from,
                               email_to, context)

        headers = None
        if email_to:
            # Add the email entered as a Reply-To header
            headers = {'Reply-To': email_to}
        email_copies = split_addresses(page.homepage.contact_form.email_copies)
        if email_copies:
            send_mail_template(subject, "email/form_response_copies",
                               email_from, email_copies, context,
                               attachments=attachments, headers=headers)
        form_valid.send(sender=request, form=form, entry=entry)
        return redirect(url)
    form_invalid.send(sender=request, form=form)
    return form


@processor_for(HomePage)
def homepage_processor(request, page):
    """
    Add products from store to the homepage and pass contact
    form created in admin interface
    """
    # create contact form
    if page.homepage.contact_form:
        form = create_contact_form(page, request)
    else:
        form = None

    return {"products": products, "form": form}


pages/index.html

{% extends "pages/form.html" %}

...

{% if page.homepage.contact_form %}
    {# Contact Section #}
    <section id="contact" class="contact-section">
        <div class="container">
            <div class="row">
                <div class="col-md-4 col-sm-6 col-xs-12">
                    <h1>Contact</h1>
                    {% block main %}
                        {{ block.super }}
                    {% endblock %}
                </div>
            </div>
        </div>
    </section>
{% endif %}


My page_processor.py is exact copy of the 
forms.page_processors.form_processor with
few extra tweeks. Thing is it's not really DRY because it's repeating the 
same logic.
So is there a better way to do it? For example one of the ideas I had was 
try to use solution from Python Cookbook 
<http://chimera.labs.oreilly.com/books/1230000000393/ch09.html#_unwrapping_a_decorator>
.
But problem is it's only possible to use it if decorator is implemented 
with @wraps decorator.
So maybe it could be an idea for future improvements. Or I'm just missing 
something and there
is already and easy way to use those forms?

-- 
You received this message because you are subscribed to the Google Groups 
"Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to