~ubuntu-branches/ubuntu/utopic/python-django-threadedcomments/utopic

« back to all changes in this revision

Viewing changes to examples/example2/exampleblog/views.py

  • Committer: Package Import Robot
  • Author(s): Bernhard Reiter, Bernhard Reiter, Jakub Wilk
  • Date: 2013-08-09 19:12:14 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130809191214-p8482p4e1ctekffq
Tags: 0.9.0-1
[ Bernhard Reiter ]
* New upstream release
* debian/compat: Bump to 9.
* debian/control, debian/copyright: Update upstream links. (Closes: #713930)
* debian/control: 
  - Remove python-textile, python-markdown, python-docutils from
    Build-Depends: and Suggests:
  - Bump debhelper Build-Depends to >= 9.
  - Bump Standards-Version to 3.9.4.
  - Add Suggests: python-django-south
* debian/copyright: s/BSD-new/BSD-3-clause/
* debian/docs, debian/doc-base: Update/remove to correspond with
  upstream sources.
* debian/patches/series, debian/patches/add_markup_deps_to_install,
  debian/patches/gravatar_default_url, debian/patches/django14:
  Remove patches, as obsolete.
* debian/rules: Add a SECRET_KEY for tests during building. (Closes: #711369)
* debian/NEWS.Debian: Add.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from django.shortcuts import render_to_response
2
 
from django.template import RequestContext
3
 
from django.core.urlresolvers import reverse
4
 
from django import newforms as forms
5
 
from django.http import Http404, HttpResponse
6
 
from exampleblog.models import BlogPost
7
 
from threadedcomments.models import ThreadedComment, MARKDOWN
8
 
from voting.models import Vote
9
 
from django.contrib.auth.models import User
10
 
from django.contrib.auth import authenticate, login
11
 
 
12
 
class PostForm(forms.Form):
13
 
    comment = forms.CharField(widget=forms.Textarea)
14
 
    next = forms.CharField(widget=forms.HiddenInput)
15
 
    markup = forms.IntegerField(widget=forms.HiddenInput)
16
 
 
17
 
def comment_partial(request, comment_id):
18
 
    depth = 0
19
 
    comment = ThreadedComment.objects.get(id=comment_id)
20
 
    depth_probe = comment
21
 
    while depth_probe.parent != None:
22
 
        depth = depth + 1
23
 
        depth_probe = depth_probe.parent
24
 
    context = {
25
 
        'comment' : comment,
26
 
        'depth' : depth,
27
 
        'score' : Vote.objects.get_score(comment),
28
 
        'vote' : Vote.objects.get_for_user(comment, request.user),
29
 
    }
30
 
    return render_to_response('exampleblog/comment_partial.html', context, context_instance=RequestContext(request))
31
 
#comment_partial = login_required(comment_partial)
32
 
 
33
 
def latest_post(request):
34
 
    post = BlogPost.objects.latest('date_posted')
35
 
    comments = ThreadedComment.public.get_tree(post)
36
 
    scores = Vote.objects.get_scores_in_bulk(comments)
37
 
    uservotes = Vote.objects.get_for_user_in_bulk(comments, request.user)
38
 
    c_and_s = [(c,scores.get(c.pk,{'score':0,'num_votes':0}),uservotes.get(c.pk,{'is_upvote' : False, 'is_downvote' : False})) for c in comments]
39
 
    init_data = {
40
 
        'next' : reverse('exampleblog_latest'),
41
 
        'markup' : MARKDOWN,
42
 
    }
43
 
    context = {
44
 
        'post' : post,
45
 
        'comments_and_scores' : c_and_s,
46
 
        'form' : PostForm(initial=init_data),
47
 
    }
48
 
    return render_to_response('exampleblog/latest.html', context, context_instance=RequestContext(request))
49
 
#latest_post = login_required(latest_post)
50
 
 
51
 
class RegistrationForm(forms.Form):
52
 
    username = forms.CharField(min_length = 3, max_length = 128)
53
 
    password = forms.CharField(min_length = 4, max_length = 128);
54
 
 
55
 
def register(request):
56
 
    form = RegistrationForm(request.POST or None)
57
 
    if form.is_valid():
58
 
        try:
59
 
            check = User.objects.get(username = form.cleaned_data['username'])
60
 
            raise Http404
61
 
        except User.DoesNotExist:
62
 
            pass
63
 
        u = User(username = form.cleaned_data['username'], is_active = True)
64
 
        u.set_password(form.cleaned_data['password'])
65
 
        u.save()
66
 
        authed_user = authenticate(username = form.cleaned_data['username'], password = form.cleaned_data['password'])
67
 
        if not authed_user:
68
 
            raise Http404
69
 
        login(request, authed_user)
70
 
        return HttpResponse('Success')
71
 
    raise Http404
72
 
 
73
 
def auth_login(request):
74
 
    form = RegistrationForm(request.POST or None)
75
 
    if form.is_valid():
76
 
        authed_user = authenticate(username = form.cleaned_data['username'], password = form.cleaned_data['password'])
77
 
        if authed_user:
78
 
            login(request, authed_user)
79
 
            return HttpResponse('Success')
80
 
    raise Http404
81
 
 
82
 
def check_exists(request):
83
 
    username = request.GET.get('username', None)
84
 
    if username:
85
 
        try:
86
 
            u = User.objects.get(username=username)
87
 
        except User.DoesNotExist:
88
 
            return HttpResponse("Does Not Exist")
89
 
    raise Http404
 
 
b'\\ No newline at end of file'