def try_register(user_form): new_user = user_form.save() new_user.set_password(user_form.cleaned_data['password1']) key = "" while key == "": key = get_random_string(15, string.lowercase + string.digits) user = BlocklogicUser.objects.filter(password_reset_key=key) if user: key = "" new_user.password_reset_key = key new_user.type = 'normal' new_user.is_active = False new_user.save() new_user.update_user_profile() # TODO: add the free subscription on register # add_free_subscription(new_user) activation_url = reverse('web:activate_account', args={key}) # put the stuff in template, then render it to string and send it via email mail_context = { 'url': settings.SITE_URL + activation_url, 'site_url': settings.SITE_URL, } subject = settings.EMAIL_SUBJECT_PREFIX + " " + _("Registration successful") message_html = render_to_string('email/email_verification.html', mail_context) message_text = render_to_string('email/email_verification.txt', mail_context) if settings.DEBUG: print message_text print message_html else: send_email(settings.EMAIL_FROM, [new_user.email], None, subject, message_text, message_html)
def create_key(): # 15: defined in BlocklogicUser model (password_reset_key) return get_random_string(15, string.lowercase + string.digits)
user_form = BlocklogicUserBaseForm(query_dict) if user_form.is_valid(): email = user_form.cleaned_data['email'] if User.exists(email): print "User exists, something wrong, check it out..." raise Exception new_user = user_form.save() new_user.set_password(user_form.cleaned_data['password1']) key = "" while key == "": key = get_random_string(15, string.lowercase + string.digits) user = BlocklogicUser.objects.filter(password_reset_key=key) if user: key = "" new_user.password_reset_key = key new_user.type = 'normal' new_user.is_active = False new_user.save() else: print "User form not valid" print user_form.errors BlocklogicUser.objects.get(email='*****@*****.**').delete()
def google_login_or_register(request, mobile=False): """ log the user in; if successful, redirect to the index page if not successful, redirect back to the login page """ context = {} d = JsonParse(request.POST.get('data')) if "access_token" not in d: return JsonError("no_access_token", _("No access_token provided.")) url = (GOOGLE_API['client_userinfo'] % d["access_token"]) r = requests.get(url) if mobile: if r is None: return {'status': 'error', 'google_api_error': _("Something went wrong.")} if r.status_code == 401: return {'status': 'error', 'invalid_token': _("Token is invalid")} elif r.status_code != 200: return {'status': 'error', 'google_api_error': _("Something went wrong.")} else: if r is None: return JsonError("google_api_error", _("Something went wrong.")) if r.status_code != 200: return JsonError("google_api_error", _("Something went wrong.")) r = r.json() if "error" in r: return JsonError("google_api_error", _("Something went wrong.")) if not "email" in r and r['email'] != "": if mobile: return {'status': 'error', 'google_api_error': _("Something went wrong.")} else: return JsonError("google_api_error", _("Something went wrong.")) try: bluser = BlocklogicUser.objects.get(email=r["email"]) if bluser.first_name != r["given_name"] or bluser.last_name != r["family_name"] or bluser.sex != r["gender"]: bluser.first_name = r["given_name"] bluser.last_name = r["family_name"] bluser.sex = r["gender"] bluser.save() bluser.update_user_profile() if bluser.type != GOOGLE and not mobile: return JsonError("already_registered_via_normal", _("Already registered via normal")) elif bluser.type != GOOGLE and mobile: return {'status': 'error', 'already_registered_via_normal': _("Already registered via normal")} except BlocklogicUser.DoesNotExist: # we do not have user in our db, so we add register/new one # print r try: gender = r["gender"] except KeyError: gender = g.MALE bluser = BlocklogicUser(email=r["email"], first_name=r["given_name"], last_name=r["family_name"], sex=gender, type="google") bluser.save() key = "" while key == "": key = get_random_string(15, string.lowercase + string.digits) user = BlocklogicUser.objects.filter(password_reset_key=key) if user: key = "" bluser.password_reset_key = key bluser.save() bluser.update_user_profile() # group = bluser.homegroup # add_free_subscription(bluser) user = django_authenticate(username=r["email"], password='', type=GOOGLE) if user is not None: if not mobile: django_login(request, user) set_user_value(bluser, 'google_access_token_' + settings.SITE_URL, d["access_token"]) set_language(request) if mobile: data = {'status': 'ok', 'user_id': user.id} else: data = {'status': 'ok', 'redirect_url': reverse('web:select_company'), 'user_id': user.id} if len(user.images.all()) == 0: picture_url = r['picture'] r = requests.get(picture_url, stream=True) if r.status_code == 200: original_filename = picture_url.rsplit('/', 1) user_image = UserImage(name=user.first_name + " " + user.last_name, original_filename=original_filename[1], created_by=user, updated_by=user) user_image.image.save(original_filename[1], ContentFile(r.raw.read())) user_image.save() user.images.add(user_image) user.save() if mobile: return data return JsonResponse(data) return JsonError("error", _("Something went wrong during login with google"))
def url_name_suggestions(request): # there's a 'name' in request: # take it and return a list of a few company url suggestions suggestions = [] try: # get name from sent data name = JsonParse(request.POST.get('data'))['name'] except: return JsonResponse({'suggestions':[]}) # 0. "lowercaseize" name = name.lower() # 1. "asciify" s = unidecode.unidecode(name) # 2. remove forbidden characters (everything but alphanumeric and dash) ("alphanumerify") s = re.sub(r'[^\w-]', ' ', s).strip() # first suggestion: full name - also remove duplicate -'s suggestions.append(unique_url_name(re.sub(r' +', '-', s))) # 2. remove one-character words ("unonecharacterify") # regex: one character followed by a space at the beginning OR # space followed by a character followed by a space OR # space followed by a character at the end s = re.sub(r'(^. )|(( ).(?= ))|( .$)', ' ', s) # 3. suggestions: # first word, then first and second, then ..., up to g.MISC['company_url_length'] chars max_len = g.MISC['company_url_length'] words = s.split() for w in range(1, len(words)+1): s = '' for i in range(w): s += '-' + words[i] s = s[1:] # cut off the first '-' if len(s) >= max_len: break suggestions.append(unique_url_name(s)) # only first characters of every word (a.k.a. acronym) s = '' for w in words: s += w[0] suggestions.append(unique_url_name(s)) # 4. remove possible inapropriate suggestions valid = [] for s in suggestions: if check_url_name(s): valid.append(s) # 5. remove duplicates valid = list(set(valid)) # 6. sort by length valid.sort(key=len) suggestions = valid del valid # 3a. add a random string to suggestions suggestions.append(unique_url_name(get_random_string(length=5).lower())) # pass on to the page return JsonResponse({'suggestions':suggestions})