def read_regex(request): if is_session_active(request.session): if request.method == 'POST': try: regex = request.POST['regex'] is_global_search = request.POST['is_global_search'] matched_goal_family_subsets = Goal.read_regex( regex, is_global_search) json_goal_family_subsets = [] for family_subset in matched_goal_family_subsets: json_family_subset = [] for goal in family_subset: json_family_subset.append(jsonize_goal(goal)) json_goal_family_subsets.append(json_family_subset) return HttpResponse( json.dumps( ResponseWrapper.of(json_goal_family_subsets, ResponseWrapper.ARRAY_RESPONSE))) except MultiValueDictKeyError: return HttpResponse( json.dumps(ResponseWrapper.error('Improper data'))) except re.error: return HttpResponse( json.dumps(ResponseWrapper.error('Not proper regex'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid request'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid session')))
def remove_relation(request): if is_session_active(request.session): if request.method == 'POST': try: parent_id = int(request.POST['parent_id']) child_id = int(request.POST['child_id']) goal_families = Goal.remove_relation(parent_id, child_id) json_families = [] for goal_family in goal_families: json_family = [] for member in goal_family: json_family.append(jsonize_goal(member)) json_families.append(json_family) return HttpResponse( json.dumps( ResponseWrapper.of(json_families, ResponseWrapper.ARRAY_RESPONSE))) except (ValueError, TypeError, MultiValueDictKeyError): return HttpResponse( json.dumps(ResponseWrapper.error('Improper data'))) except ObjectDoesNotExist: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid id'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid request'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid session')))
def delete_if_single(request): if is_session_active(request.session): if request.method == 'POST': try: pk = request.POST['id'] is_deleted = Goal.delete_if_single(pk) if is_deleted is True: return HttpResponse( json.dumps( ResponseWrapper.of( 0, ResponseWrapper.NUMBER_RESPONSE))) else: return HttpResponse( json.dumps(ResponseWrapper.error(is_deleted[1]))) except (ValueError, TypeError, MultiValueDictKeyError): return HttpResponse( json.dumps(ResponseWrapper.error('Improper data'))) except ObjectDoesNotExist: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid id'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid request'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid session')))
def chain_update(request): if is_session_active(request.session): if request.method == 'POST': try: pk = request.POST['id'] description = request.POST['description'] deadline = json.loads(request.POST['deadline']) color = request.POST['color'] if deadline is not None: deadline = dt(year=deadline['year'], month=deadline['month'], day=deadline['day'], hour=deadline['hour'], minute=deadline['minute'], second=deadline['second'], microsecond=deadline['microsecond']) is_updated = Goal.chain_update(pk, description, deadline, color) if is_updated[0] is True: goal_family = is_updated[1] json_family = [] for member in goal_family: json_family.append(jsonize_goal(member)) return HttpResponse( json.dumps( ResponseWrapper.of( json_family, ResponseWrapper.OBJECT_RESPONSE))) else: return HttpResponse( json.dumps(ResponseWrapper.error(is_updated[1]))) except (ValueError, TypeError, MultiValueDictKeyError): return HttpResponse( json.dumps(ResponseWrapper.error('Improper data'))) except ObjectDoesNotExist: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid id'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid request'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid session')))
def read_family(request, pk): if is_session_active(request.session): if request.method == 'POST': try: goal_family = Goal.read_family(pk) json_family = [] for member in goal_family: json_family.append(jsonize_goal(member)) return HttpResponse( json.dumps( ResponseWrapper.of(json_family, ResponseWrapper.ARRAY_RESPONSE))) except ObjectDoesNotExist: return HttpResponse( json.dumps(ResponseWrapper.error('No goal with such id'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid request'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid session')))
def snapshot_month(request): if is_session_active(request.session): if request.method == 'POST': try: year = int(request.POST['year']) month = int(request.POST['month']) per_day_goals = Goal.snapshot_month(year, month) for day, goals in per_day_goals.items(): per_day_goals[day] = list(map(jsonize_goal, goals)) return HttpResponse( json.dumps( ResponseWrapper.of(per_day_goals, ResponseWrapper.OBJECT_RESPONSE))) except (ValueError, MultiValueDictKeyError): return HttpResponse( json.dumps(ResponseWrapper.error('Improper data'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid request'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid session')))
def toggle_is_achieved(request, pk): if is_session_active(request.session): if request.method == 'POST': try: is_saved = Goal.toggle_is_achieved(pk) if is_saved[0] is True: return HttpResponse( json.dumps( ResponseWrapper.of( is_saved[1], ResponseWrapper.OBJECT_RESPONSE))) else: return HttpResponse( json.dumps(ResponseWrapper.error(is_saved[1]))) except ObjectDoesNotExist: return HttpResponse( json.dumps(ResponseWrapper.error('No goal with such id'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid request'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid session')))
def create(request): if is_session_active(request.session): if request.method == 'POST': try: description = request.POST['description'] deadline = json.loads(request.POST['deadline']) color = request.POST['color'] if deadline is not None: deadline = dt(year=deadline['year'], month=deadline['month'], day=deadline['day'], hour=deadline['hour'], minute=deadline['minute'], second=deadline['second'], microsecond=deadline['microsecond']) is_created = Goal.create(description, deadline, color) if is_created[0] is True: return HttpResponse( json.dumps( ResponseWrapper.of( jsonize_goal(is_created[1]), ResponseWrapper.OBJECT_RESPONSE))) else: return HttpResponse( json.dumps(ResponseWrapper.error(is_created[1]))) except (ValueError, TypeError, MultiValueDictKeyError): return HttpResponse( json.dumps(ResponseWrapper.error('Improper data'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid request'))) else: return HttpResponse( json.dumps(ResponseWrapper.error('Invalid session')))
def index(request): if is_session_active(request.session): context = {'isSessionActive': True} else: context = {'isSessionActive': False} return render(request, 'interface/index/index.html', context)