def edit_event(user_name, event_id, title=None, description=None, category=None, priority=None, from_datetime=None, to_datetime=None, place=None): """Edit event If the next argument is not None, then change the corresponding field to a new value. """ event = get_event(event_id) if title: event.title = title if description: event.description = description if category: event.category = category if priority: event.priority = priority if from_datetime: event.from_datetime = from_datetime if to_datetime: event.to_datetime = to_datetime if place: event.place = place event.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 add_reminders_to_event(user_name, event_id, reminders_ids): """Adds reminders to event. :param user_name: users name, who makes query :param event_id: event id :param reminders_ids: list of reminder ids :return: """ event = get_event(event_id) for id in reminders_ids: reminder = get_reminder(id) relation = EventReminders(event=event, reminder=reminder) relation.save()
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 remove_participants_from_event(username, event_id, participants): """Removes participants from event. :param user_name: users name, who makes query :param event_id: event id :param participants: list of participants """ event = get_event(event_id) for participant in participants: user = get_user(participant) try: relation = UserEvents.objects.get(user=user, event=event) relation.delete() except: continue
def remove_reminders_from_event(username, event_id, reminders_ids): """Remove reminders from event. :param user_name: users name, who makes query :param event_id: event id :param reminders_ids: list of reminder ids """ event = get_event(event_id) for id in reminders_ids: reminder = get_reminder(id) try: relation = EventReminders.objects.get(event=event, reminder=reminder) relation.delete() except: continue
def duplicate_event(executor, event, created_at): """Duplicate existing event: get participants, reminders attached to event and simply add a new one. :param executor: who duplicates a event :param task: event to duplicate :param created_at: datetime in which event will be created """ participants = get_objects_owners(event) reminders = [reminder.id for reminder in event.reminders.all()] executor = "{}(Plan)".format(executor) event_id = add_event(executor, event.title, event.from_datetime, event.to_datetime, event.description, event.category, event.priority, event.place, participants, reminders) new_event = get_event(event_id) new_event.created_at = created_at new_event.save()
def wrapper(username, event_id, *args, **kwargs): if not username == ADMINS_NAME: user = get_user(username) event = get_event(event_id) try: relation = UserEvents.objects.get(user=user, event=event) except ObjectDoesNotExist: msg = ("Invalid operation, you are not the owner " "of the '{}' event.").format(event.id) raise PermissionError(msg) if relation.access_level == AccessLevels.EDIT.value: func(username, event_id, *args, **kwargs) else: msg = ("Permission denied, you can't " "edit '{}' event.").format(event.id) raise PermissionError(msg) else: func(username, event_id, *args, **kwargs)
def add_participants_to_event(user_name, event_id, participants): """Adds participants to event. :param user_name: users name, who makes query :param event_id: event id :param participants: list of participants :return: """ event = get_event(event_id) for participant in participants: user = get_user(participant.user_name) try: relation = UserEvents.objects.get(user=user, event=event) except: relation = UserEvents(user=user, event=event, assign_date=datetime.now(), access_level=participant.access_level) relation.save()
def remove_event(user_name, event_id): """ Remove event if it can be removed. """ get_event(event_id).delete()