def remove_inactives_photos(request,*args,**kwargs):
	"""Sanitize all inactives' photos that garnered 0 comments.

	1) We get the list of inactive ids (divided into batches for performance)
	2) For each batch, find all photo ids
	3) For each photo id, mark photos that do not have a corresponding photocomment
	4) Retrieve the file name of all such photos and store them in a redis list
	"""
	if request.user.username == 'mhb11':
		if request.method == "POST":
			decision = request.POST.get("dec",None)
			if decision == 'No':
				delete_inactives_copy()
				return redirect("home")
			elif decision == 'Yes':
				inactives, last_batch = get_inactives(get_10K=True, key="copy_of_inactive_users")
				id_list = map(itemgetter(1), inactives) #list of user ids
				uninteresting_photo_ids = Photo.objects.filter(owner_id__in=id_list, comment_count=0,vote_score=0,latest_comment__isnull=True).\
				values_list('id',flat=True)
				uninteresting_photo_ids = confirm_uninteresting(uninteresting_photo_ids)
				filenames_and_ids = Photo.objects.filter(id__in=uninteresting_photo_ids).values_list('image_file','id')
				save_deprecated_photo_ids_and_filenames(filenames_and_ids)
				if last_batch:
					delete_inactives_copy(delete_orig=True)
				return render(request,"sanitize_photos.html",{'last_batch':last_batch, \
					'inactives_remaining':get_inactive_count(key_name="copy_of_inactive_users")})
		else:
			return render(request,"sanitize_photos.html",{'inactives_remaining':create_inactives_copy()})
	else:
		return redirect("missing_page")
def remove_inactives_notification_activity(request,*args,**kwargs):
	"""Sanitize all notification activity of inactive users from redis2.

	We will be removing the following for each inactive user:
	1) sn:<user_id> --- a sorted set containing home screen 'single notifications',
	2) ua:<user_id> --- a sorted set containing notifications for 'matka',
	3) uar:<user_id> --- a sorted set containing resorted notifications,
	4) np:<user_id>:*:* --- all notification objects associated to the user,
	5) o:*:* --- any objects that remain with 0 subscribers,
	We will do everything in chunks of 10K, so that no server timeouts are encountered.
	"""
	if request.user.username == 'ZippoLighter':
		if request.method == "POST":
			decision = request.POST.get("dec",None)
			if decision == 'No':
				delete_inactives_copy()
				return redirect("home")
			elif decision == 'Yes':
				inactives, last_batch = get_inactives(get_5K=True, key="copy_of_inactive_users")
				id_list = map(itemgetter(1), inactives) #list of user ids
				notification_hash_deleted, sorted_sets_deleted, object_hash_deleted = bulk_sanitize_notifications(id_list)
				if last_batch:
					# delete_inactives_copy(delete_orig=True)
					delete_inactives_copy()
				return render(request,'sanitize_inactives_activity.html',{'inactives_remaining':get_inactive_count(key_name="copy_of_inactive_users"),\
					'last_batch':last_batch,'notif_deleted':notification_hash_deleted,'sorted_sets_deleted':sorted_sets_deleted,\
					'objs_deleted':object_hash_deleted})
		else:
			return render(request,'sanitize_inactives_activity.html',{'inactives_remaining':create_inactives_copy()})
	else:
		return redirect("missing_page")
def remove_inactives_photos(request, *args, **kwargs):
    """Sanitize all inactives' photos that garnered 0 comments.

	1) We get the list of inactive ids (divided into batches for performance)
	2) For each batch, find all photo ids
	3) For each photo id, mark photos that do not have a corresponding photocomment
	4) Retrieve the file name of all such photos and store them in a redis list
	"""
    if request.user.username == 'mhb11':
        if request.method == "POST":
            decision = request.POST.get("dec", None)
            if decision == 'No':
                delete_inactives_copy()
                return redirect('home')
            elif decision == 'Yes':
                inactives, last_batch = get_inactives(
                    get_10K=True, key="copy_of_inactive_users")
                id_list = map(itemgetter(1), inactives)  #list of user ids
                uninteresting_photo_ids = Photo.objects.filter(owner_id__in=id_list, comment_count=0,vote_score=0,latest_comment__isnull=True).\
                values_list('id',flat=True)
                uninteresting_photo_ids = confirm_uninteresting(
                    uninteresting_photo_ids)
                filenames_and_ids = Photo.objects.filter(
                    id__in=uninteresting_photo_ids).values_list(
                        'image_file', 'id')
                save_deprecated_photo_ids_and_filenames(filenames_and_ids)
                if last_batch:
                    delete_inactives_copy(delete_orig=True)
                return render(request,"sanitize_photos.html",{'last_batch':last_batch, \
                 'inactives_remaining':get_inactive_count(key_name="copy_of_inactive_users")})
        else:
            return render(request, "sanitize_photos.html",
                          {'inactives_remaining': create_inactives_copy()})
    else:
        return redirect("missing_page")
def export_nicks(request,*args,**kwargs):
	"""Exports deprecated nicks in a CSV.

	This writes all nicks, scores and ids into a CSV.
	The CSV is then exported.
	"""
	if request.user.username == 'mhb11':
		inactives = get_inactives()
		import csv
		with open('inactives.csv','wb') as f:
			wtr = csv.writer(f, delimiter=',')
			columns = "username score id".split()
			wtr.writerow(columns)
			for inactive in inactives:
				tup = inactive[0].split(":")
				username = tup[0]
				try:
					score = tup[1]
				except:
					score = None
				id_ = inactive[1]
				to_write = [username, score, id_]
				wtr.writerows([to_write])
		return render(request,'deprecate_nicks.html',{})
	else:
		return render(request,'404.html',{})
def remove_inactive_user_sessions(request, *args, **kwargs):
    """
	Sanitize all sessions of deprecated ids.

	TODO: 'Session' table is now defunct - this functionality needs to be updated
	"""
    if request.user.username == 'mhb11':
        if request.method == "POST":
            decision = request.POST.get("dec", None)
            if decision == 'No':
                delete_inactives_copy()
                return redirect('home')
            elif decision == 'Yes':
                inactives, last_batch = get_inactives(
                    get_10K=True, key="copy_of_inactive_users")
                id_list = map(itemgetter(1), inactives)  #list of user ids
                # print "Deleting %s sessions created by %s users" % (Session.objects.filter(user_id__in=id_list).count(), len(id_list))
                # Session.objects.filter(user_id__in=id_list).delete()
                if last_batch:
                    delete_inactives_copy(delete_orig=True)
                return render(request,'sanitize_inactive_sessions.html',{'last_batch':last_batch, \
                 'inactives_remaining':get_inactive_count(key_name="copy_of_inactive_users")})
        else:
            return render(request, 'sanitize_inactive_sessions.html',
                          {'inactives_remaining': create_inactives_copy()})
    else:
        return redirect("missing_page")
def remove_inactives_groups(request, *args, **kwargs):
    """Sanitize all group invites and memberships.
	
	This is from redis 1
	"""
    if request.user.username == 'mhb11':
        if request.method == "POST":
            decision = request.POST.get("dec", None)
            if decision == 'No':
                delete_inactives_copy()
                return redirect('home')
            elif decision == 'Yes':
                inactives, last_batch = get_inactives(
                    get_50K=True, key="copy_of_inactive_users")
                id_list = map(itemgetter(1), inactives)  #list of user ids
                bulk_sanitize_group_invite_and_membership(id_list)
                if last_batch:
                    # delete_inactives_copy(delete_orig=True)
                    delete_inactives_copy()
                return render(request,"sanitize_groups.html",{'last_batch':last_batch, \
                 'inactives_remaining':get_inactive_count(key_name="copy_of_inactive_users")})
        else:
            return render(request, "sanitize_groups.html",
                          {'inactives_remaining': create_inactives_copy()})
    else:
        return redirect("missing_page")
def remove_inactives_notification_activity(request, *args, **kwargs):
    """Sanitize all notification activity of inactive users from redis2.

	We will be removing the following for each inactive user:
	1) sn:<user_id> --- a sorted set containing home screen 'single notifications',
	2) ua:<user_id> --- a sorted set containing notifications for 'matka',
	3) uar:<user_id> --- a sorted set containing resorted notifications,
	4) np:<user_id>:*:* --- all notification objects associated to the user,
	5) o:*:* --- any objects that remain with 0 subscribers,
	We will do everything in chunks of 10K, so that no server timeouts are encountered.
	"""
    if request.user.username == 'ZippoLighter':
        if request.method == "POST":
            decision = request.POST.get("dec", None)
            if decision == 'No':
                delete_inactives_copy()
                return redirect('home')
            elif decision == 'Yes':
                inactives, last_batch = get_inactives(
                    get_5K=True, key="copy_of_inactive_users")
                id_list = map(itemgetter(1), inactives)  #list of user ids
                notification_hash_deleted, sorted_sets_deleted, object_hash_deleted = None, None, None  #bulk_sanitize_notifications(id_list)
                if last_batch:
                    # delete_inactives_copy(delete_orig=True)
                    delete_inactives_copy()
                return render(request,'sanitize_inactives_activity.html',{'inactives_remaining':get_inactive_count(key_name="copy_of_inactive_users"),\
                 'last_batch':last_batch,'notif_deleted':notification_hash_deleted,'sorted_sets_deleted':sorted_sets_deleted,\
                 'objs_deleted':object_hash_deleted})
        else:
            return render(request, 'sanitize_inactives_activity.html',
                          {'inactives_remaining': create_inactives_copy()})
    else:
        return redirect("missing_page")
def export_nicks(request, *args, **kwargs):
    """Exports deprecated nicks in a CSV.

	This writes all nicks, scores and ids into a CSV.
	The CSV is then exported.
	"""
    if request.user.username == 'mhb11':
        inactives = get_inactives()
        import csv
        with open('inactives.csv', 'wb') as f:
            wtr = csv.writer(f, delimiter=',')
            columns = "username score id".split()
            wtr.writerow(columns)
            for inactive in inactives:
                tup = inactive[0].split(":")
                username = tup[0]
                try:
                    score = tup[1]
                except:
                    score = None
                id_ = inactive[1]
                to_write = [username, score, id_]
                wtr.writerows([to_write])
        return render(request, 'deprecate_nicks.html', {})
    else:
        return render(request, '404.html', {})
def change_nicks(request,*args,**kwargs):
	"""This frees up the name space of nicks, 100K at a time.

	Nicks, once taken, are locked out of the namespace.
	This changes nicknames that aren't in use anymore to a random string.
	It also removes their mobile verification and cached uname entries (if they exist)
	It does NOT sync the redis maintained list of usernames used at nickname creation. That must be synced after this is run.
	
	list of functions that make nicks a.k.a. username
	####################
	
	Redis7	
	account_created - username

	Redis6
	#retrieve_group_topic_log - writer_uname
	#final_data.append((dictionary['tp'].decode('utf-8'),dictionary['t'],writer_uname,writer_avurl))

	#retrieve_group_chatter -  credentials[user_id]['uname']
	#final_data.append((credentials[user_id]['uname'],credentials[user_id]['avurl']))

	Redis5

	#####################
	"""
	if request.user.username == 'mhb11':
		if request.method == "POST":
			decision = request.POST.get("dec",None)
			count = int(request.POST.get("count",None))
			if decision == 'No':
				return redirect("home")
			elif decision == 'Yes':
				inactives, last_batch = get_inactives(get_10K=True)
				id_list = map(itemgetter(1), inactives) #list of ids to deprecate
				id_list = map(int, id_list)
				id_len = len(id_list)
				start = count*100000
				end = start+100000-1
				sample_size = id_len+10# ensure sample_size is lesser than (start-end)
				rand_nums = map(str,random.sample(xrange(start,end),sample_size))#strinigied random nums
				invalidated_cached_uname_credentials(user_ids=id_list)# invalidating uname caches in bulk
				counter = 0
				for pk in id_list:
					# change 'd_m_d_' (and can't keep 'i_i__' either) next time this is run; otherwise there will be collisions
					change_nick(target_id=pk, new_nick='d_m_d_'+rand_nums[counter])
					counter += 1
				remove_verified_mob(target_user_ids=id_list)# unverifying users in bulk
				if last_batch:
					return render(request,'deprecate_nicks.html',{})
				else:
					return render(request,'change_nicks.html',{'count':count+1,'nicks_remaining':get_inactive_count()})
			else:
				return redirect("home")
		else:
			return render(request,'change_nicks.html',{'count':1,'nicks_remaining':get_inactive_count()})
	else:
		return redirect("home")
def remove_inactive_user_sessions(request,*args,**kwargs):
	"""Sanitize all sessions of deprecated ids.

	"""
	if request.user.username == 'mhb11':
		if request.method == "POST":
			decision = request.POST.get("dec",None)
			if decision == 'No':
				delete_inactives_copy()
				return redirect("home")
			elif decision == 'Yes':
				inactives, last_batch = get_inactives(get_10K=True, key="copy_of_inactive_users")
				id_list = map(itemgetter(1), inactives) #list of user ids
				# print "Deleting %s sessions created by %s users" % (Session.objects.filter(user_id__in=id_list).count(), len(id_list))
				Session.objects.filter(user_id__in=id_list).delete()
				if last_batch:
					delete_inactives_copy(delete_orig=True)
				return render(request,'sanitize_inactive_sessions.html',{'last_batch':last_batch, \
					'inactives_remaining':get_inactive_count(key_name="copy_of_inactive_users")})
		else:
			return render(request,'sanitize_inactive_sessions.html',{'inactives_remaining':create_inactives_copy()})
	else:
		return redirect("missing_page")
def remove_inactives_groups(request,*args,**kwargs):
	"""Sanitize all group invites and memberships.
	
	This is from redis 1
	"""
	if request.user.username == 'mhb11':
		if request.method == "POST":
			decision = request.POST.get("dec",None)
			if decision == 'No':
				delete_inactives_copy()
				return redirect("home")
			elif decision == 'Yes':
				inactives, last_batch = get_inactives(get_50K=True, key="copy_of_inactive_users")
				id_list = map(itemgetter(1), inactives) #list of user ids
				bulk_sanitize_group_invite_and_membership(id_list)
				if last_batch:
					# delete_inactives_copy(delete_orig=True)
					delete_inactives_copy()
				return render(request,"sanitize_groups.html",{'last_batch':last_batch, \
					'inactives_remaining':get_inactive_count(key_name="copy_of_inactive_users")})
		else:
			return render(request,"sanitize_groups.html",{'inactives_remaining':create_inactives_copy()})
	else:
		return redirect("missing_page")
def change_nicks(request, *args, **kwargs):
    """This frees up the name space of nicks, 100K at a time.

	Nicks, once taken, are locked out of the namespace.
	This changes nicknames that aren't in use anymore to a random string.
	It also removes their mobile verification and cached uname entries (if they exist)
	It does NOT sync the redis maintained list of usernames used at nickname creation. That must be synced after this is run.
	
	list of functions that make nicks a.k.a. username
	####################
	
	Redis7	
	account_created - username

	Redis6
	#retrieve_group_topic_log - writer_uname
	#final_data.append((dictionary['tp'].decode('utf-8'),dictionary['t'],writer_uname,writer_avurl))

	#retrieve_group_chatter -  credentials[user_id]['uname']
	#final_data.append((credentials[user_id]['uname'],credentials[user_id]['avurl']))

	Redis5

	#####################
	"""
    if request.user.username == 'mhb11':
        if request.method == "POST":
            decision = request.POST.get("dec", None)
            count = int(request.POST.get("count", None))
            if decision == 'No':
                return redirect('home')
            elif decision == 'Yes':
                inactives, last_batch = get_inactives(get_10K=True)
                id_list = map(itemgetter(1),
                              inactives)  #list of ids to deprecate
                id_list = map(int, id_list)
                id_len = len(id_list)
                start = count * 100000
                end = start + 100000 - 1
                sample_size = id_len + 10  # ensure sample_size is lesser than (start-end)
                rand_nums = map(
                    str, random.sample(xrange(start, end),
                                       sample_size))  #strinigied random nums
                invalidated_cached_uname_credentials(
                    user_ids=id_list)  # invalidating uname caches in bulk
                counter = 0
                for pk in id_list:
                    # change 'd_m_d_' (and can't keep 'i_i__' either) next time this is run; otherwise there will be collisions
                    change_nick(target_id=pk,
                                new_nick='d_m_d_' + rand_nums[counter])
                    counter += 1
                remove_verified_mob(
                    target_user_ids=id_list)  # unverifying users in bulk
                if last_batch:
                    return render(request, 'deprecate_nicks.html', {})
                else:
                    return render(
                        request, 'change_nicks.html', {
                            'count': count + 1,
                            'nicks_remaining': get_inactive_count()
                        })
            else:
                return redirect('home')
        else:
            return render(request, 'change_nicks.html', {
                'count': 1,
                'nicks_remaining': get_inactive_count()
            })
    else:
        return redirect('home')