Skip to content

Commit

Permalink
Use django messages framework for user prompts.
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan2412 committed Jan 25, 2013
1 parent 428fe95 commit 16e6089
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
30 changes: 17 additions & 13 deletions CodeStreak/contests/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.http import HttpResponse, Http404
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.db import IntegrityError
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.shortcuts import render_to_response
from django.http import Http404
from django.views.decorators.http import require_POST
Expand Down Expand Up @@ -65,7 +65,7 @@ def contest_list(request):
title = 'CodeStreak'

page = \
<cs:page user={request.user} title={title}>
<cs:page request={request} title={title}>
<cs:header-home />
<cs:content>
<h2>Contest List</h2>
Expand All @@ -87,7 +87,7 @@ def contest_home(request, contest_id):
title = 'CodeStreak - {}'.format(contest.name)
content = <textarea />
page = \
<cs:page user={request.user} title={title}>
<cs:page request={request} title={title}>
<cs:header-contest contest={contest} active_tab="contest-home" />
<cs:content>{content}</cs:content>
<cs:footer />
Expand Down Expand Up @@ -136,7 +136,7 @@ def contest_ranking(request, contest_id):
title = 'CodeStreak - {}'.format(contest.name)
content = <textarea />
page = \
<cs:page user={request.user} title={title}>
<cs:page request={request} title={title}>
<cs:header-contest contest={contest} active_tab="contest-ranking" />
<cs:content>{content}</cs:content>
<cs:footer />
Expand Down Expand Up @@ -168,28 +168,32 @@ def contest_ranking(request, contest_id):
def register_to_contest(request, contest_id):
try:
contest = Contest.get_contest(contest_id)
contest_url = url_reverse('contest-home', args=(contest_id,))
Participation.register_user(contest_id, request.user.id)
except Contest.DoesNotExist:
raise Http404
except IntegrityError:
return HttpResponse("You are already signed up for this contest!")
messages.error(request, 'You are already signed up for this contest!')
return HttpResponseRedirect(contest_url)

page = ''
page += '<p>Registration complete for {}</p>'.format(contest)
return HttpResponse(str(page))
messages.info(request,
'Registration complete for {}'.format(contest.name))
return HttpResponseRedirect(contest_url)


@require_POST
@login_required
def unregister_from_contest(request, contest_id):
try:
contest = Contest.get_contest(contest_id)
contest_url = url_reverse('contest-home', args=(contest_id,))
Participation.unregister_user(contest_id, request.user.id)
except Contest.DoesNotExist:
raise Http404
except Participation.DoesNotExist:
return HttpResponse("You are not signed up for this contest!")
messages.error(request, 'You are not signed up for this contest!')
return HttpResponseRedirect(contest_url)

page = ''
page += '<p>You are no longer registered for {}</p>'.format(contest)
return HttpResponse(str(page))
messages.info(request,
'You are no longer registered for {}'.format(contest.name))
return HttpResponseRedirect(contest_url)
2 changes: 1 addition & 1 deletion CodeStreak/static/base/css/cs-base.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
body {
padding-top: 50px;
padding-top: 60px;
}

.navbar-contest-name {
Expand Down
22 changes: 18 additions & 4 deletions CodeStreak/xhpy/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Copyright 2012 Bogdan-Cristian Tataroiu

from django.conf import settings
from django.contrib import messages

from CodeStreak.xhpy.lib import *


class :cs:page(:x:element):
attribute str title @required,
object user
object request @required
children :cs:header, :cs:content, :cs:footer

def render(self):
Expand All @@ -27,11 +28,14 @@ def render(self):
<title>{self.getAttribute('title')}</title>
</head>

user = self.getAttribute('user')
request = self.getAttribute('request')
# Compose body of page
for child in self.getChildren():
if user and 'user' in child._xhpyAttributeDeclaration():
child.setAttribute('user', user)
if 'user' in child._xhpyAttributeDeclaration():
child.setAttribute('user', request.user)
if 'messages' in child._xhpyAttributeDeclaration():
child.setAttribute('messages',
messages.get_messages(request))
if isinstance(child, :cs:header):
header = child
elif isinstance(child, :cs:content):
Expand Down Expand Up @@ -126,11 +130,21 @@ def render(self):


class :cs:content(:x:element):
attribute list messages @required
children any

def render(self):
messages = self.getAttribute('messages')
messages_xhp = <div class="alerts" />
for message in messages:
messages_xhp.appendChild(
<div class={"alert alert-{}".format(message.tags)}>
{message.message}
</div>)

return \
<div class="main-content">
{messages_xhp}
{self.getChildren()}
</div>

Expand Down

0 comments on commit 16e6089

Please sign in to comment.