Source

seedswapapp / seedswap / seed / views.py

# Create your views here.

import json

from django.shortcuts import render_to_response, redirect
from django.http import HttpResponse

from seed import forms

from django.contrib.auth.decorators import login_required
from django.contrib import auth
from django.template import RequestContext

from seed.models import UserProfile, Species, Varieties, Seed
from seed.forms import SearchForm, InsertSeedForm, InsertSpeciesForm, InsertVarietiesForm, InsertUserForm

def add_seeds(request):
    """
    A simple form to add a stock of seeds.
    TODO: add login requirement
    """
    if request.method == 'POST':
        myForm = InsertSeedForm(request.POST)
        if myForm.is_valid():
            seeditem = myForm.save(commit=False)
            seeditem.user = request.user
            seeditem.save()
            return HttpResponse('Record correctly inserted.')
    else:
        myForm = InsertSeedForm()
    return render_to_response("insertSeed.html",
                              dict(form=myForm),
                              context_instance = RequestContext(request))

def add_species(request):
    """ Minimalist form to insert specie. """
    if request.method == 'POST':
        myForm = InsertSpeciesForm(request.POST)
        if myForm.is_valid():
            myForm.save()
            return HttpResponse('Record correctly inserted.')
    else:
        myForm = InsertSpeciesForm()
    return render_to_response("insertSeed.html", {'logged' : request.user.is_authenticated(), "form": myForm})

def add_user(request):
    """ Minimalist form to insert specie. """
    if request.method == 'POST':
        myForm = InsertUserForm(request.POST)
        if myForm.is_valid():
            myForm.save()
            return HttpResponse('Record correctly inserted.')
    else:
        myForm = InsertUserForm()
    return render_to_response("insertSeed.html",
                              dict(form = myForm),
                              context_instance=RequestContext(request))

def add_variety(request):
    """ Minimalist form to insert variety. """
    if request.method == 'POST':
        myForm = InsertVarietiesForm(request.POST)
        if myForm.is_valid():
            myForm.save()
            return HttpResponse('Record correctly inserted.')
    else:
        myForm = InsertVarietiesForm()
    return render_to_response("insertSeed.html",
                              dict(form = myForm),
                              context_instance=RequestContext(request))

def index(request):
    return render_to_response('index.html',
                              context_instance=RequestContext(request))


def contacts(request):
    return render_to_response('contacts.html',
                              context_instance=RequestContext(request))
def about(request):
    return render_to_response('about.html',
                              context_instance=RequestContext(request))

def search(request):

    if request.method == 'POST':
        form = forms.SearchForm(request.POST)
        if form.is_valid() or 1:
            all_seeds = Seed.objects.all()
            rdata = ( (45.000, 11.000), )
            return HttpResponse(json.dumps(rdata), mimetype="application/json")
    else:
        form = forms.SearchForm()
        return render_to_response('search.html',
                                  dict(form = form),
                                  context_instance=RequestContext(request))

def view(request, seed_id):
    one_seed = Seed.objects.get(pk=seed_id)
    return render_to_response('view.html',
                              dict(data = one_seed),
                              context_instance=RequestContext(request))

def login(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']

        user = auth.authenticate(username=username, password=password)
        if user is not None and user.is_active:
            auth.login(request, user)
            return redirect('/')
        else:
            return redirect('http://gtfo.org/')
    else:
        return render_to_response('login.html',
                                  context_instance=RequestContext(request))


def logout(request):
    auth.logout(request)
    return redirect('/')

@login_required
def dashboard(request):
    user = request.user
    return render_to_response('dashboard.html',
                              context_instance=RequestContext(request))