def create_ticket_reply(new_ticket):
    new_reply = FlicketPost(
        ticket=new_ticket,
        content=get_random_sentence(),
        user=get_random_user(),
        date_added=datetime.datetime.now()
    )

    db.session.add(new_reply)
def create_ticket_reply(new_ticket):
    t = Text()

    new_reply = FlicketPost(ticket=new_ticket,
                            content=t.text(randint(3, 15)),
                            user=get_random_user(),
                            date_added=datetime.datetime.now())

    new_reply.user.total_posts += 1

    db.session.add(new_reply)
Exemplo n.º 3
0
def get_posts(page=1, ticket_id=None):
    posts = FlicketPost.query.filter_by(ticket_id=ticket_id)
    per_page = min(
        request.args.get("per_page", app.config["posts_per_page"], type=int),
        100)
    data = FlicketPost.to_collection_dict(posts,
                                          page,
                                          per_page,
                                          "bp_api.get_posts",
                                          ticket_id=ticket_id)
    return jsonify(data)
Exemplo n.º 4
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.º 5
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.º 6
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,
    )