def autologin(request): """ Django View. Gets called when user wants to autologin @type request: C{django.http.HttpRequest} @param request: Django Request """ from booki.utils.json_wrapper import simplejson from booki.utils.misc import isUserLimitReached from booki.editor.models import BookiGroup from django.core.exceptions import ObjectDoesNotExist from django.contrib import auth limitReached = isUserLimitReached() ret = {"result": 0} encusername = request.GET.get("username", "").strip() strusername = encusername.decode('base64','strict') encpassword = request.GET.get("password", "").strip() strpassword = encpassword.decode('base64','strict') user = auth.authenticate(username=strusername, password=strpassword) if user: auth.login(request, user) ret["result"] = 1 else: try: usr = auth.models.User.objects.get(username=strusername) # User does exist. Must be wrong password then ret["result"] = 3 except auth.models.User.DoesNotExist: # User does not exist ret["result"] = 2 #from django.core.urlresolvers import reverse redirect = request.GET.get('redirect', '/accounts/'+strusername) if request.GET.get('next', None): redirect = request.GET.get('next') resp = HttpResponseRedirect(redirect) return resp
def view_frontpage(request): """ Django View. This is main Booktype Front Page. @type request: C{django.http.HttpRequest} @param request: Client Request object """ from booki.utils import config from booki.utils.misc import isUserLimitReached showHistory = config.getConfiguration('BOOKTYPE_FRONTPAGE_HISTORY', True) if showHistory: activityHistory = models.BookHistory.objects.filter(kind__in=[1, 10], book__hidden=False).order_by('-modified')[:20] else: activityHistory = [] return render_to_response('portal/frontpage.html', {"request": request, "activity_history": activityHistory, "show_history": showHistory, "limit_reached": isUserLimitReached(), "title": "Booktype"})
def signin(request): """ Django View. Gets called when user wants to signin or create new account. @type request: C{django.http.HttpRequest} @param request: Django Request """ from booki.utils.json_wrapper import simplejson from booki.utils.misc import isUserLimitReached from booki.editor.models import BookiGroup from django.core.exceptions import ObjectDoesNotExist from django.contrib import auth limitReached = isUserLimitReached() if request.POST.get("ajax", "") == "1": ret = {"result": 0} if request.POST.get("method", "") == "register" and config.getConfiguration( 'FREE_REGISTRATION') and not limitReached: def _checkIfEmpty(key): return request.POST.get(key, "").strip() == "" def _doChecksForEmpty(): if _checkIfEmpty("username"): return 2 if _checkIfEmpty("email"): return 3 if _checkIfEmpty("password") or _checkIfEmpty("password2"): return 4 if _checkIfEmpty("fullname"): return 5 return 0 ret["result"] = _doChecksForEmpty() if ret["result"] == 0: # if there was no errors import re def _doCheckValid(): # check if it is valid username # - from 2 to 20 characters long # - word, number, ., _, - mtch = re.match('^[\w\d\_\.\-]{2,20}$', request.POST.get("username", "").strip()) if not mtch: return 6 # check if it is valid email if not bool(email_re.match(request.POST["email"].strip())): return 7 if request.POST.get("password", "") != request.POST.get( "password2", "").strip(): return 8 if len(request.POST.get("password", "").strip()) < 6: return 9 if len(request.POST.get("fullname", "").strip()) > 30: return 11 # check if this user exists try: u = auth.models.User.objects.get( username=request.POST.get("username", "").strip()) return 10 except auth.models.User.DoesNotExist: pass return 0 ret["result"] = _doCheckValid() if ret["result"] == 0: ret["result"] = 1 user = None try: user = auth.models.User.objects.create_user( username=request.POST["username"].strip(), email=request.POST["email"].strip(), password=request.POST["password"].strip()) except IntegrityError: ret["result"] = 10 except: ret["result"] = 10 user = None # this is not a good place to fire signal, but i need password for now # should create function createUser for future use if user: user.first_name = request.POST["fullname"].strip() import booki.account.signals booki.account.signals.account_created.send( sender=user, password=request.POST["password"]) try: user.save() # groups for groupName in simplejson.loads( request.POST.get("groups")): if groupName.strip() != '': sid = transaction.savepoint() try: group = BookiGroup.objects.get( url_name=groupName) group.members.add(user) except: transaction.savepoint_rollback(sid) else: transaction.savepoint_commit(sid) user2 = auth.authenticate( username=request.POST["username"].strip(), password=request.POST["password"].strip()) auth.login(request, user2) except: transaction.rollback() ret["result"] = 666 else: transaction.commit() else: transaction.rollback() if request.POST.get("method", "") == "signin": user = auth.authenticate(username=request.POST["username"].strip(), password=request.POST["password"].strip()) if user: auth.login(request, user) ret["result"] = 1 from django.core.urlresolvers import reverse ret["redirect"] = reverse('view_profile', args=[user.username]) else: try: usr = auth.models.User.objects.get( username=request.POST["username"]) # User does exist. Must be wrong password then ret["result"] = 3 except auth.models.User.DoesNotExist: # User does not exist ret["result"] = 2 try: resp = HttpResponse(simplejson.dumps(ret), mimetype="text/json") except: transaction.rollback() raise else: transaction.commit() return resp from django.core.urlresolvers import reverse redirect = request.GET.get('redirect', '') if (redirect == reverse('frontpage')): redirect = '' if request.GET.get('next', None): redirect = request.GET.get('next') joinGroups = [] for groupName in request.GET.getlist("group"): try: joinGroups.append(BookiGroup.objects.get(url_name=groupName)) except BookiGroup.DoesNotExist: pass try: resp = render_to_response( 'account/signin.html', { 'request': request, 'redirect': redirect, 'joingroups': joinGroups, 'limit_reached': limitReached }) except: transaction.rollback() raise else: transaction.commit() return resp
def signin(request): """ Django View. Gets called when user wants to signin or create new account. @type request: C{django.http.HttpRequest} @param request: Django Request """ from booki.utils.json_wrapper import simplejson from booki.utils.misc import isUserLimitReached from booki.editor.models import BookiGroup from django.core.exceptions import ObjectDoesNotExist from django.contrib import auth limitReached = isUserLimitReached() if request.POST.get("ajax", "") == "1": ret = {"result": 0} if request.POST.get("method", "") == "register" and config.getConfiguration('FREE_REGISTRATION') and not limitReached: def _checkIfEmpty(key): return request.POST.get(key, "").strip() == "" def _doChecksForEmpty(): if _checkIfEmpty("username"): return 2 if _checkIfEmpty("email"): return 3 if _checkIfEmpty("password") or _checkIfEmpty("password2"): return 4 if _checkIfEmpty("fullname"): return 5 return 0 ret["result"] = _doChecksForEmpty() if ret["result"] == 0: # if there was no errors import re def _doCheckValid(): # check if it is valid username # - from 2 to 20 characters long # - word, number, ., _, - mtch = re.match('^[\w\d\_\.\-]{2,20}$', request.POST.get("username", "").strip()) if not mtch: return 6 # check if it is valid email if not bool(email_re.match(request.POST["email"].strip())): return 7 if request.POST.get("password", "") != request.POST.get("password2", "").strip(): return 8 if len(request.POST.get("password", "").strip()) < 6: return 9 if len(request.POST.get("fullname", "").strip()) > 30: return 11 # check if this user exists try: u = auth.models.User.objects.get(username=request.POST.get("username", "").strip()) return 10 except auth.models.User.DoesNotExist: pass return 0 ret["result"] = _doCheckValid() if ret["result"] == 0: ret["result"] = 1 user = None try: user = auth.models.User.objects.create_user(username=request.POST["username"].strip(), email=request.POST["email"].strip(), password=request.POST["password"].strip()) except IntegrityError: ret["result"] = 10 except: ret["result"] = 10 user = None # this is not a good place to fire signal, but i need password for now # should create function createUser for future use if user: user.first_name = request.POST["fullname"].strip() import booki.account.signals booki.account.signals.account_created.send(sender = user, password = request.POST["password"]) try: user.save() # groups for groupName in simplejson.loads(request.POST.get("groups")): if groupName.strip() != '': sid = transaction.savepoint() try: group = BookiGroup.objects.get(url_name=groupName) group.members.add(user) except: transaction.savepoint_rollback(sid) else: transaction.savepoint_commit(sid) user2 = auth.authenticate(username=request.POST["username"].strip(), password=request.POST["password"].strip()) auth.login(request, user2) except: transaction.rollback() ret["result"] = 666 else: transaction.commit() else: transaction.rollback() if request.POST.get("method", "") == "signin": user = auth.authenticate(username=request.POST["username"].strip(), password=request.POST["password"].strip()) if user: auth.login(request, user) ret["result"] = 1 from django.core.urlresolvers import reverse ret["redirect"] = reverse('view_profile', args=[user.username]) else: try: usr = auth.models.User.objects.get(username=request.POST["username"]) # User does exist. Must be wrong password then ret["result"] = 3 except auth.models.User.DoesNotExist: # User does not exist ret["result"] = 2 try: resp = HttpResponse(simplejson.dumps(ret), mimetype="text/json") except: transaction.rollback() raise else: transaction.commit() return resp from django.core.urlresolvers import reverse redirect = request.GET.get('redirect', '') if(redirect == reverse('frontpage')): redirect = '' if request.GET.get('next', None): redirect = request.GET.get('next') joinGroups = [] for groupName in request.GET.getlist("group"): try: joinGroups.append(BookiGroup.objects.get(url_name=groupName)) except BookiGroup.DoesNotExist: pass try: resp = render_to_response('account/signin.html', {'request': request, 'redirect': redirect, 'joingroups': joinGroups, 'limit_reached': limitReached}) except: transaction.rollback() raise else: transaction.commit() return resp