def fb_login(request, onsuccess="/availabook/temp", onfail="/availabook/"): print "fb_login" user_id = str(request.POST.get("email")) pwd = str(request.POST.get("psw")) pwd_a = pwd firstname = request.POST.get("fn") lastname = request.POST.get("ln") age = request.POST.get("age") picture = request.POST.get("picture") print user_id, pwd, firstname, lastname, age, picture city = 'ny' zipcode = '10027' signup_handler = Signup(user_id, pwd, pwd_a, firstname, lastname, age, city, zipcode) signup_handler.add_picture(picture) user_db = Users(user_id, pwd) if user_db.verify_email() == False: print "account not exist" if not user_exists(user_id): user = User(username=user_id, email=user_id) user.set_password(pwd) user.save() authenticate(username=user_id, password=pwd) user.backend = 'django.contrib.auth.backends.ModelBackend' auth_login(request, user) try: print "~~~~~~~~~" signup_handler.push_to_dynamodb() print "push success" except Exception as e: print (e) print str(request.user.username) + " is signed up and logged in: " + str(request.user.is_authenticated()) return redirect(onsuccess) else: user=authenticate(username=user_id, password=pwd) user.backend = 'django.contrib.auth.backends.ModelBackend' auth_login(request, user) print str(request.user.username) + " is signed up and logged in: " + str(request.user.is_authenticated()) return redirect(onsuccess) else: print "dynamo has the user info", user_id, pwd user = authenticate(username=user_id, password=pwd) print user try: if user is not None: auth_login(request, user) print "redirecting" return redirect(onsuccess) else: print "user is none" return redirect(onfail) except Exception as e: print e return redirect(onfail)
def signup(request, onsuccess="/availabook/temp", onfail="/availabook/"): user_id = request.POST.get("email") pwd = request.POST.get("psw") pwd_a = request.POST.get("psw_a") firstname = request.POST.get("fn") lastname = request.POST.get("ln") age = request.POST.get("age") city = request.POST.get("city") zipcode = request.POST.get("zipcode") print user_id, pwd, pwd_a, firstname, lastname, age, city, zipcode signup_handler = Signup(user_id, pwd, pwd_a, firstname, lastname, age, city, zipcode) signup_handler.add_picture("https://s3.amazonaws.com/image-availabook/default") if pwd == pwd_a: if not user_exists(user_id): user = User(username=user_id, email=user_id) user.set_password(pwd) user.save() authenticate(username=user_id, password=pwd) user.backend = 'django.contrib.auth.backends.ModelBackend' auth_login(request, user) print ("Successfully saving user information to Django sqlite.") try: signup_handler.push_to_dynamodb() print ("Successfully pushing user information to AWS Dynamodb.") print str(request.user.username) + " is signed up and logged in: " + str(request.user.is_authenticated()) return redirect(onsuccess) except Exception as e: print (e) print ("Pushing user information to AWS Dynamodb failed! Please sign up again!") return HttpResponse("Error") else: user_db = Users(user_id, pwd) if user_db.verify_email() == False: print "Current user is already existed in Django sqlite, but not in AWS Dynamodb! Generally, sign up will fail. For debugging, maybe you should first delete this user from your django sqlite, clean the cache in browser and sign up again!" try: signup_handler.push_to_dynamodb() print ("Successfully pushing user information to AWS Dynamodb.") if request.user.is_authenticated(): print str(request.user.username) + " is signed up and logged in: " + str(request.user.is_authenticated()) return redirect(onsuccess) else: print ("Sign up failed. Please debug following the instructions above!") return HttpResponse("Error") except Exception as e: print (e) print ("Pushing user information to AWS Dynamodb failed! Please debug and sign up again!") return HttpResponse("Error") else: print ("Current user is already existed in both Django sqlite and AWS Dynamodb! Please turn to log in first!") print ("If you still can't sign up and log in, for debugging, please delete this user from your django sqlite, clean the cache in browser and sign up again!") return HttpResponse("Error") else: print "Two input passwords inconsistent! Please sign up again!" return HttpResponse("Error")
def upload(request): print "uploading" profile_link = "" if request.method == 'POST': print "posting" print request.POST #print type(request.FILES['pic'].get('content_type')) #print type(request.FILES['pic']['file']) form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): print 'valid form' k = Key(bucket) k.key = request.user.username k.set_contents_from_file(request.FILES['pic']) print "upload!" profile_link = "https://s3.amazonaws.com/image-availabook/" + request.user.username profile_link = profile_link.replace('@','%40') print "link", profile_link uploaded = Users.update_image_by_id(request.user.username, profile_link) print uploaded #print k.get_contents_to_filename else: print 'invalid form' print form.errors return redirect("/availabook/profile")
def home(request): print(request.user.username) event_list = get_recommend_newversion(request.user.username) email_list, user_name_list, user_picture_list = get_user_info_from_eventlist(event_list) zipped_list = zip(event_list, email_list, user_name_list, user_picture_list) if request.user.is_authenticated(): fname = Users.get_user_info(request.user.username)['first_name'] return render(request, 'homepage.html',{'zipped_list':zipped_list, 'logedin': True,'fname':fname}) else: print "not authenticate" return render(request, 'homepage.html',{'zipped_list':zipped_list, 'logedin': False})
def info(request): print "info" item = Users.get_user_info(request.user.username) if item is None: return redirect("/availabook/home") fname = item['first_name'] lname = item['last_name'] city = item['city'] zipcode = item['zipcode'] age = item['age'] print "info send json", {'fname':fname,'lname':lname,'city':city,'age':age,'zipcode':zipcode} return JsonResponse({'fname':fname,'lname':lname,'city':city,'age':age,'zipcode':zipcode})
def login(request, onsuccess="/availabook/temp", onfail="/availabook/"): csrf_token = csrf.get_token(request) user_id = request.POST.get("id") pwd = request.POST.get("psw") print user_id, pwd, csrf_token user = authenticate(username=user_id, password=pwd) if user is not None: auth_login(request, user) print ("Current input user information is already existed in Django sqlite") else: print ("Current input user information is not existed in Django sqlite or the input password incorrect with that in Django sqlite!") return HttpResponse("Error") login_user = Users(user_id, pwd) if login_user.authen_user(): login_user.authorize() print str(request.user.username) + " is logged in: " + str(request.user.is_authenticated()) return redirect(onsuccess) else: print ("Current user information not exist in AWS Dynamodb or the input password is incorrect with that in AWS Dynamodb!") #return redirect(onfail) return HttpResponse("Error")
def profile(request): if request.user.is_authenticated(): print "views profile: ", request.user.username posts_list, events_list = get_post_events_from_user(request.user.username) zipped_list = zip(posts_list, events_list) item = Users.get_user_info(request.user.username) if item is None: print "No user info" return redirect("/availabook/home") email = item['email'] link = item['picture'] fname = item['first_name'] lname = item['last_name'] city = item['city'] zipcode = item['zipcode'] age = item['age'] print link print {'fname':fname,'lname':lname,'city':city,'age':age,'zipcode':zipcode} return render(request, 'profile.html', {'email': email, 'link': link, 'logedin': True, 'fname': fname, 'lname': lname, 'city': city, 'age': age,'zipcode': zipcode, 'zipped_list': zipped_list}) else: return redirect("/availabook/home")