def createComment(request, type_requested, comment_type): """ Creates a new comment for specified post or event """ template = None redirect = None if type_requested == 'rest': template = None elif type_requested == 'desktop': #Change redirect based on whether this is a post comment or event comment if comment_type == 'posts': try: post_id = helper_functions.get_request_dict( request, type_requested)["post_id"] redirect = "/desktop/posts/" + post_id + "/comments/" except KeyError: redirect = "/desktop/block/" else: try: event_id = helper_functions.get_request_dict( request, type_requested)["event_id"] redirect = "/desktop/block/events/" + event_id except KeyError: redirect = "/desktop/block/" if comment_type == 'posts': return helper_functions.login_required_view( request, type_requested, UserProfile.createPostComment, template, redirect) else: return helper_functions.login_required_view( request, type_requested, UserProfile.createEventComment, template, redirect)
def createComment(request, type_requested, comment_type): """ Creates a new comment for specified post or event """ template = None redirect = None if type_requested == 'rest': template = None elif type_requested == 'desktop': #Change redirect based on whether this is a post comment or event comment if comment_type == 'posts': try: post_id = helper_functions.get_request_dict(request, type_requested)["post_id"] redirect = "/desktop/posts/" + post_id + "/comments/" except KeyError: redirect = "/desktop/block/" else: try: event_id = helper_functions.get_request_dict(request, type_requested)["event_id"] redirect = "/desktop/block/events/" + event_id except KeyError: redirect = "/desktop/block/" if comment_type == 'posts': return helper_functions.login_required_view(request, type_requested, UserProfile.createPostComment, template, redirect) else: return helper_functions.login_required_view(request, type_requested, UserProfile.createEventComment, template, redirect)
def resetPassword(request, type_requested): """ Resets user's password to a random value, then sends this in an email to the user Display's confirmation page after """ request_dict = helper_functions.get_request_dict(request, type_requested) try: email = request_dict["email"] #Attempt to get user profile matching email user = auth.models.User.objects.get(email=email) #Reset their password to some random value new_password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10)) user.set_password(new_password) #Send them this in an email send_mail('New Password for The Block', 'Hi ' + str(user.get_profile().first_name) + ' ' + str(user.get_profile().last_name) + ',\n\nYour password has been reset. Your new password is:\n' + new_password + '\n\nLogin using this password at http://www.inmyblock.com/ and then create a new password using the \'Account\' page. If you have further issues, email [email protected]\n\n-The Block team', '*****@*****.**', [email], fail_silently=False) response = { 'success' : 1 } except KeyError: response = { 'success' : 0, 'error' : 'No email given' } except auth.models.User.DoesNotExist: response = { 'success' : 0, 'error' : 'No account with that email address could be found' } if type_requested == 'desktop': return render_to_response('forgot_password.html', response, context_instance=RequestContext(request))
def currentBlock(request, type_requested, offset=0, num_results=10): """ Gets the Block feed for the posted latitude and longitude To be used as a preview pane """ request_dict = helper_functions.get_request_dict(request, type_requested) try: #Get Block corresponding to POSTed lat/long (x, y) = helper_functions.computeXY(request_dict['latitude'], request_dict['longitude']) current_block = BlockPage.objects.get(x_coordinate=x, y_coordinate=y) response = current_block.getActivity(offset=offset) except KeyError: response = { 'success': 0, 'error': 'Latitude and longitude were not passed in' } except BlockPage.DoesNotExist: response = { 'success': 0, 'error': 'There is nothing posted for the current block' } if type_requested == 'rest': return helper_functions.dict_to_json_response(response)
def viewAccount(request): """ Returns 'Account' page, where users can view and edit their account settings which are currently just password, name, and profile pic """ if request.method != 'POST': return render_to_response('account.html', context_instance=RequestContext(request)) request_dict = helper_functions.get_request_dict(request, 'desktop') if not request.user.is_authenticated(): response = {'success': 0, 'error': 'You are not logged in.'} else: #Modify user's info if the password is correct response = None try: password = request_dict['password'] except KeyError: response = {'success': 0, 'error': 'Password must be entered.'} if not response: user = auth.authenticate(username=request.user.username, password=password) if user is not None: #Correct password, modify settings for values that exist try: user.first_name = request_dict['first_name'] except KeyError: pass try: user.last_name = request_dict['last_name'] except KeyError: pass try: if request_dict['new_password'] != '': user.set_password(request_dict['new_password']) except KeyError: pass try: setProfilePic(request, 'rest') except KeyError: pass user.save() response = {'success': 1, 'saved': 1} else: response = {'success': 0, 'error': 'Incorrect password given.'} return render_to_response('account.html', response, context_instance=RequestContext(request))
def viewAccount(request): """ Returns 'Account' page, where users can view and edit their account settings which are currently just password, name, and profile pic """ if request.method != 'POST': return render_to_response('account.html', context_instance=RequestContext(request)) request_dict = helper_functions.get_request_dict(request, 'desktop') if not request.user.is_authenticated(): response = { 'success' : 0, 'error' : 'You are not logged in.' } else: #Modify user's info if the password is correct response = None try: password = request_dict['password'] except KeyError: response = { 'success' : 0, 'error' : 'Password must be entered.' } if not response: user = auth.authenticate(username=request.user.username, password=password) if user is not None: #Correct password, modify settings for values that exist try: user.first_name = request_dict['first_name'] except KeyError: pass try: user.last_name = request_dict['last_name'] except KeyError: pass try: if request_dict['new_password'] != '': user.set_password(request_dict['new_password']) except KeyError: pass try: setProfilePic(request, 'rest') except KeyError: pass user.save() response = { 'success' : 1, 'saved' : 1} else: response = { 'success' : 0, 'error' : 'Incorrect password given.' } return render_to_response('account.html', response, context_instance=RequestContext(request))
def currentBlock(request, type_requested, offset=0, num_results=10): """ Gets the Block feed for the posted latitude and longitude To be used as a preview pane """ request_dict = helper_functions.get_request_dict(request, type_requested) try: #Get Block corresponding to POSTed lat/long (x, y) = helper_functions.computeXY(request_dict['latitude'], request_dict['longitude']) current_block = BlockPage.objects.get(x_coordinate=x, y_coordinate=y) response = current_block.getActivity(offset=offset) except KeyError: response = { 'success' : 0, 'error' : 'Latitude and longitude were not passed in' } except BlockPage.DoesNotExist: response = { 'success' : 0, 'error' : 'There is nothing posted for the current block' } if type_requested == 'rest': return helper_functions.dict_to_json_response(response)
def resetPassword(request, type_requested): """ Resets user's password to a random value, then sends this in an email to the user Display's confirmation page after """ request_dict = helper_functions.get_request_dict(request, type_requested) try: email = request_dict["email"] #Attempt to get user profile matching email user = auth.models.User.objects.get(email=email) #Reset their password to some random value new_password = ''.join( random.choice(string.ascii_uppercase + string.digits) for x in range(10)) user.set_password(new_password) #Send them this in an email send_mail( 'New Password for The Block', 'Hi ' + str(user.get_profile().first_name) + ' ' + str(user.get_profile().last_name) + ',\n\nYour password has been reset. Your new password is:\n' + new_password + '\n\nLogin using this password at http://www.inmyblock.com/ and then create a new password using the \'Account\' page. If you have further issues, email [email protected]\n\n-The Block team', '*****@*****.**', [email], fail_silently=False) response = {'success': 1} except KeyError: response = {'success': 0, 'error': 'No email given'} except auth.models.User.DoesNotExist: response = { 'success': 0, 'error': 'No account with that email address could be found' } if type_requested == 'desktop': return render_to_response('forgot_password.html', response, context_instance=RequestContext(request))
def login(request, type_requested): """Accepts a password and either an username or email, then attempts to login user""" request_dict = helper_functions.get_request_dict(request, type_requested) username = None try: email = request_dict['email'] username = auth.models.User.objects.get(email=email) except KeyError: username = None except auth.models.User.DoesNotExist: username = None try: if not username: #Only try to get username if not already determined by email username = request_dict['username'] password = request_dict['password'] user = auth.authenticate(username=username, password=password) if (user is not None and user.is_active): #Correct password, so we can log them in auth.login(request, user) response = {'success': 1, 'user_id': user.id} else: #Not authorized, send back error response = {'success': 0, 'error': 'Incorrect login information.'} except KeyError: response = {'success': 0, 'error': 'Incorrect login information.'} #Switch rendering based on type of view being returned if type_requested == 'rest': #Return JSONObject return HttpResponse(simplejson.dumps(response), mimetype='application/json') #elif type_requested == 'mobile': #Render mobile site elif type_requested == 'desktop': #Render desktop site if response['success'] == 0: return render_to_response('block_page.html', response, context_instance=RequestContext(request)) return HttpResponseRedirect('/desktop/block/')
def getThread(request, type_requested, thread_id): """Returns most recent messages from specified thread""" request_dict = helper_functions.get_request_dict(request, type_requested) #Make sure user is logged in response = None if not request.user.is_authenticated(): response = {'success': 0, 'error': 'You are not logged in.'} if not response: #Make sure thread exists if Thread.objects.filter(pk=thread_id).exists(): thread = Thread.objects.get(pk=thread_id) else: response = {'success': 0, 'error': 'Thread does not exist'} if not response: if ThreadMembership.objects.filter(user=request.user.get_profile(), thread=thread).exists(): #Set has_been_read to True tm = ThreadMembership.objects.get(user=request.user.get_profile(), thread=thread) tm.has_been_read = True tm.save() response = { 'success': 1, 'thread': thread.getThread(), 'recipients': [ recip.user.getInfo() for recip in ThreadMembership.objects.filter(thread=thread) ] } else: response = { 'success': 0, 'error': 'You do not have permission to view this thread.' } if type_requested == 'rest': return helper_functions.dict_to_json_response(response)
def login(request, type_requested): """Accepts a password and either an username or email, then attempts to login user""" request_dict = helper_functions.get_request_dict(request, type_requested) username = None try: email = request_dict['email'] username = auth.models.User.objects.get(email=email) except KeyError: username = None except auth.models.User.DoesNotExist: username = None try: if not username: #Only try to get username if not already determined by email username = request_dict['username'] password = request_dict['password'] user = auth.authenticate(username=username, password=password) if (user is not None and user.is_active): #Correct password, so we can log them in auth.login(request, user) response = { 'success' : 1, 'user_id' : user.id } else: #Not authorized, send back error response = { 'success' : 0, 'error' : 'Incorrect login information.' } except KeyError: response = { 'success' : 0, 'error' : 'Incorrect login information.' } #Switch rendering based on type of view being returned if type_requested == 'rest': #Return JSONObject return HttpResponse(simplejson.dumps(response), mimetype='application/json') #elif type_requested == 'mobile': #Render mobile site elif type_requested == 'desktop': #Render desktop site if response['success'] == 0: return render_to_response('block_page.html', response, context_instance=RequestContext(request)) return HttpResponseRedirect('/desktop/block/')
def createEvent(request, type_requested='desktop'): """ Creates a block event if user is logged in and post is not empty """ #Check if they actually tried to create an event if request.method != 'POST': #They are just viewing page for the first time, display empty form return render_to_response('create_event.html', context_instance=RequestContext(request)) request_dict = helper_functions.get_request_dict(request, type_requested) response = None if not request.user.is_authenticated(): response = { 'success' : 0, 'error' : 'You are not logged in.' } else: title = '' description = '' duration = '' location = '' try: title = request_dict['title'] description = request_dict['description'] if 'description' in request_dict else '' duration = request_dict['duration'] location = request_dict['location'] except KeyError: error = '' if 'title' not in request_dict: error += 'No title entered. ' if 'description' not in request_dict: error += 'No description entered. ' if 'duration' not in request_dict: error += 'No duration given. ' if 'location' not in request_dict: error += 'No location given. ' response = { 'success' : 0, 'error' : error, 'title' : title, 'description' : description, 'duration' : duration, 'location' : location } if not response: if title == '' or duration == '' or location == '': error = '' if title == '': error += 'No title entered. ' if description == '': error += 'No description entered. ' if duration == '': error += 'No duration given. ' if location == '': error += 'No location given. ' response = { 'success' : 0, 'error' : error, 'title' : title, 'description' : description, 'duration' : duration, 'location' : location } if not response: #Check if there was an image uploaded form = ImageUploadForm(request.POST, request.FILES) if form.is_valid(): try: new_image = helper_functions.handleUploadedImage(request.FILES['image'], request.user.get_profile()) new_event = BlockEvent(block_page=request.user.get_profile().current_block, author=request.user.get_profile(), duration=duration, event_title=title, description=description, location=location, image=new_image) except KeyError: #Create new block event new_event = BlockEvent(block_page=request.user.get_profile().current_block, author=request.user.get_profile(), duration=duration, event_title=title, description=description, location=location) else: #Create new block event new_event = BlockEvent(block_page=request.user.get_profile().current_block, author=request.user.get_profile(), duration=duration, event_title=title, description=description, location=location) #Save new event new_event.save() #Set user as attending event request.user.get_profile().events.add(new_event) request.user.get_profile().save() response = { 'success' : 1 } if type_requested == 'rest': return helper_functions.dict_to_json_response(response) elif type_requested == 'desktop': if response['success'] == 0: #There's an error; redisplay create block page with error text return render_to_response('create_event.html', response, context_instance=RequestContext(request)) else: #It was successful, redirect back to Block page return HttpResponseRedirect('/desktop/block/')
def signup(request, type_requested): """Creates a new user (and user profile) if the data is valid (i.e. email/username not already taken)""" request_dict = helper_functions.get_request_dict(request, type_requested) #Make sure data is set response = None email = '' username = '' password_1 = '' password_2 = '' first_name = '' last_name = '' #Sanity checking to make sure this wasn't a get if request_dict == None: response = { 'success' : 0 } else: try: email = request_dict['email'] username = email if 'username' not in request_dict else request_dict['username'] password_1 = request_dict['password'] password_2 = request_dict['password'] names = request_dict['name'].split() if (len(names) > 1): first_name = names[0] last_name = names[1] elif len(names) == 1: first_name = names[0] last_name = '' else: first_name = '' last_name = '' except KeyError: error = '' if 'email' not in request_dict: error += 'Email not entered. ' if 'password' not in request_dict: error += 'Password not entered. ' if 'first_name' not in request_dict: error += 'Name not entered.\n' response = { 'success' : 0, 'error' : error, 'email' : email, 'username' : username, 'password_1' : password_1, 'password_2' : password_2, 'name' : first_name + last_name} if not response: if email == u'' or username == u'' or first_name == u'': error = '' if email == '': error += 'Email not entered. ' if password_1 == '': error += 'Password not entered. ' if first_name == '': error += 'Name not entered.' response = { 'success' : 0, 'error' : error, 'email' : email, 'username' : username, 'password_1' : password_1, 'password_2' : password_2, 'first_name' : first_name, 'last_name' : last_name } #Check if email is valid if not response and not helper_functions.validate_email(email): response = { 'success' : 0, 'error' : 'Invalid email address.' } if not response: if password_1 != password_2: response = { 'success' : 0, 'error' : 'Passwords do not match.' } #Check if username or email is taken if not response: if auth.models.User.objects.filter(username=username).exists(): response = { 'success' : 0, 'error' : 'Username already taken.' } if not response: if auth.models.User.objects.filter(email=email).exists(): response = { 'success' : 0, 'error' : 'Email already in use.' } if not response: new_user = auth.models.User.objects.create_user(username=username, email=email, password=password_1) new_user.first_name = first_name new_user.last_name = last_name new_user.is_active = True new_user.save() #Send user e-mail containing key which is their username salted & hashed #key = hashlib.md5('bailey' + username).hexdigest() #send_mail('The Block registration', 'Hi ' + str(first_name) + ' ' + str(last_name) + # ',\n\nWelcome to The Block! ' + # 'Click the following link to finish signing up: http://inmyblock.com/users/' + # str(new_user.id) + '/register/' + str(key) + # '\n\n-The Block', '*****@*****.**' # , [email], fail_silently=False) #Create profile for this user user_account = UserProfile(user=new_user) user_account.save() user = auth.authenticate(username=username, password=password_1) if (user is not None and user.is_active): #Correct password, so we can log them in auth.login(request, user) response = { 'success' : 1 } if type_requested == 'rest': return helper_functions.dict_to_json_response(response) elif type_requested == 'desktop': if response['success'] == 0: #Theres an error, so redisplay signup page with error text return render_to_response('block_page.html', response, context_instance=RequestContext(request)) else: return HttpResponseRedirect('/desktop/block/')
def createEvent(request, type_requested='desktop'): """ Creates a block event if user is logged in and post is not empty """ #Check if they actually tried to create an event if request.method != 'POST': #They are just viewing page for the first time, display empty form return render_to_response('create_event.html', context_instance=RequestContext(request)) request_dict = helper_functions.get_request_dict(request, type_requested) response = None if not request.user.is_authenticated(): response = {'success': 0, 'error': 'You are not logged in.'} else: title = '' description = '' duration = '' location = '' try: title = request_dict['title'] description = request_dict[ 'description'] if 'description' in request_dict else '' duration = request_dict['duration'] location = request_dict['location'] except KeyError: error = '' if 'title' not in request_dict: error += 'No title entered. ' if 'description' not in request_dict: error += 'No description entered. ' if 'duration' not in request_dict: error += 'No duration given. ' if 'location' not in request_dict: error += 'No location given. ' response = { 'success': 0, 'error': error, 'title': title, 'description': description, 'duration': duration, 'location': location } if not response: if title == '' or duration == '' or location == '': error = '' if title == '': error += 'No title entered. ' if description == '': error += 'No description entered. ' if duration == '': error += 'No duration given. ' if location == '': error += 'No location given. ' response = { 'success': 0, 'error': error, 'title': title, 'description': description, 'duration': duration, 'location': location } if not response: #Check if there was an image uploaded form = ImageUploadForm(request.POST, request.FILES) if form.is_valid(): try: new_image = helper_functions.handleUploadedImage( request.FILES['image'], request.user.get_profile()) new_event = BlockEvent( block_page=request.user.get_profile().current_block, author=request.user.get_profile(), duration=duration, event_title=title, description=description, location=location, image=new_image) except KeyError: #Create new block event new_event = BlockEvent( block_page=request.user.get_profile().current_block, author=request.user.get_profile(), duration=duration, event_title=title, description=description, location=location) else: #Create new block event new_event = BlockEvent( block_page=request.user.get_profile().current_block, author=request.user.get_profile(), duration=duration, event_title=title, description=description, location=location) #Save new event new_event.save() #Set user as attending event request.user.get_profile().events.add(new_event) request.user.get_profile().save() response = {'success': 1} if type_requested == 'rest': return helper_functions.dict_to_json_response(response) elif type_requested == 'desktop': if response['success'] == 0: #There's an error; redisplay create block page with error text return render_to_response('create_event.html', response, context_instance=RequestContext(request)) else: #It was successful, redirect back to Block page return HttpResponseRedirect('/desktop/block/')
def signup(request, type_requested): """Creates a new user (and user profile) if the data is valid (i.e. email/username not already taken)""" request_dict = helper_functions.get_request_dict(request, type_requested) #Make sure data is set response = None email = '' username = '' password_1 = '' password_2 = '' first_name = '' last_name = '' #Sanity checking to make sure this wasn't a get if request_dict == None: response = {'success': 0} else: try: email = request_dict['email'] username = email if 'username' not in request_dict else request_dict[ 'username'] password_1 = request_dict['password'] password_2 = request_dict['password'] names = request_dict['name'].split() if (len(names) > 1): first_name = names[0] last_name = names[1] elif len(names) == 1: first_name = names[0] last_name = '' else: first_name = '' last_name = '' except KeyError: error = '' if 'email' not in request_dict: error += 'Email not entered. ' if 'password' not in request_dict: error += 'Password not entered. ' if 'first_name' not in request_dict: error += 'Name not entered.\n' response = { 'success': 0, 'error': error, 'email': email, 'username': username, 'password_1': password_1, 'password_2': password_2, 'name': first_name + last_name } if not response: if email == u'' or username == u'' or first_name == u'': error = '' if email == '': error += 'Email not entered. ' if password_1 == '': error += 'Password not entered. ' if first_name == '': error += 'Name not entered.' response = { 'success': 0, 'error': error, 'email': email, 'username': username, 'password_1': password_1, 'password_2': password_2, 'first_name': first_name, 'last_name': last_name } #Check if email is valid if not response and not helper_functions.validate_email(email): response = {'success': 0, 'error': 'Invalid email address.'} if not response: if password_1 != password_2: response = {'success': 0, 'error': 'Passwords do not match.'} #Check if username or email is taken if not response: if auth.models.User.objects.filter(username=username).exists(): response = {'success': 0, 'error': 'Username already taken.'} if not response: if auth.models.User.objects.filter(email=email).exists(): response = {'success': 0, 'error': 'Email already in use.'} if not response: new_user = auth.models.User.objects.create_user(username=username, email=email, password=password_1) new_user.first_name = first_name new_user.last_name = last_name new_user.is_active = True new_user.save() #Send user e-mail containing key which is their username salted & hashed #key = hashlib.md5('bailey' + username).hexdigest() #send_mail('The Block registration', 'Hi ' + str(first_name) + ' ' + str(last_name) + # ',\n\nWelcome to The Block! ' + # 'Click the following link to finish signing up: http://inmyblock.com/users/' + # str(new_user.id) + '/register/' + str(key) + # '\n\n-The Block', '*****@*****.**' # , [email], fail_silently=False) #Create profile for this user user_account = UserProfile(user=new_user) user_account.save() user = auth.authenticate(username=username, password=password_1) if (user is not None and user.is_active): #Correct password, so we can log them in auth.login(request, user) response = {'success': 1} if type_requested == 'rest': return helper_functions.dict_to_json_response(response) elif type_requested == 'desktop': if response['success'] == 0: #Theres an error, so redisplay signup page with error text return render_to_response('block_page.html', response, context_instance=RequestContext(request)) else: return HttpResponseRedirect('/desktop/block/')