def add_bookmarks_to_category(request): ''' add bookmark to category ''' if request.method == "POST": if request.is_ajax(): data = simplejson.loads(request.POST.keys()[0]) else: data = request.POST category_id = data.get('category_id','') bookmark_ids = data.get('bookmark_ids','') if category_id == '' or bookmark_ids == '': data['status'] = 'failure' data['category_id'] = 'Invalid entries' data['bookmark_ids'] = 'Invalid entries' print "no" return HttpResponse(simplejson.dumps(data),mimetype='application/json') user_id = get_userId(request) redis_obj = Redis() for bookmark_id in bookmark_ids: store_bookmark_category_mapping(redis_obj, get_userId(request), int(category_id), int(bookmark_id)) data = {} data['category_id'] = category_id data['bookmark_ids'] = bookmark_ids data['status'] = 'success' return HttpResponse(simplejson.dumps(data),mimetype='application/json')
def display_bookmarks(request): ''' Display existing bookmarks ''' username , data = get_bookmarks(request) bookmark_form = BookmarkForm(initial={'visibility':'public'}) category_form = CategoryForm() tag_form = TagForm() redis_obj = Redis() if request.is_ajax(): data = simplejson.dumps(data) return HttpResponse(data, mimetype='application/json') user_info = get_user_info(redis_obj, get_unique_id(redis_obj, username), get_userId(request)) user_info['following'] = get_following_count(redis_obj, get_unique_id(redis_obj, username)) user_info['followers'] = get_followers_count(redis_obj, get_unique_id(redis_obj, username)) return render_to_response(HOME_TEMPLATE_PATH, { 'username' : username, 'bookmark_form':bookmark_form, 'category_form':category_form, 'tag_form':tag_form, 'user_info':user_info, }, context_instance=RequestContext(request))
def logout(request): ''' Logout functionality ''' auth_token = request.COOKIES.get("auth", None) email = request.COOKIES.get("email", None) try: if auth_token != None and email != None: redis_obj = Redis() old_auth_token = redis_obj.get_value("email:%s:auth.token" %(email)) new_auth_token = get_auth_token() key = "email:%s:auth.token" % (email) redis_obj.set_value(key, new_auth_token) key = "auth.token:%s:email" % (new_auth_token) redis_obj.set_value(key, email) redis_obj.remove_key("auth.token:%s:email" % (old_auth_token)) user_id = get_userId(request) key = "auth.token:%s:userId" %(new_auth_token) redis_obj.set_value(key, user_id) redis_obj.remove_key("auth.token:%s:userId" %(old_auth_token)) key = "userId:%d:auth.token" %(user_id) redis_obj.set_value(key, new_auth_token) except: pass return HttpResponseRedirect('/')
def store_bookmark(request, bookmark_form,edit_bookmark_id=''): ''' A controller which calls the individual store methods ''' redis_obj = Redis() #PUT and POST requests are handled in the same view if edit_bookmark_id != '': bookmark_id = int(edit_bookmark_id) else: bookmark_id = get_next_bookmarkId(redis_obj) store_created_date(redis_obj, bookmark_id, str(datetime.datetime.now())) store_bookmark_uid_mapping(redis_obj, bookmark_id, get_userId(request)) store_userId(redis_obj, bookmark_id, get_userId(request)) store_url(redis_obj, bookmark_id, bookmark_form['url']) store_name(redis_obj, bookmark_id, bookmark_form['name']) store_description(redis_obj, bookmark_id, bookmark_form['description']) store_visibility(redis_obj, bookmark_id, bookmark_form['visibility']) return get_json_bookmark(redis_obj,bookmark_id)
def get_friends_count(request): ''' Get the followers and following count ''' redis_obj = Redis() user_id = get_userId(request) friends_count = {} friends_count['following_count'] = get_following_count(redis_obj,user_id) friends_count['followers_count'] = get_followers_count(redis_obj,user_id) return HttpResponse(simplejson.dumps(friends_count),mimetype='application/json')
def retrieve_tags(request): ''' Retrieve all the tag names ''' redis_obj = Redis() tag_info = get_tag_names(redis_obj) username = get_username(redis_obj,get_userId(request)) return render_to_response(TAG_NAMES_LIST_TEMPLATE_PATH, { 'tag_info': tag_info, 'username': username }, context_instance=RequestContext(request))
def delete_bookmark(request): ''' Delete a bookmark ''' bookmark_id = request.POST.get("bookmark_id", "") user_id = get_userId(request) if bookmark_id != "": clear_bookmark(user_id, int(bookmark_id)) data = {} data ['status'] = 'success' return HttpResponse(simplejson.dumps(data),mimetype='application/json')
def users(request): ''' Displays list of users ''' current_user_id = get_userId(request) redis_obj = Redis() users_list = get_users(redis_obj, current_user_id) username = get_username(redis_obj, current_user_id) return render_to_response(USERS_LIST_TEMPLATE_PATH, { 'users_list':users_list, 'username':username, }, context_instance=RequestContext(request))
def clear_category(request): ''' clear the category ''' if request.method == "POST": user_id = get_userId(request) category_id = request.POST.get("category_id", "") redis_obj = Redis() delete_category_name(redis_obj, category_id) delete_categoryId_uid_mapping(redis_obj, user_id, category_id) delete_category_name_uid_mapping(redis_obj, user_id, category_id) delete_category_name_userId_uid_mapping(redis_obj, user_id, category_id) return render_to_response(HOME_TEMPLATE_PATH, context_instance=RequestContext(request))
def change_password(request): """ Module for changing the password of the user """ redis_obj = Redis() user_id = get_userId(request) username = get_username(redis_obj, user_id) if request.method == "POST": change_password_form = ChangePasswordForm(data=request.POST) if change_password_form.is_valid(): change_password_form_cleaned = change_password_form.cleaned_data old_password = encrypt_password(change_password_form_cleaned["old_password"]) new_password = encrypt_password(change_password_form_cleaned["new_password"]) if get_password(redis_obj, user_id) == old_password: store_password(redis_obj, user_id, new_password) return HttpResponseRedirect("/home") return render_to_response( CHANGE_PASSWORD_TEMPLATE_PATH, { "change_password_form": change_password_form, "change_password_error": "Password you gave is incorrect", "username": username, }, context_instance=RequestContext(request), ) return render_to_response( CHANGE_PASSWORD_TEMPLATE_PATH, { "change_password_form": change_password_form, "change_password_error": "Invalid password entries", "username": username, }, context_instance=RequestContext(request), ) change_password_form = ChangePasswordForm() return render_to_response( CHANGE_PASSWORD_TEMPLATE_PATH, {"change_password_form": change_password_form, "username": username}, context_instance=RequestContext(request), )
def toggle_relationship(request): ''' Follow/unfollow a user ''' current_user_id = get_userId(request) redis_obj = Redis() username = get_username(redis_obj, current_user_id) if request.method == "POST": if request.is_ajax(): data = simplejson.loads(request.POST.keys()[0]) else: data = request.POST others_id = data.get("others_id", "") relationship_request = data.get("relationship_request", "") if others_id != "" and relationship_request != "": if relationship_request == "follow": follow_user(redis_obj, current_user_id, int(others_id)) toggle_status = "unfollow" else: unfollow_user(redis_obj, current_user_id, int(others_id)) toggle_status = "follow" data = {} try: data['status'] = 'success' data['toggle_status'] = toggle_status data['followers'] = get_followers_count(redis_obj,int(others_id)) data['following'] = get_following_count(redis_obj,int(others_id)) except: pass return HttpResponse(simplejson.dumps(data),mimetype='application/json') users_list = get_users(redis_obj, current_user_id) return render_to_response(USERS_LIST_TEMPLATE_PATH, { 'users_list':users_list, 'username':username, }, context_instance=RequestContext(request))
def get_recommendations(request): ''' Get public bookmarks for the user ''' redis_obj = Redis() user_id = get_userId(request) username = get_username(redis_obj,user_id) key = "userId:%d:following" %(user_id) following_ids = redis_obj.members_in_set(key) recommendations = [] for following_id in following_ids: recommendations.extend(get_public_bookmarks(redis_obj,int(following_id),limit=5)) if len(recommendations) > 50: break return render_to_response(RECOMMENDATIONS_LIST_TEMPLATE_PATH, { 'recommendations':recommendations, 'username':username }, context_instance=RequestContext(request))
def profile(request, profile_name=''): ''' profile for a user ''' current_user_id = get_userId(request) redis_obj = Redis() username = get_username(redis_obj, current_user_id) if profile_name != '': user_id = get_unique_id(redis_obj,profile_name) if user_id == '': raise Http404() user_info = get_user_info(redis_obj, get_unique_id(redis_obj, profile_name), user_id) followers_count = get_followers_count(redis_obj, get_unique_id(redis_obj, profile_name)) following_count = get_following_count(redis_obj, get_unique_id(redis_obj, profile_name)) public_bookmarks = get_public_bookmarks(redis_obj, get_unique_id(redis_obj, profile_name)) if user_id == current_user_id: my_profile = True else: my_profile = False follow = is_following(redis_obj, current_user_id, user_id) return render_to_response(USER_PROFILE_TEMPLATE_PATH, { 'user_info':user_info, 'followers_count':followers_count, 'following_count':following_count, 'public_bookmarks':public_bookmarks, 'my_profile':my_profile, 'follow': follow, 'username':username, }, context_instance=RequestContext(request)) return Http404()
def edit_profile(request): ''' Edit a user's profile ''' redis_obj = Redis() user_id = get_userId(request) username = get_username(redis_obj, user_id) if request.method == "POST": edit_profile_form = EditProfileForm(data=request.POST) if edit_profile_form.is_valid(): edit_profile_form_cleaned = edit_profile_form.cleaned_data update_profile(redis_obj, edit_profile_form_cleaned, user_id, username) return HttpResponseRedirect('/home') return render_to_response(EDIT_PROFILE_TEMPLATE_PATH, { 'edit_profile_form':edit_profile_form, 'username':username, }, context_instance=RequestContext(request)) username = get_username(redis_obj, user_id) first_name = get_first_name(redis_obj, user_id) last_name = get_last_name(redis_obj, user_id) summary = get_summary(redis_obj, user_id) edit_profile_form = EditProfileForm(initial={ 'username':username, 'first_name':first_name, 'last_name':last_name, 'summary':summary }) return render_to_response(EDIT_PROFILE_TEMPLATE_PATH, { 'edit_profile_form':edit_profile_form, 'username':username, }, context_instance=RequestContext(request))
def get_bookmarks(request): redis_obj = Redis() user_id = get_userId(request) username = get_username(redis_obj, user_id) bookmarks = get_bookmark_uid_mapping_range(redis_obj, user_id, 0, 50) data = [{} for i in xrange(len(bookmarks))] for i, bookmark_id in enumerate(bookmarks): data_dic ={} bookmark_id = int(bookmark_id) data_dic['bookmark_id'] = bookmark_id data_dic['name'] = get_name(redis_obj, bookmark_id) data_dic['url'] = get_url(redis_obj, bookmark_id) data_dic['visibility'] = get_visibility(redis_obj, bookmark_id) data_dic['creation_date'] = get_created_date(redis_obj, bookmark_id).split()[0] data_dic['description'] = get_description(redis_obj, bookmark_id) data_dic['category_id'] = get_categoryId_from_bookmark(redis_obj,user_id,bookmark_id) data[i] = data_dic return username, data
def create_category(request): ''' create a new category ''' if request.method == "POST": if request.is_ajax(): data = simplejson.loads(request.POST.keys()[0]) else: data = request.POST category_form = CategoryForm(data=data) if category_form.is_valid(): category_form_cleaned = category_form.cleaned_data user_id = get_userId(request) category_json = store_category_user(user_id, category_form_cleaned) return HttpResponse(category_json, mimetype='application/json') category_form.errors['status'] = 'failure' return HttpResponse(simplejson.dumps(category_form.errors),mimetype='application/json') raise Http404()
def get_categories(request): ''' Get the categories of the user ''' redis_obj = Redis() data = simplejson.dumps(get_category_for_user(redis_obj, get_userId(request))) return HttpResponse(data, mimetype='application/json')