def challenge(request): """ Challenging a user to explain a term. """ params=json.loads(request.body) context_dict = {} challengee_id = params['challengee_id'] term_id = params['term_id'] challengee = get_object_or_404(get_user_model(), id=challengee_id) term = Term.objects.get(id = term_id) context_dict['term_id'] = term.id context_dict['term_name'] = term.name context_dict['challengee_id'] = challengee.id context_dict['challengee_name'] = challengee.username if request.method == "POST": if not Challenge.objects.been_challenged(request.user, challengee, term): if request.user.id != challengee.id: challenge = Challenge() challenge.challenger = request.user challenge.challengee = challengee challenge.subject = term challenge.save() context_dict['success'] = True Notification(typeof = 'challenge_notification', sender = request.user, receiver = challengee, term = term).save() email_data = { 'email': challengee.email, 'domain': request.META['HTTP_HOST'], 'site_name': SITE_NAME, 'receiver': challengee, 'sender': request.user, 'term': term, 'protocol': 'http', } subject_template_name='simplenation/challenge_notification_subject.txt' email_template_name='simplenation/challenge_notification_email.html' send_email(email_data, subject_template_name, email_template_name) else: context_dict['success'] = False context_dict['no_success_message'] = "Can't challenge yourself." else: context_dict['success'] = False context_dict['no_success_message'] = 'Has already been challenged.' else: context_dict['success'] = False context_dict['no_success_message'] = 'Invalid form request.' return HttpResponse(json.dumps(context_dict), content_type="application/json")
def send_email_confirmation(request): context_dict = {} email = request.user.email salt = hashlib.sha1(str(random.random())).hexdigest()[:5] account_deletion_key = hashlib.sha1(salt+email).hexdigest() email_data = { 'email': email, 'domain': request.META['HTTP_HOST'], 'site_name': SITE_NAME, 'account_deletion_key': account_deletion_key, 'receiver_username': request.user.username, 'site_email': EMAIL_HOST_USER, 'protocol': 'http', } subject_template_name='simplenation/registration_notification_subject.txt' email_template_name='simplenation/registration_notification_email.html' if not send_email(email_data, subject_template_name, email_template_name): context_dict['success'] = False return HttpResponse(json.dumps(context_dict), content_type='application/json') context_dict['success'] = True return HttpResponse(json.dumps(context_dict), content_type='application/json')
def register_user_while_post(request): response = {} response['success'] = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = ProfileForm(data=request.POST) if not 'username' in request.POST: response['error_message'] = "Please enter username." response['user_form'] = user_form response['profile_form'] = profile_form return response if not 'email' in request.POST: response['error_message'] = "Please enter email." response['user_form'] = user_form response['profile_form'] = profile_form return response if not 'password1' in request.POST: response['error_message'] = "Please enter password." response['user_form'] = user_form response['profile_form'] = profile_form return response email_for_validation = request.POST['email'] if not simplenation_email_validation(email_for_validation): response['error_message'] = "Please enter correct email." response['user_form'] = user_form response['profile_form'] = profile_form return response username_for_validation = request.POST['username'] if not simplenation_username_validation(username_for_validation): response['error_message'] = "Username is a bit invalid, try something awesome like "+"'"+random.choice(awesomeUsernames)+"'" response['user_form'] = user_form response['profile_form'] = profile_form return response if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() username = user_form.cleaned_data['username'] email = user_form.cleaned_data['email'] salt = hashlib.sha1(str(random.random())).hexdigest()[:5] account_deletion_key = hashlib.sha1(salt+email).hexdigest() user.save() profile = profile_form.save(commit=False) profile.user = user profile.account_deletion_key = account_deletion_key if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() email_data = { 'email': email, 'domain': request.META['HTTP_HOST'], 'site_name': SITE_NAME, 'account_deletion_key': account_deletion_key, 'receiver_username': username, 'site_email': EMAIL_HOST_USER, 'protocol': 'http', } subject_template_name='simplenation/registration_notification_subject.txt' email_template_name='simplenation/registration_notification_email.html' if not send_email(email_data, subject_template_name, email_template_name): response['error_message'] = "Couldn't send confirmation email." response['user_form'] = user_form response['profile_form'] = profile_form return response new_user = authenticate(username = request.POST['username'], password = request.POST['password1']) login(request, new_user) response['success'] = True response['user'] = new_user else: response['error_message'] = None else: user_form = UserForm() profile_form = ProfileForm() response['user_form'] = user_form response['profile_form'] = profile_form return response