Source

beerfingers / bf / bar / views.py

Full commit
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response, get_object_or_404
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.contrib.auth.models import User
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.views.generic.create_update import create_object, update_object, delete_object
from bouncer import profile
from bar.models import Satisfaction, ExpState, Inbox, Journal, Beers
from bouncer.models import UserProfile
from bar.forms import JournalForm


@login_required
def journal(request, brewery_slug, beer_slug):
	#user_profile = profile.retrieve(request)
	profile = request.user.get_profile()
	if request.method == 'POST':
		form = JournalForm(data=request.POST, files=request.FILES)
		if form.is_valid():
			profile_obj = form.save(commit=False)
			profile_obj.beerhead = request.user
			#import pdb; pdb.set_trace()
			profile_obj.brewery_id = form.data['brewery']
			profile_obj.beer_id = form.data['beer']
			profile_obj.save()
			
			if hasattr(form, 'save_m2m'):
			    form.save_m2m()
			return HttpResponseRedirect(reverse('bar.views.index_journal')) #, args=[profile_obj.id]
	else:
		setformvalues = Beers.objects.filter(beer_slug=beer_slug)
		form = JournalForm()
	
	journal_list = Journal.objects.filter(beerhead=request.user).order_by('-datetime')
	#inbox_list = Inbox.objects.filter(Q(source=request.user.email) | Q(source=profile.cell1)).order_by('-datetime') #cell1
	paginator = Paginator(journal_list, 10) # Show 5 posts per page

    # Make sure page request is an int. If not, deliver first page.
	try:
		page = int(request.GET.get('page', '1'))
	except ValueError:
		page = 1

    # If page request (9999) is out of range, deliver last page of results.
	try:
		journal_list = paginator.page(page)
	except (EmptyPage, InvalidPage):
		journal_list = paginator.page(paginator.num_pages)

	return render_to_response('bar/bar_index.html', locals(),context_instance=RequestContext(request))	

@login_required
def index_journal(request):
	# http://dev.beerfingers.com/sms/?user=\p&req=\0
	profile = request.user.get_profile()
	journal_list = Journal.objects.filter(beerhead=request.user).order_by('-datetime')
	inbox_list = Inbox.objects.filter(Q(source=request.user.email) | Q(source=profile.cell1)).order_by('-datetime') #cell1
	paginator = Paginator(journal_list, 20) # Show 5 posts per page

    # Make sure page request is an int. If not, deliver first page.
	try:
		page = int(request.GET.get('page', '1'))
	except ValueError:
		page = 1

    # If page request (9999) is out of range, deliver last page of results.
	try:
		journal_list = paginator.page(page)
	except (EmptyPage, InvalidPage):
		journal_list = paginator.page(paginator.num_pages)

	return render_to_response('bar/index_journal.html', locals(),context_instance=RequestContext(request))

@login_required
def edit_journal(request, id):
	if request.method == 'POST':
		#form.beerhead = request.user
		return update_object(request, form_class=JournalForm, object_id=id, post_save_redirect=(reverse('bar.views.view_journal', args=[id] )))
	else:
		edit_journal = get_object_or_404(Journal, pk=id)
		form = JournalForm(instance=edit_journal)
		
	return render_to_response('bar/edit_journal.html', locals(),context_instance=RequestContext(request))
	
@login_required
def inbox(request):
	user_profile = profile.retrieve(request)
	inbox_list = Inbox.objects.filter(Q(source=request.user.email) | Q(source=user_profile.cell1)).order_by('-datetime')
	paginator = Paginator(inbox_list, 10) # Show 5 posts per page

    # Make sure page request is an int. If not, deliver first page.
	try:
		page = int(request.GET.get('page', '1'))
	except ValueError:
		page = 1

    # If page request (9999) is out of range, deliver last page of results.
	try:
		inbox_list = paginator.page(page)
	except (EmptyPage, InvalidPage):
		inbox_list = paginator.page(paginator.num_pages)
		
	return render_to_response('bar/inbox.html', locals(),context_instance=RequestContext(request))

@login_required
def view_journal(request, id):
	j = Journal.objects.filter(beerhead=request.user).filter(pk=id)
	return render_to_response('bar/view_journal.html', locals(),context_instance=RequestContext(request))

@login_required
def del_inbox(request, id):
	return delete_object(request, Inbox, id, post_delete_redirect=(reverse('bar.views.journal')))