Exemplo n.º 1
0
def ticket_claim(ticket_id=False):
    if ticket_id:
        # claim ticket
        ticket = FlicketTicket.query.filter_by(id=ticket_id).first()

        if ticket.assigned == g.user:
            flash(gettext('You have already been assigned this ticket.'),
                  category='success')
            return redirect(
                url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

        # set status to in work
        status = FlicketStatus.query.filter_by(status='In Work').first()
        ticket.assigned = g.user
        g.user.total_assigned += 1
        ticket.current_status = status
        ticket.last_updated = datetime.datetime.now()
        db.session.commit()

        # add action record
        add_action(ticket, 'claim')

        # send email notifications
        f_mail = FlicketMail()
        f_mail.assign_ticket(ticket=ticket)

        flash(gettext('You claimed ticket: %(value)s', value=ticket.id))
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

    return redirect(url_for('flicket_bp.tickets'))
Exemplo n.º 2
0
def release(ticket_id=False):
    if ticket_id:

        ticket = FlicketTicket.query.filter_by(id=ticket_id).first()

        # is ticket assigned.
        if not ticket.assigned:
            flash('Ticket has not been assigned')
            return redirect(
                url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

        # check ticket is owned by user or user is admin
        if (ticket.assigned.id != g.user.id) and (not g.user.is_admin):
            flash('You can not release a ticket you are not working on.')
            return redirect(
                url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

        # set status to open
        status = FlicketStatus.query.filter_by(status='Open').first()
        ticket.current_status = status
        ticket.assigned = None
        db.session.commit()

        # add action record
        add_action(action='release', ticket=ticket)

        # send email to state ticket has been released.
        f_mail = FlicketMail()
        f_mail.release_ticket(ticket)

        flash('You released ticket: {}'.format(ticket.id))
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

    return redirect(url_for('flicket_bp.tickets'))
Exemplo n.º 3
0
def ticket_assign(ticket_id=False):
    form = AssignUserForm()
    ticket = FlicketTicket.query.filter_by(id=ticket_id).one()

    if ticket.current_status.status == 'Closed':
        flash(gettext("Can't assign a closed ticket."), category='warning')
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

    if form.validate_on_submit():

        user = FlicketUser.query.filter_by(username=form.username.data).first()

        if ticket.assigned == user:
            flash(gettext('User is already assigned to ticket.'),
                  category='warning')
            return redirect(
                url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

        # set status to in work
        status = FlicketStatus.query.filter_by(status='In Work').first()
        # assign ticket
        ticket.assigned = user
        ticket.current_status = status
        ticket.last_updated = datetime.datetime.now()

        if not user.total_assigned:
            user.total_assigned = 1
        else:
            user.total_assigned += 1

        # add action record
        add_action(ticket, 'assign', recipient=user)

        # subscribe to the ticket
        if not ticket.is_subscribed(user):
            subscribe = FlicketSubscription(ticket=ticket, user=user)
            db.session.add(subscribe)

        db.session.commit()

        # send email to state ticket has been assigned.
        f_mail = FlicketMail()
        f_mail.assign_ticket(ticket)

        flash(gettext('You reassigned ticket: {} to {}'.format(
            ticket.id, user.name)),
              category='success')
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

    title = gettext('Assign Ticket')

    return render_template("flicket_assign.html",
                           title=title,
                           form=form,
                           ticket=ticket)
Exemplo n.º 4
0
    def run(self):
        # find all users
        users = FlicketUser.query.all()
        for user in users:
            # that have created a ticket or have a ticket assigned to them.
            tickets = FlicketTicket.query.filter(
                FlicketTicket.user == user).filter(
                    FlicketTicket.assigned == user).filter(
                        FlicketTicket.status_id != 2)

            if tickets.count() > 0:
                mail = FlicketMail()
                mail.tickets_not_closed(user, tickets)
                time.sleep(10)
Exemplo n.º 5
0
def ticket_assign(ticket_id=False):
    form = AssignUserForm()
    ticket = FlicketTicket.query.filter_by(id=ticket_id).one()

    if ticket.current_status.status == "Closed":
        flash(gettext("Can't assign a closed ticket."))
        return redirect(url_for("flicket_bp.ticket_view", ticket_id=ticket_id))

    if form.validate_on_submit():

        user = FlicketUser.query.filter_by(username=form.username.data).first()

        if ticket.assigned == user:
            flash(gettext("User is already assigned to ticket."))
            return redirect(
                url_for("flicket_bp.ticket_view", ticket_id=ticket.id))

        # assign ticket
        ticket.assigned = user
        ticket.current_status = FlicketStatus.query.filter_by(id=1).first()

        if not user.total_assigned:
            user.total_assigned = 1
        else:
            user.total_assigned += 1

        # add action record
        add_action(ticket, "assign", recipient=user)

        # subscribe to the ticket
        if not ticket.is_subscribed(user):
            subscribe = FlicketSubscription(ticket=ticket, user=user)
            db.session.add(subscribe)

        db.session.commit()

        # send email to state ticket has been assigned.
        f_mail = FlicketMail()
        f_mail.assign_ticket(ticket)

        flash(gettext(f"You reassigned ticket: {ticket.id}s to {user.name}"))
        return redirect(url_for("flicket_bp.ticket_view", ticket_id=ticket.id))

    title = gettext("Assign Ticket")

    return render_template("flicket_assign.html",
                           title=title,
                           form=form,
                           ticket=ticket)
Exemplo n.º 6
0
def ticket_create():
    form = CreateTicketForm()

    if form.validate_on_submit():

        # this is a new post so ticket status is 'open'
        ticket_status = FlicketStatus.query.filter_by(status='open').first()
        ticket_priority = FlicketPriority.query.filter_by(
            id=int(form.priority.data)).first()
        ticket_category = FlicketCategory.query.filter_by(
            id=int(form.category.data)).first()

        files = request.files.getlist("file")
        upload_attachments = UploadAttachment(files)
        if upload_attachments.are_attachements():
            upload_attachments.upload_files()

        # submit ticket data to database
        new_ticket = FlicketTicket(title=form.title.data,
                                   date_added=datetime.datetime.now(),
                                   user=g.user,
                                   current_status=ticket_status,
                                   content=form.content.data,
                                   ticket_priority=ticket_priority,
                                   category=ticket_category)
        db.session.add(new_ticket)

        # add attachments to the dataabase.
        upload_attachments.populate_db(new_ticket)
        # subscribe user to ticket
        subscribe = FlicketSubscription(user=g.user, ticket=new_ticket)
        db.session.add(subscribe)

        # commit changes to the database
        db.session.commit()

        # send email
        # TEST SEND EMAIL HERE
        f_mail = FlicketMail()
        f_mail.create_ticket(ticket=new_ticket)

        flash('New Ticket created.', category='success')

        return redirect(
            url_for('flicket_bp.ticket_view', ticket_id=new_ticket.id))

    return render_template('flicket_create.html',
                           title='Flicket - Create Ticket',
                           form=form)
Exemplo n.º 7
0
def email_test():
    form = EmailTest()

    if form.validate_on_submit():
        # send email notification
        mail = FlicketMail()
        mail.test_email([form.email_address.data])
        flash(Markup(gettext(
            'Flicket has tried to send an email to the address you entered. Please check your inbox. If no email has '
            'arrived please double check the <a href="{}{}">config</a> settings.'.format(
                app.config['base_url'], url_for('admin_bp.config')))), category='warning')

    return render_template('admin_email_test.html',
                           title='Send Email Test',
                           form=form)
Exemplo n.º 8
0
def password_reset():
    form = PasswordResetForm()

    if form.validate_on_submit():
        new_password = FlicketUser.generate_password()
        hashed_password = hash_password(new_password)
        user = FlicketUser.query.filter_by(email=form.email.data).first()
        user.password = hashed_password
        db.session.commit()

        email = FlicketMail()
        email.password_reset(user, new_password)

        flash(gettext('Password reset. Please check your email for your new password'))
        return redirect(url_for('flicket_bp.login'))

    title = 'Password Reset'
    return render_template('password_reset.html', form=form, title=title)
Exemplo n.º 9
0
def ticket_assign(ticket_id=False):
    form = SearchEmailForm()
    ticket = FlicketTicket.query.filter_by(id=ticket_id).one()

    if ticket.current_status.status == 'Closed':
        flash("Can't assign a closed ticket.")
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

    if form.validate_on_submit():

        user = FlicketUser.query.filter_by(email=form.email.data).first()

        if ticket.assigned == user:
            flash('User is already assigned to ticket silly')
            return redirect(
                url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

        # set status to in work
        status = FlicketStatus.query.filter_by(status='In Work').first()
        # assign ticket
        ticket.assigned = user
        ticket.current_status = status

        # add action record
        add_action(action='assign', ticket=ticket, recipient=user)

        # subscribe to the ticket
        if not ticket.is_subscribed(user):
            subscribe = FlicketSubscription(ticket=ticket, user=user)
            db.session.add(subscribe)

        db.session.commit()

        # send email to state ticket has been assigned.
        f_mail = FlicketMail()
        f_mail.assign_ticket(ticket)

        flash('You reassigned ticket:{}'.format(ticket.id))
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

    return render_template("flicket_assign.html",
                           title="Assign Ticket",
                           form=form,
                           ticket=ticket)
Exemplo n.º 10
0
def change_status(ticket_id, status):
    ticket = FlicketTicket.query.filter_by(id=ticket_id).first()
    closed = FlicketStatus.query.filter_by(status=status).first()

    # Check to see if user is authorised to close ticket.
    edit = False
    if ticket.user == g.user:
        edit = True
    if ticket.assigned == g.user:
        edit = True
    if g.user.is_admin:
        edit = True

    if not edit:
        flash(
            gettext(
                "Only the person to which the ticket has been assigned, creator or Admin can close this ticket."
            ),
            category="warning",
        )
        return redirect(url_for("flicket_bp.ticket_view", ticket_id=ticket_id))

    # Check to see if the ticket is already closed.
    if ticket.current_status.status == "Closed":
        flash(gettext("Ticket is already closed."), category="warning")
        return redirect(url_for("flicket_bp.ticket_view", ticket_id=ticket.id))

    f_mail = FlicketMail()
    f_mail.close_ticket(ticket)

    # add action record
    add_action(ticket, "close")

    ticket.current_status = closed
    ticket.assigned_id = None
    db.session.commit()

    flash(
        gettext("Ticket %(value)s closed.", value=str(ticket_id).zfill(5)),
        category="success",
    )

    return redirect(url_for("flicket_bp.tickets"))
Exemplo n.º 11
0
def email_test():
    form = EmailTest()

    if form.validate_on_submit():
        # send email notification
        mail = FlicketMail()
        mail.test_email([form.email_address.data])
        flash(
            Markup(
                gettext(
                    f"Flicket has tried to send an email to the address you entered. Please check your inbox. If no email has "
                    f'arrived please double check the <a href="{app.config["base_url"]}{url_for("admin_bp.config")}">config</a>'
                    f" settings.")),
            category="warning",
        )

    return render_template("admin_email_test.html",
                           title="Send Email Test",
                           form=form)
Exemplo n.º 12
0
def release(ticket_id=False):

    if ticket_id:

        ticket = FlicketTicket.query.filter_by(id=ticket_id).first()

        # is ticket assigned.
        if not ticket.assigned:
            flash(gettext("Ticket has not been assigned"))
            return redirect(
                url_for("flicket_bp.ticket_view", ticket_id=ticket_id))

        # check ticket is owned by user or user is admin
        if (ticket.assigned.id != g.user.id) and (not g.user.is_admin):
            flash(
                gettext(
                    "You can not release a ticket you are not working on."))
            return redirect(
                url_for("flicket_bp.ticket_view", ticket_id=ticket_id))

        # set status to open
        status = FlicketStatus.query.filter_by(status="New").first()
        ticket.current_status = status
        user = ticket.assigned
        ticket.assigned = None
        user.total_assigned -= 1

        db.session.commit()

        # add action record
        add_action(ticket, "release")

        # send email to state ticket has been released.
        f_mail = FlicketMail()
        f_mail.release_ticket(ticket)

        flash(gettext("You released ticket: %(value)s", value=ticket.id))
        return redirect(url_for("flicket_bp.ticket_view", ticket_id=ticket.id))

    return redirect(url_for("flicket_bp.tickets"))
Exemplo n.º 13
0
def change_status(ticket_id, status):
    ticket = FlicketTicket.query.filter_by(id=ticket_id).first()
    closed = FlicketStatus.query.filter_by(status=status).first()

    # Check to see if user is authorised to close ticket.
    edit = False
    if ticket.user == g.user:
        edit = True
    if ticket.assigned == g.user:
        edit = True
    if g.user.is_admin:
        edit = True

    if not edit:
        flash(gettext(
            'Only the person to which the ticket has been assigned, creator or Admin can close this ticket.'
        ),
              category='warning')
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

    # Check to see if the ticket is already closed.
    if ticket.current_status.status == 'Closed':
        flash(gettext('Ticket is already closed.'), category='warning')
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

    f_mail = FlicketMail()
    f_mail.close_ticket(ticket)

    # add action record
    add_action(ticket, 'close')

    ticket.current_status = closed
    ticket.assigned_id = None
    ticket.last_updated = datetime.datetime.now()
    db.session.commit()

    flash(gettext('Ticket %(value)s closed.', value=str(ticket_id).zfill(5)),
          category='success')

    return redirect(url_for('flicket_bp.tickets'))
Exemplo n.º 14
0
def ticket_claim(ticket_id=False):
    if ticket_id:
        # claim ticket
        ticket = FlicketTicket.query.filter_by(id=ticket_id).first()

        # set status to in work
        status = FlicketStatus.query.filter_by(status='In Work').first()
        ticket.assigned = g.user
        ticket.current_status = status
        db.session.commit()

        # add action record
        add_action(action='claim', ticket=ticket)

        # send email notifications
        f_mail = FlicketMail()
        f_mail.assign_ticket(ticket=ticket)

        flash('You claimed ticket:{}'.format(ticket.id))
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

    return redirect(url_for('flicket_bp.tickets'))
Exemplo n.º 15
0
def release(ticket_id=False):

    if ticket_id:

        ticket = FlicketTicket.query.filter_by(id=ticket_id).first()

        # is ticket assigned.
        if not ticket.assigned:
            flash(gettext('Ticket has not been assigned'), category='warning')
            return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

        # check ticket is owned by user or user is admin
        if (ticket.assigned.id != g.user.id) and (not g.user.is_admin):
            flash(gettext('You can not release a ticket you are not working on.'), category='warning')
            return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

        # set status to open
        status = FlicketStatus.query.filter_by(status='Open').first()
        ticket.current_status = status
        ticket.last_updated = datetime.datetime.now()
        user = ticket.assigned
        ticket.assigned = None
        user.total_assigned -= 1

        db.session.commit()

        # add action record
        add_action(ticket, 'release')

        # send email to state ticket has been released.
        f_mail = FlicketMail()
        f_mail.release_ticket(ticket)

        flash(gettext('You released ticket: %(value)s', value=ticket.id), category='success')
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket.id))

    return redirect(url_for('flicket_bp.tickets'))
Exemplo n.º 16
0
def ticket_view(ticket_id, page=1):
    # todo: make sure underscores aren't allowed in usernames as it breaks markdown?

    # is ticket number legitimate
    ticket = FlicketTicket.query.filter_by(id=ticket_id).first()

    if not ticket:
        flash(gettext('Cannot find ticket: "%(value)s"', value=ticket_id), category='warning')
        return redirect(url_for('flicket_bp.tickets'))

    # find all replies to ticket.
    replies = FlicketPost.query.filter_by(ticket_id=ticket_id).order_by(FlicketPost.date_added.asc())

    # get reply id's
    post_rid = request.args.get('post_rid')
    ticket_rid = request.args.get('ticket_rid')

    form = ReplyForm()

    # add reply post
    if form.validate_on_submit():

        # upload file if user has selected one and the file is in accepted list of
        files = request.files.getlist("file")
        upload_attachments = UploadAttachment(files)
        if upload_attachments.are_attachements():
            upload_attachments.upload_files()

        new_reply = FlicketPost(
            ticket=ticket,
            user=g.user,
            date_added=datetime.datetime.now(),
            content=form.content.data,
        )
        ticket.status_id = form.status.data
        db.session.add(new_reply)

        # add files to database.
        upload_attachments.populate_db(new_reply)

        # change ticket status to open if closed.
        if ticket.current_status.status.lower() == 'closed':
            ticket_open = FlicketStatus.query.filter_by(status='Open').first()
            ticket.current_status = ticket_open

        # subscribe to the ticket
        if not ticket.is_subscribed(g.user):
            subscribe = FlicketSubscription(
                ticket=ticket,
                user=g.user
            )
            db.session.add(subscribe)

        # add count of 1 to users total posts.
        g.user.total_posts += 1

        db.session.commit()

        # send email notification
        mail = FlicketMail()
        mail.reply_ticket(ticket=ticket, reply=new_reply)

        flash(gettext('You have replied to ticket %(value_1)s: %(value_2)s.', value_1=ticket.id_zfill,
                      value_2=ticket.title), category="success")

        # if the reply has been submitted for closure.
        if form.submit_close.data:
            return redirect(url_for('flicket_bp.change_status', ticket_id=ticket.id, status='Closed'))

        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

    # get post id and populate contents for auto quoting
    if post_rid:
        query = FlicketPost.query.filter_by(id=post_rid).first()
        reply_contents = gettext("%(value_1)s wrote on %(value_2)s\r\n\r\n%(value_3)s",
                                 value_1=query.user.name,
                                 value_2=query.date_added,
                                 value_3=query.content)
        form.content.data = block_quoter(reply_contents)
    if ticket_rid:
        reply_contents = gettext("%(value_1)s wrote on %(value_2)s\r\n\r\n%(value_3)s",
                                 value_1=ticket.user.name,
                                 value_2=ticket.date_added,
                                 value_3=ticket.content)
        form.content.data = block_quoter(reply_contents)

    replies = replies.paginate(page, app.config['posts_per_page'])

    form.status.data = ticket.status_id

    title = gettext('View Ticket')

    return render_template('flicket_view.html',
                           title=title,
                           ticket=ticket,
                           form=form,
                           replies=replies,
                           page=page)
Exemplo n.º 17
0
def ticket_view(ticket_id, page=1):
    # todo: make sure underscores aren't allowed in usernames as it breaks markdown?

    # is ticket number legitimate
    ticket = FlicketTicket.query.filter_by(id=ticket_id).first()

    if not ticket:
        flash(gettext('Cannot find ticket: "%(value)s"', value=ticket_id),
              category='warning')
        return redirect(url_for('flicket_bp.tickets'))

    # find all replies to ticket.
    replies = FlicketPost.query.filter_by(ticket_id=ticket_id).order_by(
        FlicketPost.date_added.asc())

    # get reply id's
    post_id = request.args.get('post_id')
    ticket_rid = request.args.get('ticket_rid')

    form = ReplyForm()
    subscribers_form = SubscribeUser()

    # add subscribed user
    if subscribers_form.sub_user.data and subscribers_form.validate_on_submit(
    ):
        user = FlicketUser.query.filter_by(
            username=subscribers_form.username.data).first()
        if user:
            if subscribe_user(ticket, user):
                flash(gettext('User subscribed.'), category='success')
            else:
                flash(gettext('User already subscribed.'), category="warning")
        else:
            flash(gettext('Invalid username.'), category='warning')
        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

    # add reply post
    if (form.submit.data
            or form.submit_close.data) and form.validate_on_submit():
        # upload file if user has selected one and the file is in accepted list of
        files = request.files.getlist("file")
        upload_attachments = UploadAttachment(files)
        if upload_attachments.are_attachments():
            upload_attachments.upload_files()

        new_reply = FlicketPost(
            ticket=ticket,
            user=g.user,
            date_added=datetime.datetime.now(),
            content=form.content.data,
            hours=form.hours.data,
        )

        if ticket.status_id != form.status.data:
            status = FlicketStatus.query.get(form.status.data)
            ticket.current_status = status
            add_action(ticket,
                       'status',
                       data={
                           'status_id': status.id,
                           'status': status.status
                       })

        if ticket.ticket_priority_id != form.priority.data:
            priority = FlicketPriority.query.get(form.priority.data)
            ticket.ticket_priority = priority
            add_action(ticket,
                       'priority',
                       data={
                           'priority_id': priority.id,
                           'priority': priority.priority
                       })

        db.session.add(new_reply)

        # add files to database.
        upload_attachments.populate_db(new_reply)

        # change ticket status to open if closed.
        if ticket.current_status.status.lower() == 'closed':
            ticket_open = FlicketStatus.query.filter_by(status='Open').first()
            ticket.current_status = ticket_open

        # subscribe to the ticket
        if not ticket.is_subscribed(g.user):
            subscribe = FlicketSubscription(ticket=ticket, user=g.user)
            db.session.add(subscribe)

        # add count of 1 to users total posts.
        g.user.total_posts += 1

        print('updating updating')
        ticket.last_updated = datetime.datetime.now()

        db.session.commit()

        # send email notification
        mail = FlicketMail()
        mail.reply_ticket(ticket=ticket, reply=new_reply, user=g.user)

        flash(gettext('You have replied to ticket %(value_1)s: %(value_2)s.',
                      value_1=ticket.id_zfill,
                      value_2=ticket.title),
              category="success")

        # if the reply has been submitted for closure.
        if form.submit_close.data:
            return redirect(
                url_for('flicket_bp.change_status',
                        ticket_id=ticket.id,
                        status='Closed'))

        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))

    # get post id and populate contents for auto quoting
    if post_id:
        query = FlicketPost.query.filter_by(id=post_id).first()
        reply_contents = gettext(
            "%(value_1)s wrote on %(value_2)s\r\n\r\n%(value_3)s",
            value_1=query.user.name,
            value_2=query.date_added,
            value_3=query.content)
        form.content.data = block_quoter(reply_contents)

    if ticket_rid:
        reply_contents = gettext(
            "%(value_1)s wrote on %(value_2)s\r\n\r\n%(value_3)s",
            value_1=ticket.user.name,
            value_2=ticket.date_added,
            value_3=ticket.content)
        form.content.data = block_quoter(reply_contents)

    replies = replies.paginate(page, app.config['posts_per_page'])

    form.status.data = ticket.status_id
    form.priority.data = ticket.ticket_priority_id

    title = f"Ticket #{ticket.id_zfill} {ticket.title}"

    # display or not category change link
    change_category = app.config['change_category']
    if change_category and app.config[
            'change_category_only_admin_or_super_user']:
        if not g.user.is_admin and not g.user.is_super_user:
            change_category = False

    return render_template('flicket_view.html',
                           title=title,
                           ticket=ticket,
                           form=form,
                           subscribers_form=subscribers_form,
                           replies=replies,
                           change_category=change_category,
                           page=page)
Exemplo n.º 18
0
def ticket_view(ticket_id, page=1):
    # todo: make sure underscores aren't allowed in usernames as it breaks markdown?

    # is ticket number legitimate
    ticket = FlicketTicket.query.filter_by(id=ticket_id).first()

    if not ticket:
        flash(
            gettext('Cannot find ticket: "%(value)s"', value=ticket_id),
            category="warning",
        )
        return redirect(url_for("flicket_bp.tickets"))

    # find all replies to ticket.
    replies = FlicketPost.query.filter_by(ticket_id=ticket_id).order_by(
        FlicketPost.date_added.asc()
    )

    # get reply id's
    post_rid = request.args.get("post_rid")
    ticket_rid = request.args.get("ticket_rid")

    form = ReplyForm()
    subscribers_form = SubscribeUser()

    # add subscribed user
    if subscribers_form.validate_on_submit():

        user = FlicketUser.query.filter_by(
            username=subscribers_form.username.data
        ).first()
        if subscribe_user(ticket, user):
            flash(gettext("User subscribed."), category="success")
        else:
            flash(gettext("User already subscribed."), category="warning")
        return redirect(url_for("flicket_bp.ticket_view", ticket_id=ticket_id))

    # add reply post
    if form.validate_on_submit():
        # upload file if user has selected one and the file is in accepted list of
        files = request.files.getlist("file")
        upload_attachments = UploadAttachment(files)
        if upload_attachments.are_attachments():
            upload_attachments.upload_files()

        new_reply = FlicketPost(
            ticket=ticket,
            user=g.user,
            date_added=datetime.datetime.now(),
            content=form.content.data,
            days=form.days.data,
        )

        if ticket.request_stage_id != form.request_stage.data:
            request_stage = FlicketRequestStage.query.get(form.request_stage.data)
            ticket.request_stage = request_stage
            add_action(
                ticket,
                "request_stage",
                data={
                    "request_stage_id": request_stage.id,
                    "request_stage": request_stage.request_stage,
                },
            )

        if ticket.procedure_stage_id != form.procedure_stage.data:
            procedure_stage = FlicketProcedureStage.query.get(form.procedure_stage.data)
            ticket.procedure_stage = procedure_stage
            add_action(
                ticket,
                "procedure_stage",
                data={
                    "procedure_stage_id": procedure_stage.id,
                    "procedure_stage": procedure_stage.procedure_stage,
                },
            )

        db.session.add(new_reply)

        # add files to database.
        upload_attachments.populate_db(new_reply)

        # change ticket status to open if closed.
        if ticket.current_status and ticket.current_status.status == "Closed":
            ticket_open = FlicketStatus.query.filter_by(status="Open").first()
            ticket.current_status = ticket_open

        elif not ticket.current_status:
            ticket_open = FlicketStatus.query.filter_by(status="Open").first()
            ticket.current_status = ticket_open

        # subscribe to the ticket
        if not ticket.is_subscribed(g.user):
            subscribe = FlicketSubscription(ticket=ticket, user=g.user)
            db.session.add(subscribe)

        # add count of 1 to users total posts.
        g.user.total_posts += 1

        db.session.commit()

        # send email notification
        mail = FlicketMail()
        mail.reply_ticket(ticket=ticket, reply=new_reply, user=g.user)

        flash(
            gettext(
                "You have replied to ticket %(value_1)s: %(value_2)s.",
                value_1=ticket.id_zfill,
                value_2=ticket.title,
            ),
            category="success",
        )

        # if the reply has been submitted for closure.
        if form.submit_close.data:
            return redirect(
                url_for(
                    "flicket_bp.change_status", ticket_id=ticket.id, status="Closed"
                )
            )

        return redirect(url_for("flicket_bp.ticket_view", ticket_id=ticket_id))

    # get post id and populate contents for auto quoting
    if post_rid:
        query = FlicketPost.query.filter_by(id=post_rid).first()
        reply_contents = gettext(
            "%(value_1)s wrote on %(value_2)s\r\n\r\n%(value_3)s",
            value_1=query.user.name,
            value_2=query.date_added,
            value_3=query.content,
        )
        form.content.data = block_quoter(reply_contents)
    if ticket_rid:
        reply_contents = gettext(
            "%(value_1)s wrote on %(value_2)s\r\n\r\n%(value_3)s",
            value_1=ticket.user.name,
            value_2=ticket.date_added,
            value_3=ticket.content,
        )
        form.content.data = block_quoter(reply_contents)

    replies = replies.paginate(page, app.config["posts_per_page"])

    form.request_stage.data = ticket.request_stage_id
    form.procedure_stage.data = ticket.procedure_stage_id

    title = gettext("View Ticket")

    # display or not domain change link
    change_domain = app.config["change_domain"]
    if change_domain and app.config["change_domain_only_admin_or_super_user"]:
        if not g.user.is_admin and not g.user.is_super_user:
            change_domain = False

    return render_template(
        "flicket_view.html",
        title=title,
        ticket=ticket,
        form=form,
        subscribers_form=subscribers_form,
        replies=replies,
        change_domain=change_domain,
        page=page,
    )