1 from datetime import datetime
4 from django.contrib import auth
5 from django.contrib.auth.decorators import login_required
6 from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
7 from django.core.cache import cache
8 from django.http import HttpResponse, HttpResponseRedirect
9 from django.shortcuts import render_to_response
10 from django.template import RequestContext
11 from django.utils.http import urlquote_plus
12 from django.utils.translation import ugettext_lazy as _
13 from django.views.decorators.cache import never_cache
15 from django.conf import settings
16 from ajaxable.utils import AjaxableFormView
17 from catalogue.models import Book
18 from ajaxable.utils import placeholdized
19 from social.templatetags.social_tags import choose_cite
23 def main_page(request):
24 last_published = Book.objects.filter(parent=None).order_by('-created_at')[:4]
25 cite = choose_cite(RequestContext(request))
27 return render_to_response("main_page.html", locals(),
28 context_instance=RequestContext(request))
31 class LoginFormView(AjaxableFormView):
32 form_class = AuthenticationForm
33 template = "auth/login.html"
39 def __call__(self, request):
40 if request.user.is_authenticated():
41 return self.redirect_or_refresh(request, '/',
42 message=_('Already logged in as user %(user)s', ) %
43 {'user': request.user.username})
44 return super(LoginFormView, self).__call__(request)
46 def success(self, form, request):
47 auth.login(request, form.get_user())
50 class RegisterFormView(AjaxableFormView):
51 form_class = UserCreationForm
52 template = "auth/register.html"
55 submit = _('Register')
57 form_prefix = 'register'
60 def __call__(self, request):
61 if request.user.is_authenticated():
62 return self.redirect_or_refresh(request, '/',
63 message=_('Already logged in as user %(user)s', ) %
64 {'user': request.user.username})
65 return super(RegisterFormView, self).__call__(request)
67 def success(self, form, request):
69 user = auth.authenticate(
70 username=form.cleaned_data['username'],
71 password=form.cleaned_data['password1']
73 auth.login(request, user)
76 class LoginRegisterFormView(LoginFormView):
77 template = 'auth/login_register.html'
78 title = _('You have to be logged in to continue')
80 def extra_context(self, request, obj):
82 "register_form": placeholdized(UserCreationForm(prefix='register')),
83 "register_submit": _('Register'),
88 def logout_then_redirect(request):
90 return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
94 """ Provides server UTC time for jquery.countdown,
95 in a format suitable for Date.parse()
97 return HttpResponse(datetime.utcnow().strftime('%Y/%m/%d %H:%M:%S UTC'))
100 def publish_plan(request):
101 cache_key = "publish_plan"
102 plan = cache.get(cache_key)
107 feed = feedparser.parse(settings.PUBLISH_PLAN_FEED)
111 for i in range(len(feed['entries'])):
113 'title': feed['entries'][i].title,
114 'link': feed['entries'][i].link,
116 cache.set(cache_key, plan, 1800)
118 return render_to_response("publish_plan.html", {'plan': plan},
119 context_instance=RequestContext(request))
123 def user_settings(request):
124 return render_to_response("user.html",
125 context_instance=RequestContext(request))