Exemplo n.º 1
0
def new_ticket():
    if request.method == 'GET':
        clients = Client.query.all()
        products = ProductArea.query.all()
        users = TicketUser.query.all()
        return render_template('new_ticket.html',
                               clients=clients,
                               products=products,
                               users=users)
    else:
        title = request.form['title']
        description = request.form['description']
        top_queue = False
        if request.form.get('top_queue'):
            top_queue = True if request.form['top_queue'].lower(
            ) == 'on' else False
        target_date = request.form['target_date']
        client_id = request.form['client']
        product_area_id = request.form['product_area']
        ticket_url = request.form['ticket_url']
        user_id = request.form['assigned_to']

        client = Client.query.get(int(client_id))
        product_area = ProductArea.query.get(int(product_area_id))
        user = TicketUser.query.get(int(user_id))

        if top_queue == True:
            for ticket in client.tickets:
                ticket.decrease_priority()

        ticket = Ticket(
            title=title,
            description=description,
            priority=1 if top_queue == True else len(client.tickets) + 1,
            target=parse_date(target_date),
            ticket_url=ticket_url,
            client=client,
            product_area=product_area,
            assigned_to=user)
        db.session.add(ticket)
        db.session.commit()

        ticket.add_log(constants.TICKET_LOG_TYPE_STATUS_CHANGE)

        return redirect(url_for('tickets'))