def register(request): if request.user.is_authenticated: return redirect("login") next = request.GET.get('next', '') if request.method == 'POST': errors, _, form = process_register_form(request) if len(errors) == 0: if "noredirect" in request.POST: return HttpResponse("ok") elif "new?assignment=" in request.POST.get("next", ""): next = request.POST.get("next", "") return HttpResponseRedirect(next) else: next = request.POST.get("next", "/") if "?" in next: next += "&welcome=to-sefaria" else: next += "?welcome=to-sefaria" return HttpResponseRedirect(next) else: if request.GET.get('educator', ''): form = SefariaNewUserForm(initial={'subscribe_educator': True}) else: form = SefariaNewUserForm() return render(request, "registration/register.html", { 'form': form, 'next': next })
def process_register_form(request, auth_method='session'): form = SefariaNewUserForm( request.POST) if auth_method == 'session' else SefariaNewUserFormAPI( request.POST) token_dict = None if form.is_valid(): with transaction.atomic(): new_user = form.save() user = authenticate(email=form.cleaned_data['email'], password=form.cleaned_data['password1']) p = UserProfile(id=user.id) p.assign_slug() p.join_invited_groups() if PARTNER_GROUP_EMAIL_PATTERN_LOOKUP_FILE: p.add_partner_group_by_email() if hasattr(request, "interfaceLang"): p.settings["interface_language"] = request.interfaceLang p.save() if auth_method == 'session': auth_login(request, user) elif auth_method == 'jwt': token_dict = TokenObtainPairSerializer().validate({ "username": form.cleaned_data['email'], "password": form.cleaned_data['password1'] }) return { k: v[0] if len(v) > 0 else str(v) for k, v in list(form.errors.items()) }, token_dict, form
def process_register_form(request, auth_method='session'): from sefaria.utils.util import epoch_time from sefaria.helper.file import get_resized_file import hashlib import urllib.parse, urllib.request from google.cloud.exceptions import GoogleCloudError from PIL import Image form = SefariaNewUserForm(request.POST) if auth_method == 'session' else SefariaNewUserFormAPI(request.POST) token_dict = None if form.is_valid(): with transaction.atomic(): new_user = form.save() user = authenticate(email=form.cleaned_data['email'], password=form.cleaned_data['password1']) p = UserProfile(id=user.id) p.assign_slug() p.join_invited_collections() if hasattr(request, "interfaceLang"): p.settings["interface_language"] = request.interfaceLang # auto-add profile pic from gravatar if exists email_hash = hashlib.md5(p.email.lower().encode('utf-8')).hexdigest() gravatar_url = "https://www.gravatar.com/avatar/" + email_hash + "?d=404&s=250" try: with urllib.request.urlopen(gravatar_url) as r: bucket_name = GoogleStorageManager.PROFILES_BUCKET with Image.open(r) as image: now = epoch_time() big_pic_url = GoogleStorageManager.upload_file(get_resized_file(image, (250, 250)), "{}-{}.png".format(p.slug, now), bucket_name, None) small_pic_url = GoogleStorageManager.upload_file(get_resized_file(image, (80, 80)), "{}-{}-small.png".format(p.slug, now), bucket_name, None) p.profile_pic_url = big_pic_url p.profile_pic_url_small = small_pic_url except urllib.error.HTTPError as e: logger.info("The Gravatar server couldn't fulfill the request. Error Code {}".format(e.code)) except urllib.error.URLError as e: logger.info("HTTP Error from Gravatar Server. Reason: {}".format(e.reason)) except GoogleCloudError as e: logger.warning("Error communicating with Google Storage Manager. {}".format(e)) p.save() if auth_method == 'session': auth_login(request, user) elif auth_method == 'jwt': token_dict = TokenObtainPairSerializer().validate({"username": form.cleaned_data['email'], "password": form.cleaned_data['password1']}) return { k: v[0] if len(v) > 0 else str(v) for k, v in list(form.errors.items()) }, token_dict, form