Skip to content

Commit c6cf68f

Browse files
committed
fixed ajax behavior when unsigned in user vote or create wish
1 parent c83fd54 commit c6cf68f

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

nemo/static/nemo/s/common.js

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@
6969
try {
7070
resp = $.parseJSON(xhr.responseText);
7171
//rcp.l(resp);
72-
alert(OUT_OF_VOTE_MSG.replace('{want}', resp.want).
72+
switch(xhr.status){
73+
case 403:
74+
alert(OUT_OF_VOTE_MSG.replace('{want}', resp.want).
7375
replace('{has}', resp.has));
76+
break;
77+
case 401:
78+
if(confirm('please signin first.\n'+
79+
'go to signin page now?')){
80+
location = resp.signin_uri;
81+
}
82+
}
7483
} catch(err) {
7584
// TODO extractly what will happen here?
7685
}
@@ -110,11 +119,24 @@
110119
j_form.closest('.stream_li').replaceWith(data);
111120
},
112121
error: function(xhr, textstatus, err){
113-
if(403 === xhr.status){
114-
var j_replace = $(xhr.responseText);
115-
rcp.l(j_replace);
116-
j_form.replaceWith(j_replace);
117-
j_replace.show();
122+
try {
123+
resp = $.parseJSON(xhr.responseText);
124+
//rcp.l(resp);
125+
switch(xhr.status){
126+
case 403:
127+
var j_replace = $(xhr.responseText);
128+
// rcp.l(j_replace);
129+
j_form.replaceWith(j_replace);
130+
j_replace.show();
131+
break;
132+
case 401:
133+
if(confirm('please signin first.\n'+
134+
'go to signin page now?')){
135+
location = resp.signin_uri;
136+
}
137+
}
138+
} catch(err) {
139+
// TODO extractly what will happen here?
118140
}
119141
}
120142
});
@@ -173,6 +195,21 @@
173195
success: function(data){
174196
j_stream.prepend(data);
175197
j_content.val('').trigger('blur');
198+
},
199+
error: function(xhr, textstatus, err){
200+
try {
201+
resp = $.parseJSON(xhr.responseText);
202+
//rcp.l(resp);
203+
switch(xhr.status){
204+
case 401:
205+
if(confirm('please signin first.\n'+
206+
'go to signin page now?')){
207+
location = resp.signin_uri;
208+
}
209+
}
210+
} catch(err) {
211+
// TODO extractly what will happen here?
212+
}
176213
}
177214
});
178215
});
@@ -183,7 +220,7 @@
183220
j_votesleft = $('.sidebar .votesleft .count')
184221
});
185222

186-
j_stream.on('click', '.ay:not(.signin), .negative:not(.signin)',
223+
j_stream.on('click', '.ay, .negative',
187224
function(e){
188225
e.preventDefault();
189226
vote($(this));

nemo/templates/nemo/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<script type="text/javascript" src="{{ STATIC_URL }}nemo/s/jquery.cookie.js"></script>
1515
<script type="text/javascript" src="{{ STATIC_URL }}nemo/s/rcp.js"></script>
1616
<script type="text/javascript" src="{{ STATIC_URL }}nemo/s/rcp.django.js"></script>
17-
<script src="{{ STATIC_URL }}nemo/s/common.js?r=0.1.5"></script>
17+
<script src="{{ STATIC_URL }}nemo/s/common.js?r=0.1.6"></script>
1818
<script>nemo.uri_root = "{% nemo_root %}"</script>
1919
</head>
2020

nemo/templates/nemo/list.html

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
{% for e in object_list %}
44
<div class="li stream_li" sid="{{ e.id }}">
55
<div class="ayes">
6-
{% if request.user.is_authenticated %}
76
<a class="ay{% if e.count_user_ayes %} on{% endif %}"
8-
href="{% nemo_root %}ay/{{ e.id }}/">{% if e.count_user_ayes %}+{{e.count_user_ayes}}{% else %}+{% endif %}</a>
9-
{% else %}
10-
<a class="ay signin" href="/accounts/login/">+</a>
11-
{% endif %}
7+
href="{% nemo_root %}ay/{{ e.id }}/"
8+
>{% if e.count_user_ayes %}+{{e.count_user_ayes}}{% else %}+{% endif %}</a>
129
<div class="count">{{ e.count_votes }}</div>
13-
{% if request.user.is_authenticated %}
14-
<a class="negative{% if e.count_user_negatives %} on{% endif %}" href="{% nemo_root %}negative/{{ e.id }}/">{% if e.count_user_negatives %}-{{e.count_user_negatives}}{% else %}-{% endif %}</a>
15-
{% else %}
16-
<a class="negative signin" href="/accounts/login/">-</a>
17-
{% endif %}
10+
<a class="negative{% if e.count_user_negatives %} on{% endif %}"
11+
href="{% nemo_root %}negative/{{ e.id }}/"
12+
>{% if e.count_user_negatives %}-{{e.count_user_negatives}}{% else %}-{% endif %}</a>
1813
</div>
1914

2015
<div class="content{% if e.author == request.user %} editable{% endif %}">{{ e.content }}</div>

nemo/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
urlpatterns = patterns('',
1010
#url(r'^$', nv.IndexV.as_view()),
1111
url(r'^(?P<stream_type>hot|top|all|done)?/?$', nv.IndexV.as_view()),
12-
url(r'^create/$', login_required(nv.CreateV.as_view())),
12+
url(r'^create/$', nv.CreateV.as_view()),
1313
url(r'^update/(?P<pk>\d+)/$', login_required(nv.UpdateV.as_view())),
1414
url(r'^response/(?P<pk>\d+)/$',
1515
permission_required('nemo.response_wish')(nv.ResponseV.as_view())),
1616
url(r'^list/(?P<stream_type>hot|top|all|done)/(since)?(?P<since>\d+)?/?(till)?(?P<till>\d+)?/?$', nv.ListV.as_view()),
17-
url(r'^vote/(?P<pk>\d+)/$', login_required(nv.VoteV.as_view())),
17+
url(r'^vote/(?P<pk>\d+)/$', nv.VoteV.as_view()),
1818
url(r'^votes_left/$', login_required(nv.VotesLeftV.as_view())),
1919
)

nemo/views.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@ def get_success_url(self):
3030
else:
3131
self.success_url = self.get_back_uri()
3232

33-
return self.success_url
33+
return self.success_url
34+
35+
def get_unauthenticated_response(self, format='json'):
36+
go_to = settings.LOGIN_URL
37+
38+
if 'json' == format:
39+
return HttpResponse(json.dumps(dict(signin_uri=go_to)),
40+
status=401,
41+
content_type='application/json')
42+
else:
43+
return HttpResponseRedirect(go_to)
3444

3545
class IndexV(gv.TemplateView):
3646
'''Render index'''
@@ -89,8 +99,11 @@ def dispatch(self, request, *args, **kwargs):
8999
if not request.is_ajax():
90100
self.template_name = 'nemo/create.html'
91101
# else use default wish_form.html
92-
93-
return super(CreateV, self).dispatch(request, *args, **kwargs)
102+
103+
if request.user.is_authenticated():
104+
return super(CreateV, self).dispatch(request, *args, **kwargs)
105+
else:
106+
return self.get_unauthenticated_response()
94107

95108
def form_valid(self, form):
96109
'''override to set author'''
@@ -115,6 +128,9 @@ class VoteV(RedirectMixin, gv.View):
115128
'''TODO'''
116129
def post(self, request, pk, *args, **kwargs):
117130
'''TODO'''
131+
if not request.user.is_authenticated():
132+
return self.get_unauthenticated_response()
133+
118134
user = nemo.UserProfile.objects.get_by_user(request.user)
119135
wish = nemo.Wish.objects.get(pk=pk)
120136
try:
@@ -136,7 +152,7 @@ def dispatch(self, request, *args, **kwargs):
136152
if not request.is_ajax():
137153
self.template_name = 'nemo/create.html'
138154
# else use default wish_form.html
139-
155+
140156
return super(UpdateV, self).dispatch(request, *args, **kwargs)
141157

142158
def form_invalid(self, form):

0 commit comments

Comments
 (0)