def subscribe(request,email,lesson_id,token): from django.shortcuts import redirect from django.contrib.auth import authenticate, login from django.http import HttpResponseRedirect uname = email.replace('--att--','@') uname = uname.replace('--dot--','.') lesson = Lesson.objects.get(pk=lesson_id) try: user = UserProfile.objects.get(username=uname) except: user = UserProfile() user.username = uname user.set_password('123') user.is_active = True user.email = uname user.save() user.backend = 'main.auth.ProfileUserModelBackend' login(request, user) try: Subscriber2Lesson.objects.get(lesson=lesson, user=user) except: u2l = Subscriber2Lesson() u2l.user = user u2l.lesson = lesson u2l.save() return redirect('my_profile')
def mainpage(request): #FIX TO REDIRECT context = RequestContext(request) if request.user.is_authenticated(): return HttpResponseRedirect('/main/userpage/') else: if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid() and (request.POST['password'] == request.POST['check']): user = user_form.save() user.set_password(user.password) user.save() userprofile = UserProfile() userprofile.user = user.username userprofile.email = user.email userprofile.joined = datetime.datetime.now() userprofile.save() user = authenticate(username=user_form.cleaned_data['username'], password=user_form.cleaned_data['password'], ) login(request, user) return HttpResponseRedirect('/main/userpage/') else: print user_form.errors else: user_form = UserForm() return render_to_response('main/main.html',{'form': user_form,}, context)
def test_saving_and_retrieving_profiles(self): from django.contrib.auth.models import User u1 = User.objects.create(username = '******') u2 = User.objects.create(username = '******') profile1 = UserProfile() profile1.user = u1 profile1.email = '*****@*****.**' profile1.save() profile2 = UserProfile() profile2.user = u2 profile2.email = '*****@*****.**' profile2.save() saved_profiles = UserProfile.objects.all() self.assertEqual(saved_profiles.count(), 2) first_saved_profile = saved_profiles[0] second_saved_profile = saved_profiles[1] self.assertEqual(first_saved_profile.email, '*****@*****.**') self.assertEqual(second_saved_profile.email, '*****@*****.**')
def test_saving_and_retrieving_profiles(self): from django.contrib.auth.models import User u1 = User.objects.create(username='******') u2 = User.objects.create(username='******') profile1 = UserProfile() profile1.user = u1 profile1.email = '*****@*****.**' profile1.save() profile2 = UserProfile() profile2.user = u2 profile2.email = '*****@*****.**' profile2.save() saved_profiles = UserProfile.objects.all() self.assertEqual(saved_profiles.count(), 2) first_saved_profile = saved_profiles[0] second_saved_profile = saved_profiles[1] self.assertEqual(first_saved_profile.email, '*****@*****.**') self.assertEqual(second_saved_profile.email, '*****@*****.**')
def enter(request,email,token): from django.shortcuts import redirect from django.contrib.auth import authenticate, login from django.http import HttpResponseRedirect uname = email.replace('--att--','@') uname = uname.replace('--dot--','.') try: user = User.objects.get(username=uname) except: user = UserProfile() user.username = uname user.set_password('123') user.is_active = True user.email = uname user.save() user = User.objects.get(username=uname) user.backend = 'main.auth.ProfileUserModelBackend' login(request, user) return redirect('lesson_for_student', id=1)
def register(request): if request.method == 'GET': return JsonResponse({ 'status': 3, 'message': 'The API where new users can register themselves on the app.' }) if request.method == 'POST': try: # just to decode JSON properly data = json.loads(request.body.decode('utf8').replace("'", '"')) except: return JsonResponse({ "message": "Please check syntax of JSON data passed.", 'status': 4 }) try: # see whether all fields passed in JSON or not data['name'] data['email'] data['phone'] data['emergency_phone'] except KeyError as missing_data: return JsonResponse({ "message": "Missing the following field: {}".format(missing_data), 'status': 2 }) try: int(data['phone']) except: #phone numbers should be an integer or string only of numbers return JsonResponse({ 'status': 0, 'message': 'Please enter a valid Phone Number.' }) try: int(data['emergency_phone']) except: #phone numbers should be an integer or string only of numbers return JsonResponse({ 'status': 0, 'message': 'Please enter a valid Emergency Phone Number.' }) if len(data['phone']) != 10: return JsonResponse({ 'status': 0, 'message': 'Please enter a valid Phone Number.' }) if len(data['emergency_phone']) != 10: return JsonResponse({ 'status': 0, 'message': 'Please enter a valid Emergency Phone Number.' }) email = data['email'] if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email): return JsonResponse({ 'status': 0, 'message': 'Please enter a valid Email address.' }) try: UserProfile.objects.get(email=email) return JsonResponse({ 'status': 0, 'message': 'This Email has already been registered. Try some other email.' }) except: pass try: profile = UserProfile() name = ' '.join(str(data['name']).strip().split()) profile.name = name profile.email = str(data['email']) profile.phone = int(data['phone']) profile.emergency_phone = int(data['emergency_phone']) profile.save() #verify email send_to = profile.email body = email_body.register() email_token = utils.generate_email_token(profile) body = body % ( name, str(request.build_absolute_uri(reverse("main:nill"))) + 'email_confirm/' + email_token + '/') sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY) from_email = Email('*****@*****.**') to_email = Email(send_to) subject = "Email Confirmation for your account on Alertify app" content = Content('text/html', body) try: mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post(request_body=mail.get()) except Exception: profile.delete() return JsonResponse({ 'message': 'Error sending email. Please try again.', 'status': 0 }) message = "Registration successful! A confirmation link has been sent to %s. Kindly click on it to verify your email address." % ( send_to) return JsonResponse({'message': message, 'status': 1}) except Exception: return JsonResponse({ 'message': 'Registration failed due to unknown reasons.', 'status': 0 })
def add_event(request): ''' The view that will be called when DA will add events from the WebPortal. ''' if request.method == 'POST': try: user_id = str(request.META['HTTP_X_USER_ID']) except KeyError: return JsonResponse({ "message": "Header missing: X-USER-ID", "status": 2 }) try: user_profile = UserProfile.objects.get(uuid=user_id) if not user_profile: raise Exception except Exception: return JsonResponse( {"message": "The given UserId doesnt correspond to any user."}) ''' Add the below 2 lines on every page where DA functionalities are there. ''' if not user_profile.is_da: return JsonResponse({ "message": "You must be logged in as a DA to add events.", "status": 0 }) try: # just to decode JSON properly data = json.loads(request.body.decode('utf8').replace("'", '"')) except: return JsonResponse({ "message": "Please check syntax of JSON data passed.", 'status': 4 }) try: event_name = data['name'] description = data['description'] fund_goal = data['fund_goal'] phone = data['phone'] email = data['email'] except KeyError as missing_data: return JsonResponse({ "message": "Missing the following field: {}".format(missing_data), 'status': 2 }) try: int(data['fund_goal']) except: #phone numbers should be an integer or string only of numbers return JsonResponse({ 'status': 0, 'message': 'Fund Goal has to be a positive integer.' }) try: int(data['phone']) except: #phone numbers should be an integer or string only of numbers return JsonResponse({ 'status': 0, 'message': 'Please enter a valid phone number.' }) if len(phone) != 10: return JsonResponse({ 'status': 0, 'message': 'Please enter a valid Phone Number.' }) if not re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email): return JsonResponse({ 'status': 0, 'message': 'Please enter a valid Email address.' }) try: UserProfile.objects.get(email=email) return JsonResponse({ 'status': 0, 'message': 'This Email has already been registered. PLease try some other email.' }) except: pass try: profile = UserProfile() event_name = ' '.join(str(event_name).strip().split()) name = 'Admin for Event: ' + event_name profile.name = name profile.email = str(email) profile.phone = int(phone) profile.emergency_phone = int(phone) profile.save() username = profile.name.split(':')[1] + str(profile.id) password = ''.join(choice(chars) for i in range(8)) user = User.objects.create_user(username=username, password=password) profile.user = user profile.save() event = Event.objects.create(name=event_name, description=description, admin=profile, fund_goal=fund_goal) event.save() message = 'Event added Successfully!' return JsonResponse({'message': message, 'status': 1}) except Exception as e: print(e) return JsonResponse({ 'message': 'Event could not be added. Please try again.', 'status': 0 }) if request.method == 'GET': return JsonResponse({"message": "API for DA to add events."})