def put(self, task_id): task_id = int(task_id) json_data = json.loads(self.request.body) was_positive = json_data.get("goodJob") task_event = models.update_task_event_feedback(task_id, was_positive) if task_event: task = models.get_task(task_id) obj = jsons.get_task_json(task) update_json = {} update_json['eventType'] = 'taskFeedback' update_json['taskId'] = task_id update_json['positive'] = task_event.positive_feedback update_json['negative'] = task_event.negative_feedback member = models.get_member_key().get() update_json['doneBy'] = member.first_name + " " + member.last_name #if member.avatar: #update_json['avatar'] = member.avatar json_data = json.dumps(obj) update_data = json.dumps(update_json) publisher.update_clients(models.get_members_list(), update_data) self.response.out.write(json_data)
def get(self, user_id=None): user = models.get_member_key(user_id).get() self.response.headers['Content-Type'] = 'image/png' #user.avatar is sometimes string and this breaks the avatar for users who havent uploaded an image if user.avatar: self.response.out.write(user.avatar) else: default_picture = open('app/blank-picture.jpg', 'rb').read() self.response.out.write(default_picture)
def get_task_json(task): task_dict = {} task_dict[strings.taskID] = task.key.integer_id() task_dict[strings.taskName] = task.name task_dict[strings.description] = task.description task_dict[strings.dateModified] = get_compatible_date_str(task.date_modified) task_dict[strings.frequency] = task.frequency task_dict[strings.taskStyle] = task.style task_dict[strings.everCompleted] = False task_dict[strings.assigned] = False task_dict['order'] = task.order task_dict['difficulty'] = task.difficulty if task.assigned: task_dict[strings.assignedInitials] = task.get_delagated_initials() if task.assigned == models.get_member_key(): task_dict[strings.assigned] = True if task.most_recent_event: # Get the most recent TaskEvent of this Task most_recent_event = task.most_recent_event.get() if most_recent_event: task_dict[strings.everCompleted] = True # Store the feedbacks for that Event task_dict[strings.positiveFeedback] = most_recent_event.positive_feedback task_dict[strings.negativeFeedback] = most_recent_event.negative_feedback # Store the personal feedback for that most recent TaskEvent user_feedback = most_recent_event.get_user_feedback() if len(user_feedback): task_dict[strings.userFeedback] = user_feedback[0].was_positive else: task_dict[strings.userFeedback] = None # Store the initials of the person who completed this TaskEvent task_dict[strings.completedByInitials] = most_recent_event.completed_by.get().get_initials() # The date of the last event task_dict[strings.dateLastTaskEvent] = get_compatible_date_str(most_recent_event.date_completed) else: # If there is no most recent event # We just use the current time as the date for the last TaskEvent date. task_dict[strings.dateLastTaskEvent] = get_compatible_date_str(datetime.datetime.now()) else: # If there is no most recent event # We just use the current time as the date for the last TaskEvent date. task_dict[strings.dateLastTaskEvent] = get_compatible_date_str(datetime.datetime.now()) return task_dict
def get(self): user_id = models.get_member_id() if user_id: user = models.get_member_key(user_id).get() token = channel.create_channel(user_id, 1440) user.channel_token = token user.put() obj = { 'token' : token } self.response.out.write(json.dumps(obj))
def put(self): json_data = json.loads(self.request.body) newOrder = json_data['newOrder'] oldOrder = json_data['oldOrder'] # This is swap # oldOrderTask = models.Task.query(models.Task.order == oldOrder).fetch()[0] # oldOrderTask.order = newOrder # # newOrderTask = models.Task.query(models.Task.order == newOrder).fetch()[0] # newOrderTask.order = oldOrder # # oldOrderTask.put() # newOrderTask.put() # If the new order (index) is bigger, this means that the task was moved down the list. # Hence, all the tasks in between must have their order decreased by 1 oldOrderTask = models.Task.query(models.Task.order == oldOrder).fetch()[0] if newOrder > oldOrder: q = models.Task.query(models.Task.order > oldOrder, models.Task.order <= newOrder) tasks = q.fetch() for task in tasks: task.order -= 1 task.put() # Else the new order (index) is smaller, this means that the task was moved up the list. # Hence, all the tasks in between must have their order increased by 1 else: q = models.Task.query(models.Task.order >= newOrder, models.Task.order < oldOrder) tasks = q.fetch() for task in tasks: task.order += 1 task.put() oldOrderTask.order = newOrder oldOrderTask.put() update_json = {} update_json['eventType'] = 'orderChangedEvent' update_json['taskChangedName'] = oldOrderTask.name member = models.get_member_key().get() update_json['doneBy'] = member.first_name + " " + member.last_name update_data = json.dumps(update_json) publisher.update_clients(models.get_members_list(), update_data) self.response.out.write(oldOrderTask.name + ": last order " + str(oldOrder) + " new order:" + str(newOrder))
def get(self): models.populate_default_values(models.get_household_key_for_current_user()) # Send an event to everyone that table was repopulated update_json = {} update_json['eventType'] = 'populatedEvent' member = models.get_member_key().get() update_json['doneBy'] = member.first_name + " " + member.last_name update_data = json.dumps(update_json) publisher.update_clients(models.get_members_list(), update_data) self.response.out.write("All the data has been populated. <br> NOTE: If you open this URL again, it will be added again.")
def post(self, task_id): task_id = int(task_id) new_task_event = models.add_task_event(task_id).get() task = models.get_task(task_id) obj = jsons.get_task_json(task) update_json = {} update_json['eventType'] = 'taskEvent' update_json['taskId'] = task_id update_json[strings.assignedInitials] = task.get_delagated_initials() update_json[strings.completedByInitials] = new_task_event.completed_by.get().get_initials() member = models.get_member_key().get() update_json['doneBy'] = member.first_name + " " + member.last_name #if member.avatar: #update_json['avatar'] = member.avatar json_data = json.dumps(obj) update_data = json.dumps(update_json) publisher.update_clients(models.get_members_list(), update_data) self.response.out.write(json_data)
def get(self): try: charttype = self.request.GET['charttype'] self.response.headers['Content-Type'] = 'application/json' if charttype == "housechart": # Get the household key houseKey = models.get_household_key_for_current_user() #Get all members membersKeys = models.get_members_list(houseKey,keys_only=True) # response = {"house_name":houseKey.get().name } # response = {} # Chart 1 - Total amount of Difficulty Done and Assigned (TDDA) for each member # Array of Arrays with each data point data = [] # Array of Date Strings labels = [] # Array of member Names series = [] members = models.get_members_list(houseKey, keys_only=False) for member in members: labels.append(member.first_name) tdda = member.total_difficulty_done_assigned if tdda < 0: tdda = 0 data.append(tdda) response["chart1"]= { "data": data, "labels" : labels } # Chart 2 - Activity in the last 7 days # Array of Arrays with each data point data = [] # Array of Date Strings labels = [] # Array of member Names series = [] dateBefore7Days = datetime.datetime.now() - datetime.timedelta(days=6) # Add the labels as weekdays for j in range(5): day = dateBefore7Days + datetime.timedelta(days=j) labels.append(str(day.strftime("%a"))) # Add Yesterday and Today as labels labels.append("Yesterday") labels.append("Today") # For 7 days for i in range(len(membersKeys)): memberData = [] series.append(membersKeys[i].get().first_name) for j in range(7): day = dateBefore7Days + datetime.timedelta(days=j) memberData.append(models.get_all_task_events_count_for_member_for_date(membersKeys[i],day)) data.append(memberData) response["chart2"]= { "data": data, "labels" : labels, "series": series } # Chart 3 - Overall task distribution # Array of Arrays with each data point data = [] # Array of Tasks labels = [] # Array of member Names series = [] # Get the household key houseKey = models.get_household_key_for_current_user() #Get all members taskKeys = models.get_all_tasks_for_household(houseKey) for taskKey in taskKeys: labels.append(taskKey.get().name) # For 7 days for i in range(len(membersKeys)): memberData = [] series.append(membersKeys[i].get().first_name) for taskKey in taskKeys: memberData.append(models.get_all_task_events_count_for_task_and_member(taskKey, membersKeys[i])) data.append(memberData) response["chart3"]= { "data": data, "labels" : labels, "series": series } self.response.out.write(json.dumps(response)) elif charttype == "userchart": if not 'userid' in self.request.GET.keys(): #TODO return an error self.response.out.write("ERROR: userid must be present") pass else: memberKey = models.get_member_key(self.request.GET['userid']) # Get the household key houseKey = models.get_household_key_for_current_user() #Get all members taskKeys = models.get_all_tasks_for_household(houseKey) response = {} taskEventsPerTask = [] for taskKey in taskKeys: numOfTasks = models.get_all_task_events_count_for_task_and_member(taskKey, memberKey) if numOfTasks > 0: element = { 'name': taskKey.get().name , 'value': numOfTasks } taskEventsPerTask.append(element) response['taskeventspertask'] = taskEventsPerTask response['feedbackevents'] = models.get_all_positive_negative_labels_for_member(memberKey, taskKeys) self.response.out.write(json.dumps(response)) elif charttype == "tilechart": if not 'taskid' in self.request.GET.keys(): #TODO return an error self.response.out.write("ERROR: taskid must be present") pass else: taskKey = models.get_task_key(self.request.GET['taskid']) # Get the household key houseKey = models.get_household_key_for_current_user() #Get all members membersKeys = models.get_members_list(houseKey, keys_only=True) response = {} # Array of data points data = [] # Array of Member names labels = [] for i in range(len(membersKeys)): taskEventsCnt = models.get_all_task_events_count_for_task_and_member(taskKey, membersKeys[i]) if taskEventsCnt >0: # Only add the data point if it is bigger 0 labels.append(membersKeys[i].get().first_name) data.append(taskEventsCnt) response["chart1"]= { "data": data, "labels" : labels } self.response.out.write(json.dumps(response)) except Exception as e: # TODO: Return errors. # THIS IS ERROR. The chartype must be specified !!!! But for now just send it like that print(e)