Skip to content

Admin

amlit_helpdesk.admin

amlit_helpdesk.admin.FeatureTicket

Bases: Model

One on one with ticket to have features data and additional data from civitas

recurring_ticket property

recurring_ticket

Return recurring ticket for this feature ticket

assign_community_code

assign_community_code()

assign community code to ticket

Source code in django_project/amlit_helpdesk/models/feature_ticket.py
def assign_community_code(self):
    """ assign community code to ticket"""
    if not self.community_code:
        for feature in self.features:
            try:
                code = FeatureBase.objects.get(id=feature).system.community.code
                self.community_code = code
                self.save()
                return
            except (FeatureBase.DoesNotExist, AttributeError):
                pass

amlit_helpdesk.admin.RecurringTicket

Bases: Model

Recurring info for the ticket Contains all of tickets that already created And also create ticket automatically

calculate_next_date

calculate_next_date()

For calculating new date based on the recurring type

Source code in django_project/amlit_helpdesk/models/recurring_ticket.py
def calculate_next_date(self):
    """ For calculating new date based on the recurring type """
    now = timezone.now()
    next_date = self.last_ticket.created
    try:
        timedelta = self.RECURRING_TIMEDELTA[self.recurring_type]
        if self.recurring_type in self.RECURRING_FROM_START_DATE and self.last_ticket.start_date:
            next_date = self.last_ticket.start_date + timedelta

        while next_date < now:
            next_date = next_date + timedelta

        return next_date
    except KeyError:
        recurring_types_splitted = self.recurring_type.strip().split(' ')
        period = recurring_types_splitted[0]
        try:
            # for weekly
            if period == 'Weekly':
                day = recurring_types_splitted[len(recurring_types_splitted) - 1].strip()
                idx_day = self.DAYS.index(day)

                # shift to next week day
                timedelta = relativedelta(days=1)
                while next_date.weekday() != idx_day:
                    next_date = next_date + timedelta

                # shift to next week if it is the past
                timedelta = relativedelta(weeks=1)
                while next_date < now:
                    next_date = next_date + timedelta
                return next_date

            # For monthly
            elif period == 'Monthly':
                raw_day = recurring_types_splitted[len(recurring_types_splitted) - 1].strip('#')
                day = int(raw_day)
                if day < 1:
                    day = 1
                if day > 31:
                    day = 31
                try:
                    next_date = next_date.replace(day=day)
                except ValueError:
                    day = 30
                    next_date = next_date.replace(day=day)

                # shift to next week if it is the past
                timedelta = relativedelta(months=1)
                while next_date < now:
                    next_date = next_date + timedelta

                # we replace the correct one
                if next_date.day != int(raw_day):
                    recurring_types_splitted[len(recurring_types_splitted) - 1] = f'#{next_date.day}'
                    self.recurring_type = ' '.join(recurring_types_splitted)
                    self.save()
                return next_date

            # For yearly
            elif period == 'Yearly':
                day = recurring_types_splitted[len(recurring_types_splitted) - 2]
                month = self.MONTHS.index(recurring_types_splitted[len(recurring_types_splitted) - 1]) + 1
                next_date = next_date.replace(day=int(day), month=int(month))

                # shift to next week if it is the past
                timedelta = relativedelta(years=1)
                while next_date < now:
                    next_date = next_date + timedelta
                return next_date

        except (KeyError, ValueError):
            pass

        return None

check_recurring_event

check_recurring_event()

Recurring event for creating new ticket next_date is today

Source code in django_project/amlit_helpdesk/models/recurring_ticket.py
def check_recurring_event(self):
    """ Recurring event for creating new ticket next_date is today
    """
    print(f'Check recurring for {self.original_ticket.__str__()}')
    if self.active:
        if date.today() >= self.next_date.date():
            if self.last_ticket.status not in self.STATUS_CAN_RECURRING_CREATED:
                self._create_new_ticket()
            else:
                print('Old ticket is still opened')
        pass

create_recurring staticmethod

create_recurring(
    ticket: Ticket, recurring_type: str
) -> (typing.Optional[RecurringTicket], bool)

Create recurring for the ticket

Source code in django_project/amlit_helpdesk/models/recurring_ticket.py
@staticmethod
def create_recurring(ticket: Ticket, recurring_type: str) -> (typing.Optional["RecurringTicket"], bool):
    """ Create recurring for the ticket"""
    pref = SitePreferences.preferences()
    if ticket.queue.id in pref.recurred_queues_ids:
        return RecurringTicket.objects.get_or_create(
            original_ticket=ticket,
            defaults={
                'recurring_type': recurring_type
            }
        )
    return None, False

setup_data

setup_data(ticket: Ticket = None)

Setup recurring data

Source code in django_project/amlit_helpdesk/models/recurring_ticket.py
def setup_data(self, ticket: Ticket = None):
    """ Setup recurring data """
    changed = False
    if not self.last_ticket:
        ticket = self.original_ticket
        changed = True

    if ticket and self.last_ticket != ticket:
        self.last_ticket = ticket
        self.tickets.add(self.last_ticket)
        changed = True

    # check next date
    next_date = self.calculate_next_date()
    if next_date and next_date != self.next_date:
        self.next_date = next_date
        changed = True
    if changed:
        self.save()

amlit_helpdesk.admin.SchedulerOrganisation

Bases: Model

Overridden scheduler for an organisation

activate

activate()

Activate the schedule

Source code in django_project/amlit_helpdesk/models/scheduler.py
def activate(self):
    """ Activate the schedule"""
    recurring_ticket = self.recurring_ticket
    if recurring_ticket:
        recurring_ticket.active = True
        recurring_ticket.save()

inactivate

inactivate()

Inactivate the schedule

Source code in django_project/amlit_helpdesk/models/scheduler.py
def inactivate(self):
    """ Inactivate the schedule"""
    recurring_ticket = self.recurring_ticket
    if recurring_ticket:
        recurring_ticket.active = False
        recurring_ticket.save()

update

update(data, user: User)

Update the scheduler based on data TODO: LIT We need to fix it to reuse function to create ticket, feature ticket and recurring ticket

Source code in django_project/amlit_helpdesk/models/scheduler.py
def update(self, data, user: User):
    """
    Update the scheduler based on data
    TODO: LIT
     We need to fix it to reuse function to create ticket, feature ticket and recurring ticket
    """
    from amlit_helpdesk.forms.ticket import AmlitTicketForm
    from amlit_helpdesk.views.ticket.update import update_ticket_data
    type_obj = self.feature_type_combination_obj
    if not type_obj:
        return

    preference = SitePreferences.load()
    data['queue'] = preference.recurred_queues.first().id

    features = FeatureBase.objects.filter(
        system__community__code=self.organisation.community_code,
        the_class=type_obj.the_class,
        sub_class=type_obj.sub_class,
        type=type_obj.type
    )
    recurring_ticket = self.recurring_ticket
    if not recurring_ticket:
        data['start_date'] = timezone.now()
        form = AmlitTicketForm(
            None,
            data,
            queue_choices=[(queue.id, queue.title) for queue in preference.recurred_queues.all()],
            assigned_to_choices=self.organisation.operators
        )
        if form.is_valid():
            ticket = form.save(user)
            feature_ticket = FeatureTicket.objects.create(
                ticket=ticket,
                features=list(features.values_list('id', flat=True))
            )
            recurring_ticket, created = RecurringTicket.create_recurring(
                feature_ticket.ticket, form.data['recurring_type'])
            self.recurring_ticket = recurring_ticket
            self.save()
    else:
        ticket = recurring_ticket.last_ticket
        feature_ticket = ticket.featureticket
        feature_ticket.features = list(features.values_list('id', flat=True))
        feature_ticket.save()
        ticket.description = data['body']
        update_ticket_data(ticket, user, data)

amlit_helpdesk.admin.SchedulerTemplate

Bases: Model

Scheduler template to create or update the recurring ticket Mostly used for schedule ticket ofr multi assets

API

amlit_helpdesk.api

Migrations

amlit_helpdesk.migrations

Models

amlit_helpdesk.models

amlit_helpdesk.models.SchedulerOrganisation

Bases: Model

Overridden scheduler for an organisation

activate

activate()

Activate the schedule

Source code in django_project/amlit_helpdesk/models/scheduler.py
def activate(self):
    """ Activate the schedule"""
    recurring_ticket = self.recurring_ticket
    if recurring_ticket:
        recurring_ticket.active = True
        recurring_ticket.save()

inactivate

inactivate()

Inactivate the schedule

Source code in django_project/amlit_helpdesk/models/scheduler.py
def inactivate(self):
    """ Inactivate the schedule"""
    recurring_ticket = self.recurring_ticket
    if recurring_ticket:
        recurring_ticket.active = False
        recurring_ticket.save()

update

update(data, user: User)

Update the scheduler based on data TODO: LIT We need to fix it to reuse function to create ticket, feature ticket and recurring ticket

Source code in django_project/amlit_helpdesk/models/scheduler.py
def update(self, data, user: User):
    """
    Update the scheduler based on data
    TODO: LIT
     We need to fix it to reuse function to create ticket, feature ticket and recurring ticket
    """
    from amlit_helpdesk.forms.ticket import AmlitTicketForm
    from amlit_helpdesk.views.ticket.update import update_ticket_data
    type_obj = self.feature_type_combination_obj
    if not type_obj:
        return

    preference = SitePreferences.load()
    data['queue'] = preference.recurred_queues.first().id

    features = FeatureBase.objects.filter(
        system__community__code=self.organisation.community_code,
        the_class=type_obj.the_class,
        sub_class=type_obj.sub_class,
        type=type_obj.type
    )
    recurring_ticket = self.recurring_ticket
    if not recurring_ticket:
        data['start_date'] = timezone.now()
        form = AmlitTicketForm(
            None,
            data,
            queue_choices=[(queue.id, queue.title) for queue in preference.recurred_queues.all()],
            assigned_to_choices=self.organisation.operators
        )
        if form.is_valid():
            ticket = form.save(user)
            feature_ticket = FeatureTicket.objects.create(
                ticket=ticket,
                features=list(features.values_list('id', flat=True))
            )
            recurring_ticket, created = RecurringTicket.create_recurring(
                feature_ticket.ticket, form.data['recurring_type'])
            self.recurring_ticket = recurring_ticket
            self.save()
    else:
        ticket = recurring_ticket.last_ticket
        feature_ticket = ticket.featureticket
        feature_ticket.features = list(features.values_list('id', flat=True))
        feature_ticket.save()
        ticket.description = data['body']
        update_ticket_data(ticket, user, data)

amlit_helpdesk.models.SchedulerTemplate

Bases: Model

Scheduler template to create or update the recurring ticket Mostly used for schedule ticket ofr multi assets

Serializers

amlit_helpdesk.serializer

Views

amlit_helpdesk.views