def add_reminders_to_plan(user_name, plan_id, reminders_ids): """Adds reminders to plan. :param user_name: users name, who makes query. :param plan_id: plan id :param reminders_ids: list of reminders ids """ plan = get_plan(plan_id) for id in reminders_ids: reminder = get_reminder(id) relation = PlanReminders(plan=plan, reminder=reminder) relation.save()
def add_events_to_plan(user_name, plan_id, events_ids): """Adds events to plan. :param user_name: users name, who makes query :param plan_id: plan id :param events_ids: list of events ids """ plan = get_plan(plan_id) for id in events_ids: event = get_event(id) add_participants_to_event(ADMINS_NAME, id, get_objects_owners(plan)) event.prepare_to_plan() plan.events.add(event)
def remove_events_from_plan(username, plan_id, events_ids): """Remove events from plan. :param user_name: users name, who makes query :param plan_id: plan id :param events_ids: list of events ids """ plan = get_plan(plan_id) for id in events_ids: event = get_event(id) event.status = Statuses.INPROCESS.value event.save() plan.events.remove(event)
def add_tasks_to_plan(user_name, plan_id, tasks_ids): """Adds tasks to plan. :param user_name: users name, who makes query. :param plan_id: plan id :param tasks_ids: list of tasks ids :return: """ plan = get_plan(plan_id) for id in tasks_ids: task = get_task(id) add_owners_to_task(ADMINS_NAME, id, get_objects_owners(plan)) task.prepare_to_plan() plan.tasks.add(task)
def remove_owners_from_plan(username, plan_id, owners): """Remove owners from plan. :param user_name: users name, who makes query. :param plan_id: plan id :param owners: list of owners ids """ plan = get_plan(plan_id) for owner in owners: user = get_user(owner) try: relation = UserPlans.objects.get(user=user, plan=plan) relation.delete() except: continue
def remove_reminders_from_plan(username, plan_id, reminders_ids): """Remove reminders from plan. :param user_name: users name, who makes query. :param plan_id: plan id :param reminders_ids: list of reminders ids """ plan = get_plan(plan_id) for id in reminders_ids: reminder = get_reminder(id) try: relation = PlanReminders.objects.get(plan=plan, reminder=reminder) relation.delete() except: continue
def remove_tasks_from_plan(username, plan_id, tasks_ids): """Removes tasks from plan. :param user_name: users name, who makes query. :param plan_id: plan id :param tasks_ids: list of tasks ids :return: """ plan = get_plan(plan_id) for id in tasks_ids: task = get_task(id) task.status = Statuses.INPROCESS.value task.save() plan.tasks.remove(task)
def add_owners_to_plan(user_name, plan_id, owners): """Adds owners to plan. :param user_name: users name, who makes query. :param plan_id: plan id :param owners: list of owners ids """ plan = get_plan(plan_id) for owner in owners: user = get_user(owner.user_name) relation = UserPlans(user=user, plan=plan, assign_date=datetime.now(), access_level=owner.access_level) relation.save()
def wrapper(username, plan_id, *args, **kwargs): if not username == ADMINS_NAME: user = get_user(username) plan = get_plan(plan_id) try: relation = UserPlans.objects.get(user=user, plan=plan) except ObjectDoesNotExist: msg = ("Invalid operation, you are not the owner " "of the '{}' plan.").format(plan.id) raise PermissionError(msg) if relation.access_level == AccessLevels.EDIT.value: func(username, plan_id, *args, **kwargs) else: msg = ("Permission denied, you can't " "edit '{}' plan.").format(plan.id) raise PermissionError(msg) else: func(username, plan_id, *args, **kwargs)
def edit_plan(user_name, plan_id, title=None, description=None, category=None, priority=None): """Edit plan If the next argument is not None, then change the corresponding field to a new value. """ plan = get_plan(plan_id) if title: plan.title = title if description: plan.description = description if category: plan.category = category if priority: plan.priority = priority plan.save()
def remove_plan(user_name, plan_id): """ Remove plan if it can be removed. """ get_plan(plan_id).delete()