[pirate-politics] r558 committed - fixed redirect issues for registration, login, and logout. Although lo...

2 views
Skip to first unread message

pirate-...@googlecode.com

unread,
Feb 9, 2011, 3:36:06 AM2/9/11
to pirate-poli...@googlegroups.com
Revision: 558
Author: fragro
Date: Wed Feb 9 00:35:14 2011
Log: fixed redirect issues for registration, login, and logout. Although
logout still needs some work. Also added return_path and return_query as
arguments for pp_url so the template author can pass in
request.META.PATH_INFO and request.META.QUERY_STRING. pp_url then populates
the request WSGI object with a returnurl string containing this
information. This allows these redirects to take place. Some other simple
fixes.
http://code.google.com/p/pirate-politics/source/detail?r=558

Added:
/trunk/pirate-politics/pirate_login/templatetags
/trunk/pirate-politics/pirate_login/templatetags/__init__.py
/trunk/pirate-politics/pirate_login/templatetags/usertags.py
Modified:
/trunk/pirate-politics/new_templates/_rightcol.html
/trunk/pirate-politics/new_templates/base.html
/trunk/pirate-politics/new_templates/register.html
/trunk/pirate-politics/new_templates/submit_issue.html
/trunk/pirate-politics/new_templates/submit_nay_argument.html
/trunk/pirate-politics/new_templates/submit_solution.html
/trunk/pirate-politics/new_templates/submit_yea_argument.html
/trunk/pirate-politics/pirate_core/middleware.py
/trunk/pirate-politics/pirate_core/templatetags/pp_url.py
/trunk/pirate-politics/pirate_core/views.py
/trunk/pirate-politics/pirate_issues/templatetags/issuetags.py
/trunk/pirate-politics/pirate_issues/templatetags/solutiontags.py
/trunk/pirate-politics/pirate_login/views.py

=======================================
--- /dev/null
+++ /trunk/pirate-politics/pirate_login/templatetags/usertags.py Wed Feb 9
00:35:14 2011
@@ -0,0 +1,180 @@
+from django import template
+from django import forms
+from django.http import HttpResponseRedirect
+from django.shortcuts import get_object_or_404
+import datetime
+from django.contrib.auth.models import User
+from django.contrib.contenttypes.models import ContentType
+from pirate_core import HttpRedirectException, namespace_get, FormMixin
+from pirate_issues.models import Topic, Issue
+from pirate_consensus.models import UpDownVote, Consensus
+from django.utils.translation import ugettext as _
+from pirate_reputation.models import ReputationDimension
+from django.contrib.auth import authenticate, login
+from django.core.validators import validate_email
+from pirate_core.views import template_for_model
+
+from pirate_signals.models import aso_rep_event
+from pirate_login.models import Login, Register
+
+from customtags.decorators import block_decorator
+register = template.Library()
+block = block_decorator(register)
+
+'''
+This file contains all of the tags that pertain to User/Login objects
+'''
+
+
+# this function assignment lets us reuse the same code block a bunch of
places
+get_namespace = namespace_get('pp_login')
+
+
+@block
+def pp_user_registration_form(context, nodelist, *args, **kwargs):
+ '''form_id = forms.CharField(widget=forms.HiddenInput(),
initial="pp_user_registration_form")
+ This block tag can create or process forms either to create or to
modify issues.
+ Usage is as follows:
+
+ {% pp_user_login_form request=request
return_path=request.MATH.PATH_INFO return_query=request.META.QUERY_STRING %}
+ Do stuff with {{ pp_login.form }}.
+ {% endpp_user_login_form%}
+ '''
+ context.push()
+ namespace = get_namespace(context)
+
+ request = kwargs.get('request')
+ return_path= kwargs.get('return_path')
+ return_query= kwargs.get('return_query')
+ returnurl = kwargs.get('returnurl') #this is for register.html
+
+ if request is not None:
+ POST = request.POST
+ else:
+ raise ValueError("Tag pp_user_login_form must contain the
argument 'request=request'")
+
+ if return_path is not None and return_query is not None:
+ returnurl = return_path + '?' + return_query
+ elif returnurl is not None:
+ returnurl = returnurl.replace('%','&')
+ else:
+ returnurl = '/issues.html?_s=0&_e=20&_d=hot'
+
+ if POST and POST.get("form_id") == "pp_user_registration_form":
+ try:
+ form = RegisterForm(POST)
+ if form.is_valid():
+ new_user = form.save()
+ if new_user.password1 == new_user.password2:
+ #check to see if there are other users with this name
+ try:
+ ui = User.objects.get(username=new_user.name)
+ namespace['errors'] = "Sorry that username is
taken already, please pick another."
+ except:
+ user = User.objects.create_user(new_user.name,
new_user.email, new_user.password1)
+ user = authenticate(username=new_user.name,
password=new_user.password1)
+ if user is not None:
+ if user.is_active:
+ login(request, user)
+ raise
HttpRedirectException(HttpResponseRedirect(returnurl))
+ else:
+ namespace['errors'] = "Passwords are not the same. Try
again."
+
+ except HttpRedirectException, e:
+ raise e
+ else:
+ form = RegisterForm()
+
+ namespace['form'] = form
+ output = nodelist.render(context)
+ context.pop()
+
+ return output
+
+
+class RegisterForm(forms.ModelForm):
+ def save(self, commit=True):
+ new_user = super(RegisterForm, self).save(commit=False)
+ return new_user
+
+ class Meta:
+ model = Register
+
+ form_id = forms.CharField(widget=forms.HiddenInput(),
initial="pp_user_registration_form")
+ password1 =
forms.CharField(label=_(u'Password'),widget=forms.PasswordInput(render_value=False))
+ password2 = forms.CharField(label=_(u'Password
Check'),widget=forms.PasswordInput(render_value=False))
+ email = forms.CharField(label=_(u'Email'),validators =
[validate_email])
+
+
+
+@block
+def pp_user_login_form(context, nodelist, *args, **kwargs):
+ '''form_id = forms.CharField(widget=forms.HiddenInput(),
initial="pp_user_login_form")
+ This block tag can create or process forms either to create or to
modify issues.
+ Usage is as follows:
+
+ {% pp_user_login_form request=request
return_path=request.MATH.PATH_INFO return_query=request.META.QUERY_STRING %}
+ Do stuff with {{ pp_login.form }}.
+ {% endpp_user_login_form%}
+ '''
+ context.push()
+ namespace = get_namespace(context)
+
+ request = kwargs.get('request')
+ return_path= kwargs.get('return_path')
+ return_query= kwargs.get('return_query')
+ returnurl = kwargs.get('returnurl') #this is for register.html
+
+ if request is not None:
+ POST = request.POST
+ else:
+ raise ValueError("Tag pp_user_login_form must contain the
argument 'request=request'")
+ if return_path is not None and return_query is not None:
+ returnurl = return_path + '?' + return_query
+ elif returnurl is not None:
+ returnurl = returnurl.replace('%','&')
+ elif return_path is not None and return_query is None:
+ returnurl = return_path
+ else:
+ returnurl = '/issues.html?_s=0&_e=20&_d=hot'
+
+ if POST and POST.get("form_id") == "pp_user_login_form":
+ try:
+ form = LoginForm(POST)
+ if form.is_valid():
+ new_user = form.save()
+ user = authenticate(username=new_user.name,
password=new_user.password)
+ if user is not None:
+ if user.is_active:
+ login(request, user)
+ else: #TODO: form is erroneous somehow, need to find out
how and add to error text
+ pass
+
+ raise
HttpRedirectException(HttpResponseRedirect(returnurl))
+ else: #TODO: form is empty in some way, add this message to
error
+ pass
+
+ except HttpRedirectException, e:
+ raise e
+ else:
+ form = LoginForm()
+
+ namespace['form'] = form
+ output = nodelist.render(context)
+ context.pop()
+
+ return output
+
+
+
+class LoginForm(forms.ModelForm):
+
+ def save(self, commit=True):
+ new_user = super(LoginForm, self).save(commit=False)
+ return new_user
+
+ class Meta:
+ model = Login
+ form_id = forms.CharField(widget=forms.HiddenInput(),
initial="pp_user_login_form")
+ name = forms.CharField(label=_(u'Name'))
+ password =
forms.CharField(label=_(u'Pass'),widget=forms.PasswordInput(render_value=False))
=======================================
--- /trunk/pirate-politics/new_templates/_rightcol.html Sun Feb 6 23:05:17
2011
+++ /trunk/pirate-politics/new_templates/_rightcol.html Wed Feb 9 00:35:14
2011
@@ -10,7 +10,7 @@

<ul class="d">
<div id="login" style="display:none; overflow:hidden; height:85px;">
- {% pp_user_login_form POST=request.POST path=request.path
request=request object=request.object %}
+ {% pp_user_login_form request=request
return_path=request.META.PATH_INFO return_query=request.META.QUERY_STRING %}
{{ pp_login.form.errors }}
<form method="post" action="">
<li>name: {{ pp_login.form.name }}</li>
=======================================
--- /trunk/pirate-politics/new_templates/base.html Sun Feb 6 23:09:47 2011
+++ /trunk/pirate-politics/new_templates/base.html Wed Feb 9 00:35:14 2011
@@ -185,7 +185,7 @@
{% endpp_get_reputation %}
{% else %}
<ul>welcome:
- <li><a STYLE="text-decoration:none"
href="/register.html">register</a></li> |
+ <li><a STYLE="text-decoration:none" href="{% pp_url
template='register.html' return_path=request.META.PATH_INFO
return_query=request.META.QUERY_STRING %}">register</a></li> |
<li><a href="javascript:;"
onmousedown="toggleSlide('login');">login</a></li>
{% endif %}
</ul>
=======================================
--- /trunk/pirate-politics/new_templates/register.html Sun Feb 6 23:05:17
2011
+++ /trunk/pirate-politics/new_templates/register.html Wed Feb 9 00:35:14
2011
@@ -17,7 +17,7 @@

{% block content%}

-{% pp_user_registration_form POST=request.POST path=request.path
request=request %}
+{% pp_user_registration_form returnurl=request.returnurl request=request %}
<div class="register_title">
<h2>Welcome to OpenAssembly: Alpha</h2>
</div>
@@ -44,7 +44,7 @@
</div>
<div class="register_login">
<h3> Login to existing account!</h3>
- {% pp_user_login_form POST=request.POST path=request.path
request=request %}
+ {% pp_user_login_form request=request
returnurl=request.returnurl %}
{{ pp_login.form.errors }}
<form method="post" action="">
Username:
=======================================
--- /trunk/pirate-politics/new_templates/submit_issue.html Sun Feb 6
23:05:17 2011
+++ /trunk/pirate-politics/new_templates/submit_issue.html Wed Feb 9
00:35:14 2011
@@ -18,8 +18,8 @@
<h2> {{pp_issue.contextname }}</h2>
<div class="errors">{{ pp_issue.form.errors }}</div>
<form method="post" action="">
- Name: <br>{{ pp_issue.form.name }}<br>
- Text: <br>{{ pp_issue.form.text }}
+ Summary of Issue: <br>{{ pp_issue.form.name }}<br>
+ Description: <br>{{ pp_issue.form.text }}
{{ pp_issue.form.form_id}}
{% csrf_token %}
<input type="submit" class="button green" value="Submit Issue" />
=======================================
--- /trunk/pirate-politics/new_templates/submit_nay_argument.html Tue Feb
8 19:28:28 2011
+++ /trunk/pirate-politics/new_templates/submit_nay_argument.html Wed Feb
9 00:35:14 2011
@@ -16,8 +16,8 @@
<h2>Submit a new Nay argument for <br>{{request.object.name}}</h2>
<div class="errors">{{ pp_argumentation.form.errors }}</div>
<form method="post" action="">
- Name:<br>{{ pp_argumentation.form.name }}<br>
- Argument:<br> {{ pp_argumentation.form.text }}
+ Summary of Argument:<br>{{ pp_argumentation.form.name }}<br>
+ Description:<br> {{ pp_argumentation.form.text }}
{{ pp_argumentation.form.form_id}}
{% csrf_token %}
<input type="submit" class="button green" value="Submit Nay" />
=======================================
--- /trunk/pirate-politics/new_templates/submit_solution.html Tue Feb 8
19:28:28 2011
+++ /trunk/pirate-politics/new_templates/submit_solution.html Wed Feb 9
00:35:14 2011
@@ -17,8 +17,8 @@
<h2>Submit a new solution for: <br>{{request.object.name}}</h2>
<div class="errors">{{ pp_solution.form.errors }}</div>
<form method="post" action="">
- Name: <br>{{ pp_solution.form.name }}<br>
- Text: <br>{{ pp_solution.form.text }}
+ Summary of Solution: <br>{{ pp_solution.form.name }}<br>
+ Description: <br>{{ pp_solution.form.text }}
{{ pp_solution.form.form_id}}
{% csrf_token %}
<input type="submit" class="button green" value="Submit Solution" />
=======================================
--- /trunk/pirate-politics/new_templates/submit_yea_argument.html Tue Feb
8 19:28:28 2011
+++ /trunk/pirate-politics/new_templates/submit_yea_argument.html Wed Feb
9 00:35:14 2011
@@ -11,13 +11,13 @@
<div class="stage">
<div class="issuemask">
<div class="submissionbound">
- <h2>Submit a new Yea argument for: <br>{{request.object.name}}</h2>
+ <h2>Submit a new Yea argument for:
<fragrobr>{{request.object.name}}</h2>
{% pp_argument_form POST=request.POST path=request.path
object=request.object request=request arg_type='yea' %}
<div class="add">
<div class="errors">{{ pp_argumentation.form.errors }}</div>
<form method="post" action="">
- Name: <br>{{ pp_argumentation.form.name }}<br>
- Argument: <br>{{ pp_argumentation.form.text }}<br>
+ Summary of Argument: <br>{{ pp_argumentation.form.name }}<br>
+ Description: <br>{{ pp_argumentation.form.text }}<br>
{{ pp_argumentation.form.form_id}}
{% csrf_token %}
<input type="submit" class="button green" value="Submit Yea" />
=======================================
--- /trunk/pirate-politics/pirate_core/middleware.py Sun Feb 6 23:05:17
2011
+++ /trunk/pirate-politics/pirate_core/middleware.py Wed Feb 9 00:35:14
2011
@@ -7,6 +7,7 @@
END_KEY = "_e"
DIM_KEY = "_d"
SCROLL_KEY = "_c"
+RETURN_KEY = "_r"

class UrlMiddleware(object):
"""
@@ -25,6 +26,7 @@
end = request.GET.get(END_KEY)
dim = request.GET.get(DIM_KEY)
scroll_to = request.GET.get(SCROLL_KEY)
+ returnurl = request.GET.get(RETURN_KEY)

if content_type_id is not None and obj_id is not None:
content_type = ContentType.objects.get(pk=content_type_id)
@@ -43,6 +45,8 @@
n+=1
request.rangelist = rangelist

+ if returnurl is not None:
+ request.returnurl = returnurl

if dim is not None:
request.dimension = dim
=======================================
--- /trunk/pirate-politics/pirate_core/templatetags/pp_url.py Sun Feb 6
23:05:17 2011
+++ /trunk/pirate-politics/pirate_core/templatetags/pp_url.py Wed Feb 9
00:35:14 2011
@@ -9,7 +9,7 @@
register = template.Library()
function = function_decorator(register)

-from pirate_core.middleware import TYPE_KEY, OBJ_KEY, START_KEY, END_KEY,
DIM_KEY, SCROLL_KEY
+from pirate_core.middleware import TYPE_KEY, OBJ_KEY, START_KEY, END_KEY,
DIM_KEY, SCROLL_KEY, RETURN_KEY

'''
This file contains the tag responsible for creating useful urls within pp
templates.
@@ -68,6 +68,8 @@
end = kwargs.pop('end', None)
dimension = kwargs.pop('dimension',None)
scroll_to = kwargs.pop('scroll_to',None) #argument for javascript
scroll_to function
+ return_path = kwargs.pop('return_path',None)
+ return_query = kwargs.pop('return_query',None)

pattern = kwargs.pop('view', 'pp-page')

@@ -105,16 +107,16 @@
rev_kwargs['end'] = end
if dimension is not None:
rev_kwargs['dimension'] = dimension
- output = get_reverse(pattern, kwargs, **rev_kwargs)
- else:
- output = get_reverse(pattern, kwargs, **rev_kwargs)
+ if return_path is not None and return_query:
+ rev_kwargs['returnurl'] = return_path + '?' + return_query
+ output = get_reverse(pattern, kwargs, **rev_kwargs)

#need to append reverse of pattern to user's recently visited list

return output


-def get_reverse(pattern, kwargs, content_type_id=None, obj_id=None,
start=None, end=None,dimension=None,scroll_to=None):
+def get_reverse(pattern, kwargs, content_type_id=None, obj_id=None,
start=None, end=None,dimension=None,scroll_to=None,returnurl=None):
url = reverse(pattern, kwargs=kwargs)
qs = []
if content_type_id is not None and obj_id is not None:
@@ -127,6 +129,9 @@
qs.append(DIM_KEY + "=" + str(dimension))
if scroll_to is not None:
qs.append(SCROLL_KEY + "=" + str(scroll_to))
+ if returnurl is not None:
+ returnurl = returnurl.replace('&','%') #replace so we can parse it
properly in urls, must replace('%','&') to use in tags
+ qs.append(RETURN_KEY + "=" + str(returnurl))

qs = "?" + "&".join(qs)
return url + qs
=======================================
--- /trunk/pirate-politics/pirate_core/views.py Mon Jan 31 18:26:02 2011
+++ /trunk/pirate-politics/pirate_core/views.py Wed Feb 9 00:35:14 2011
@@ -28,7 +28,6 @@

def home_page(request):
return redirect('welcome.html')
-


def redirectable(func):
=======================================
--- /trunk/pirate-politics/pirate_issues/templatetags/issuetags.py Sun Feb
6 23:09:47 2011
+++ /trunk/pirate-politics/pirate_issues/templatetags/issuetags.py Wed Feb
9 00:35:14 2011
@@ -173,9 +173,8 @@
rng = None

if not rng or len(rng) != 2:
- raise ValueException("The argument 'rng=' to the pp-issue-list
tag must be "
- "provided either in the form of an (int,
int) pair, or "
- "as a string in the form of '(<int>,
<int>)'.")
+ raise ValueError("The argument 'start=' and 'end=' to the
pp_get_issue_list tag must be "
+ "provided either in the form of an int")
else: rng = None

#TODO: future functionality to get user-specific lists
@@ -213,19 +212,14 @@
order_by = '-votes'

#need to filter on topic of content object and content_type.name
-
if topic and isinstance(topic, Topic): consensus_list =
consensus_list.filter(parent_pk=topic.pk)
+
+ # else if there is no topic, filter by content_type issue
+ else:
+ c_type = ContentType.objects.get_for_model(Issue)
+ consensus_list = consensus_list.filter(content_type=c_type)

issue_list = consensus_list.order_by(order_by)
-
- #issue_list = []
- #for i in consensus_list:
- # if i.content_type.name == u'issue':
- # if topic and isinstance(topic, Topic):
- # if i.content_object.topic == topic:
- # issue_list.append(i.content_object)
- # elif not isinstance(topic, Topic):
- # issue_list.append(i.content_object)

else: #catch all for no dimension grabs new objects
issue_list = issue_list.order_by('-submit_date')
=======================================
--- /trunk/pirate-politics/pirate_issues/templatetags/solutiontags.py Sun
Feb 6 23:09:47 2011
+++ /trunk/pirate-politics/pirate_issues/templatetags/solutiontags.py Wed
Feb 9 00:35:14 2011
@@ -120,15 +120,6 @@

solution_list = consensus_list.order_by(order_by)

- #solution_list = []
- #for i in consensus_list:
- # if topic and isinstance(topic, Topic):
- # if i.content_object.issue.topic == topic and
i.content_object.issue == issue:
- # solution_list.append(i.content_object)
- # elif not isinstance(topic, Topic) and isinstance(issue,Issue):
- # if i.content_object.issue == issue:
solution_list.append(i.content_object)
- # else: solution_list.append(i.content_object)
-
try: namespace['count'] = solution_list.count()
except: namespace['count'] = 0

=======================================
--- /trunk/pirate-politics/pirate_login/views.py Tue Jan 25 15:15:31 2011
+++ /trunk/pirate-politics/pirate_login/views.py Wed Feb 9 00:35:14 2011
@@ -4,7 +4,7 @@
from pirate_core import HttpRedirectException, namespace_get, FormMixin

def logout_view(request):
- try: goto = request.session['last_visited'][0][1]
- except: goto = '/issues.html'
+ try: goto = request.currently_visiting
+ except: goto = '/issues.html?_s=0&_e=20&_d=hot'
logout(request)
return HttpResponseRedirect(goto)

Reply all
Reply to author
Forward
0 new messages