Last active
September 23, 2020 09:27
-
-
Save miraajkadam/3759d822cd76675cf193888b6d2c4375 to your computer and use it in GitHub Desktop.
Cleaned up implementation of POST request driver code for /timespent/ view.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from io import TextIOWrapper | |
from datetime import datetime | |
from collections import Counter, defaultdict | |
import csv | |
import re | |
time = request.POST['time'] | |
lower = request.POST['lower'] | |
if lower.isdigit(): | |
lower = int(lower) | |
else: | |
lower = 0 | |
time = datetime.strptime(time, "%H:%M") | |
meeting_end_time = time.strftime("%I:%M:%S %p") | |
FMT = '%I:%M:%S %p' | |
data = list( | |
TextIOWrapper(request.FILES['csv_file1'].file, encoding='cp1252')) | |
attendees = [] | |
for line in data: | |
attendees.append(line) | |
attendees.pop(0) | |
# removes '\x00' from all strings in list of strings | |
attendees = [x.replace('\x00', '') for x in attendees] | |
# removes '\n' occurances in list of strings | |
attendees = list(filter(('\n').__ne__, attendees)) | |
# removes '\n' from end of all strings in list of strings | |
attendees = [x.split('\n')[0] for x in attendees] | |
# calculates length of all strings in list of strings if len = 0 therefore strings is empty | |
sizes = [len(x) for x in attendees] | |
# remove the empty strings by its index from sizes list | |
for _ in range(len(sizes)): | |
if (sizes[_] == 0): | |
attendees.pop(_) | |
# converting list separated into string separated for performing regex op | |
converted_string = [] | |
for i in attendees: | |
x = '' | |
for j in i: | |
x += j | |
converted_string.append(x) | |
# extracting all attendees names from converted_string | |
attendee_name = [] | |
for i in converted_string: | |
x = re.search(r"(.*?)Joined", i) | |
if x: | |
attendee_name.append( | |
i[x.start():x.end()].split('Joined')[0][:-1]) | |
else: | |
x = re.search(r"(.*?)Left", i) | |
attendee_name.append( | |
i[x.start():x.end()].split('Left')[0][:-1]) | |
# Extracting status of attendees: | |
attendee_status = [] | |
for i in converted_string: | |
if ("Joined before" in i): | |
attendee_status.append("Joined") | |
elif ("Joined" in i): | |
attendee_status.append("Joined") | |
else: | |
attendee_status.append("Left") | |
# Extracting Time and date of attendees: | |
attendee_date_time = [] | |
for i in converted_string: | |
if ("Joined before" in i): | |
attendee_date_time.append(i.split("Joined before")[1]) | |
elif ("Joined" in i): | |
attendee_date_time.append(i.split("Joined")[1]) | |
else: | |
attendee_date_time.append(i.split("Left")[1]) | |
#check if the uploaded data is in 24/12 hour format | |
if (attendee_date_time[0][-1].isdigit()): | |
twelve = False | |
# Extracting date from attendee_date_time | |
attendee_date = [] | |
for i in attendee_date_time: | |
x = i[1:] | |
attendee_date.append(x.split(' ', 1)[0]) | |
# Extracting time from attendee_date_time | |
attendee_time24 = [] | |
for i in attendee_date_time: | |
attendee_time24.append(i.split(' ', 1)[1]) | |
attendee_time = [] | |
for i in attendee_time24: | |
tm = datetime.strptime(i, "%H:%M:%S") | |
attendee_time.append(tm.strftime("%I:%M:%S %p")) | |
else: | |
twelve = True | |
# Extracting date from attendee_date_time | |
attendee_date = [] | |
for i in attendee_date_time: | |
x = i[1:] | |
attendee_date.append(x.split(' ', 1)[0]) | |
# Extracting time from attendee_date_time | |
attendee_time = [] | |
for i in attendee_date_time: | |
x = i[1:-1] | |
if (x[len(x) - 1] != "M"): | |
x += "M" # adds "M" at the end if condition is true | |
attendee_time.append(x.split(' ', 1)[1]) | |
# converting output into list of tuples | |
complete_list_dup = list( | |
map( | |
tuple, | |
zip(attendee_name, attendee_status, attendee_date, | |
attendee_time))) | |
# removing major bug caused by people joining by 2+ devices | |
""" | |
complete_list_dup -> List with adjacent "Joined" and "Left" | |
complete_list_dup1 -> List with adjacent "Joined" eliminated but "Left" still there (reversed for latter elimination) | |
complete_list -> List with both adjacent "Joined" and "Left" eliminated | |
""" | |
complete_list_dup1 = [] | |
complete_list_dup1.append(complete_list_dup[0]) | |
for i in range(len(complete_list_dup)): | |
if (complete_list_dup[i][0] == complete_list_dup1[-1][0]): | |
if (complete_list_dup[i][1] == complete_list_dup1[-1][1] == | |
"Joined"): | |
pass | |
else: | |
complete_list_dup1.append(complete_list_dup[i]) | |
else: | |
complete_list_dup1.append(complete_list_dup[i]) | |
#reversing the list so that I only keep 1st occurance of "Left" | |
complete_list_dup1.reverse() | |
complete_list = [] | |
complete_list.append(complete_list_dup1[0]) | |
for i in range(len(complete_list_dup1)): | |
if (complete_list_dup1[i][0] == complete_list[-1][0]): | |
if (complete_list_dup1[i][1] == complete_list[-1][1] == | |
"Left"): | |
pass | |
else: | |
complete_list.append(complete_list_dup1[i]) | |
else: | |
complete_list.append(complete_list_dup1[i]) | |
#reversing the list back to original state. | |
complete_list.reverse() | |
# Counting the number of instances of single attendee | |
instances = Counter(elem[0] for elem in complete_list) | |
# converting Counter output to list of tuples | |
instances_list = [] | |
for (i, j) in instances.items(): | |
instances_list.append((i, j)) | |
# segrating attendees based on partial and full information available | |
attendee_partial = [] | |
attendee_full = [] | |
for (i, j) in instances_list: | |
if (j % 2 == 0): | |
attendee_full.append((i, j)) | |
else: | |
attendee_partial.append((i, j)) | |
# appending attendees in the list with all of their information (full) | |
attendee_full_list = [] | |
for (i, j) in attendee_full: | |
for (name, status, date, time) in complete_list: | |
if (name == i): | |
attendee_full_list.append((name, status, date, time)) | |
# appending attendees in the list with all of their information (partial) | |
attendee_partial_list = [] | |
for (i, j) in attendee_partial: | |
for (name, status, date, time) in complete_list: | |
if (name == i): | |
attendee_partial_list.append((name, status, date, time)) | |
# calculating time for each attendee with instances >=2, via iterating over an attendee >> might return more than one entry for single attendee if instances > 2 | |
name_timespent_full = [] | |
for _ in range(0, len(attendee_full_list) - 1, 2): | |
name_timespent_full.append( | |
(attendee_full_list[_][0], | |
(datetime.strptime(attendee_full_list[_ + 1][3], FMT) - | |
datetime.strptime(attendee_full_list[_][3], | |
FMT)).total_seconds())) | |
# adding total time spent values from all instances obtained from above operation | |
attendee_full_list_sum = defaultdict(int) | |
for i, k in name_timespent_full: | |
attendee_full_list_sum[i] += k | |
# Scraping partial list with instances = 1 | |
attendee_partial_list_one = [] | |
for (i, j) in attendee_partial: | |
if (j == 1): | |
attendee_partial_list_one.append((i, j)) | |
# Scraping partial list with instances > 2 | |
attendee_partial_list_greater = [] | |
for (i, j) in attendee_partial: | |
if (j > 2): | |
attendee_partial_list_greater.append((i, j)) | |
# Creating a list which includes attendees with instances of 1 and every last instance of an attendee | |
attendee_partial_list_stray = [] | |
# appending every attendee with one instance to list | |
for (i, j) in attendee_partial_list_one: | |
for _ in range(len(attendee_partial_list)): | |
if (i == attendee_partial_list[_][0]): | |
attendee_partial_list_stray.append( | |
(attendee_partial_list[_][0], | |
attendee_partial_list[_][1], | |
attendee_partial_list[_][2], | |
attendee_partial_list[_][3])) | |
# appending every last attendee with odd instance to list | |
for (i, j) in attendee_partial_list_greater: | |
for _ in range(len(attendee_partial_list) - 1): | |
if (i == attendee_partial_list[_][0]): | |
if (attendee_partial_list[_][0] == attendee_partial_list[ | |
_ + 1][0]): | |
pass | |
else: | |
attendee_partial_list_stray.append( | |
(attendee_partial_list[_][0], | |
attendee_partial_list[_][1], | |
attendee_partial_list[_][2], | |
attendee_partial_list[_][3])) | |
# making a full list of separated partial attendees to be even attendees | |
attendee_partial_list_even = [] | |
for (i, j) in attendee_partial_list_greater: | |
for _ in range(len(attendee_partial_list) - 1): | |
if (i == attendee_partial_list[_][0]): | |
if (attendee_partial_list[_][0] == attendee_partial_list[ | |
_ + 1][0]): | |
attendee_partial_list_even.append( | |
(attendee_partial_list[_][0], | |
attendee_partial_list[_][1], | |
attendee_partial_list[_][2], | |
attendee_partial_list[_][3])) | |
# calculating time for partial even list | |
attendee_partial_list_even_time = [] | |
for _ in range(0, len(attendee_partial_list_even) - 1, 2): | |
attendee_partial_list_even_time.append(( | |
attendee_partial_list_even[_][0], | |
(datetime.strptime(attendee_partial_list_even[_ + 1][3], FMT) - | |
datetime.strptime(attendee_partial_list_even[_][3], | |
FMT)).total_seconds())) | |
# calculating time for partial stray list | |
attendee_partial_list_stray_time = [] | |
for _ in range(0, len(attendee_partial_list_stray)): | |
attendee_partial_list_stray_time.append( | |
(attendee_partial_list_stray[_][0], | |
(datetime.strptime(meeting_end_time, FMT) - datetime.strptime( | |
attendee_partial_list_stray[_][3], FMT)).total_seconds())) | |
final_strays_list = [] | |
attendee_partial_list_stray_time = attendee_partial_list_stray_time + \ | |
attendee_partial_list_even_time | |
attendee_partial_list_sum = defaultdict(int) | |
for i, k in attendee_partial_list_stray_time: | |
attendee_partial_list_sum[i] += k | |
attendee_partial_list_sum.update(attendee_full_list_sum) | |
final_list = [] | |
for i in attendee_partial_list_sum: | |
final_list.append((i, round(attendee_partial_list_sum[i] / 60))) | |
# To print list in console | |
# for (i, j) in final_list: | |
# print(i, j) | |
final_list.sort(key=lambda x: x[0]) # sorts on A - Z increasing | |
final_list.sort(key=lambda x: x[1], | |
reverse=True) # sorts on time decreasing | |
total = len(final_list) #returns total attendees | |
if twelve: | |
date = attendee_date[0][:-1] | |
else: | |
date = attendee_date[0][:-1] | |
context = { | |
'member_list': final_list, | |
'time': meeting_end_time, | |
'total': total, | |
'date': date, | |
'lower': lower, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://miraaj-timespent.herokuapp.com/timespent/