Example #1
0
def get_subscriptions(page=1, ticket_id=None):
    subscriptions = FlicketSubscription.query
    if ticket_id:
        subscriptions = subscriptions.filter_by(ticket_id=ticket_id)
    per_page = min(request.args.get('per_page', app.config['posts_per_page'], type=int), 100)
    data = FlicketSubscription.to_collection_dict(subscriptions, page, per_page, 'bp_api.get_subscriptions',
                                                  ticket_id=ticket_id)
    return jsonify(data)
Example #2
0
    def create_ticket(title=None,
                      user=None,
                      content=None,
                      priority=None,
                      category=None,
                      files=None,
                      hours=0):
        """
        :param title:
        :param user:
        :param content:
        :param priority:
        :param category:
        :param files:
        :param hours:
        :return:
        """

        ticket_status = FlicketStatus.query.filter_by(status="Open").first()
        ticket_priority = FlicketPriority.query.filter_by(
            id=int(priority)).first()
        ticket_category = FlicketCategory.query.filter_by(
            id=int(category)).first()

        upload_attachments = UploadAttachment(files)
        if upload_attachments.are_attachments():
            upload_attachments.upload_files()

        date_added = datetime.datetime.now()

        # submit ticket data to database
        new_ticket = FlicketTicket(
            title=title,
            date_added=date_added,
            user=user,
            current_status=ticket_status,
            content=content,
            ticket_priority=ticket_priority,
            category=ticket_category,
            hours=hours,
            last_updated=date_added,
        )

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

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

        db.session.commit()

        return new_ticket
def subscribe_user(ticket, user):
    if not ticket.is_subscribed(user):
        # subscribe user to ticket
        # noinspection PyArgumentList
        subscribe = FlicketSubscription(user=user, ticket=ticket)
        db.session.add(subscribe)
        db.session.commit()
        return True

    return False
Example #4
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)
Example #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)
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)
Example #7
0
def subscribe_ticket(ticket_id=None):

    if ticket_id:

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

        if not ticket.is_subscribed(g.user):
            # subscribe user to ticket
            subscribe = FlicketSubscription(user=g.user, ticket=ticket)
            db.session.add(subscribe)
            db.session.commit()
            flash(gettext('You have been subscribed to this ticket.'))

        else:

            flash(gettext('Already subscribed to this ticket'))

        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))
Example #8
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)
Example #9
0
def subscribe_ticket(ticket_id=None):

    if ticket_id:

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

        if not ticket.is_subscribed(g.user):
            # subscribe user to ticket
            subscribe = FlicketSubscription(user=g.user, ticket=ticket)
            ticket.last_updated = datetime.datetime.now()
            add_action(ticket, 'subscribe', recipient=g.user)
            db.session.add(subscribe)
            db.session.commit()
            flash(gettext('You have been subscribed to this ticket.'),
                  category='success')

        else:

            flash(gettext('Already subscribed to this ticket'),
                  category='warning')

        return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id))
Example #10
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)
Example #11
0
    def create_ticket(
        title=None,
        user=None,
        content=None,
        requester=None,
        referee=None,
        requester_role=None,
        request_stage=None,
        procedure_stage=None,
        domain=None,
        institute=None,
        files=None,
        days=0,
    ):
        """
        :param title:
        :param user:
        :param content:
        :param requester:
        :param requester_role:
        :param domain:
        :param files:
        :param days:
        :return:
        """

        ticket_status = FlicketStatus.query.filter_by(status="Open").first()
        ticket_institute = FlicketInstitute.query.filter_by(
            id=int(institute)).first()
        ticket_domain = FlicketDomain.query.filter_by(id=int(domain)).first()
        requester_role = FlicketRequesterRole.query.filter_by(
            id=int(requester_role)).first()

        request_stage = FlicketRequestStage.query.filter_by(
            request_stage="New").first()
        procedure_stage = FlicketProcedureStage.query.filter_by(
            id=int(procedure_stage)).first()

        upload_attachments = UploadAttachment(files)
        if upload_attachments.are_attachments():
            upload_attachments.upload_files()

        # submit ticket data to database
        new_ticket = FlicketTicket(
            title=title,
            date_added=datetime.datetime.now(),
            user=user,
            current_status=ticket_status,
            content=content,
            institute=ticket_institute,
            requester=requester,
            referee=referee,
            requester_role=requester_role,
            request_stage=request_stage,
            procedure_stage=procedure_stage,
            domain=ticket_domain,
            days=days,
        )

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

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

        db.session.commit()

        return new_ticket
Example #12
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)
Example #13
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,
    )