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 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
def edit_ticket( ticket=None, 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, form_uploads=None, days=None, ): """ :param ticket: :param title: :param user: :param content: :param domain: :param files: :param form_uploads: :param days: :return: """ # before we make any changes store the original post content in the history table if it has changed. if ticket.modified_id: history_id = ticket.modified_id else: history_id = ticket.started_id if ticket.content != content: history = FlicketHistory( original_content=ticket.content, topic=ticket, date_modified=datetime.datetime.now(), user_id=history_id, ) db.session.add(history) # loop through the selected uploads for deletion. if len(form_uploads) > 0: for i in form_uploads: # get the upload document information from the database. query = FlicketUploads.query.filter_by(id=i).first() # define the full uploaded filename the_file = os.path.join(app.config["ticket_upload_folder"], query.filename) if os.path.isfile(the_file): # delete the file from the folder os.remove(the_file) db.session.delete(query) requester_role = FlicketRequesterRole.query.filter_by( id=int(requester_role)).first() request_stage = FlicketRequestStage.query.filter_by( id=int(request_stage)).first() procedure_stage = FlicketProcedureStage.query.filter_by( id=int(procedure_stage)).first() ticket_domain = FlicketDomain.query.filter_by(id=int(domain)).first() ticket_institute = FlicketInstitute.query.filter_by( id=int(institute)).first() ticket.content = content ticket.requester = requester ticket.referee = referee ticket.title = title ticket.modified = user ticket.date_modified = datetime.datetime.now() ticket.requester_role = requester_role ticket.request_stage = request_stage ticket.procedure_stage = procedure_stage ticket.domain = ticket_domain ticket.institute = ticket_institute ticket.days = days # set status to Open ticket_status = FlicketStatus.query.filter_by(status="Open").first() ticket.current_status = ticket_status files = files upload_attachments = UploadAttachment(files) if upload_attachments.are_attachments(): upload_attachments.upload_files() # add files to database. upload_attachments.populate_db(ticket) db.session.commit() return ticket.id
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)
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, )
def edit_ticket(ticket=None, title=None, user=None, content=None, priority=None, category=None, files=None, form_uploads=None, hours=None): """ :param ticket: :param title: :param user: :param content: :param priority: :param category: :param files: :param form_uploads: :param hours: :return: """ # before we make any changes store the original post content in the history table if it has changed. if ticket.modified_id: history_id = ticket.modified_id else: history_id = ticket.started_id if ticket.content != content: history = FlicketHistory(original_content=ticket.content, topic=ticket, date_modified=datetime.datetime.now(), user_id=history_id) db.session.add(history) # loop through the selected uploads for deletion. if len(form_uploads) > 0: for i in form_uploads: # get the upload document information from the database. query = FlicketUploads.query.filter_by(id=i).first() # define the full uploaded filename the_file = os.path.join(app.config['ticket_upload_folder'], query.filename) if os.path.isfile(the_file): # delete the file from the folder os.remove(the_file) db.session.delete(query) ticket_priority = FlicketPriority.query.filter_by( id=int(priority)).first() ticket_category = FlicketCategory.query.filter_by( id=int(category)).first() ticket.content = content ticket.title = title ticket.modified = user ticket.date_modified = datetime.datetime.now() ticket.ticket_priority = ticket_priority ticket.category = ticket_category ticket.hours = hours files = files upload_attachments = UploadAttachment(files) if upload_attachments.are_attachments(): upload_attachments.upload_files() # add files to database. upload_attachments.populate_db(ticket) db.session.commit() return ticket.id
def edit_post(post_id): form = EditReplyForm(post_id=post_id) post = FlicketPost.query.filter_by(id=post_id).first() if not post: flash("Could not find post.", category="warning") return redirect(url_for("flicket_bp.flicket_main")) # check to see if topic is closed. ticket can't be edited once it's closed. if is_ticket_closed(post.ticket.current_status.status): return redirect(url_for("flicket_bp.ticket_view", ticket_id=post.ticket.id)) # check user is authorised to edit post. Only author or admin can do this. not_authorised = True if post.user == g.user or g.user.is_admin: not_authorised = False if not_authorised: flash("You are not authorised to edit this ticket.", category="warning") return redirect(url_for("flicket_bp.ticket_view", ticket_id=post.ticket_id)) if form.validate_on_submit(): # before we make any changes store the original post content in the history table if it has changed. if post.modified_id: history_id = post.modified_id else: history_id = post.user_id if post.content != form.content.data: history = FlicketHistory( original_content=post.content, post=post, date_modified=datetime.datetime.now(), user_id=history_id, ) db.session.add(history) # loop through the selected uploads for deletion. if len(form.uploads.data) > 0: for i in form.uploads.data: # get the upload document information from the database. query = FlicketUploads.query.filter_by(id=i).first() # define the full uploaded filename the_file = os.path.join( app.config["ticket_upload_folder"], query.filename ) if os.path.isfile(the_file): # delete the file from the folder os.remove(the_file) db.session.delete(query) post.content = form.content.data post.modified = g.user post.date_modified = datetime.datetime.now() post.days = form.days.data if post.ticket.request_stage_id != form.request_stage.data: request_stage = FlicketRequestStage.query.get(form.request_stage.data) post.ticket.request_stage = request_stage add_action( post.ticket, "request_stage", data={ "request_stage_id": request_stage.id, "request_stage": request_stage.request_stage, }, ) if post.ticket.procedure_stage_id != form.procedure_stage.data: procedure_stage = FlicketProcedureStage.query.get(form.procedure_stage.data) post.ticket.procedure_stage = procedure_stage add_action( post.ticket, "procedure_stage", data={ "procedure_stage_id": procedure_stage.id, "procedure_stage": procedure_stage.procedure_stage, }, ) files = request.files.getlist("file") upload_attachments = UploadAttachment(files) if upload_attachments.are_attachments(): upload_attachments.upload_files() # add files to database. upload_attachments.populate_db(post) db.session.commit() flash("Post successfully edited.", category="success") return redirect(url_for("flicket_bp.ticket_view", ticket_id=post.ticket_id)) form.content.data = post.content form.days.data = post.days form.request_stage.data = post.ticket.request_stage_id form.procedure_stage.data = post.ticket.procedure_stage_id return render_template("flicket_editpost.html", title="Edit Post", form=form)
def edit_post(post_id): form = EditReplyForm(post_id=post_id) post = FlicketPost.query.filter_by(id=post_id).first() if not post: flash('Could not find post.', category='warning') return redirect(url_for('flicket_bp.flicket_main')) # check to see if topic is closed. ticket can't be edited once it's closed. if is_ticket_closed(post.ticket.current_status.status): return redirect( url_for('flicket_bp.ticket_view', ticket_id=post.ticket.id)) # check user is authorised to edit post. Only author or admin can do this. not_authorised = True if post.user == g.user or g.user.is_admin: not_authorised = False if not_authorised: flash('You are not authorised to edit this ticket.', category='warning') return redirect( url_for('flicket_bp.ticket_view', ticket_id=post.ticket_id)) if form.validate_on_submit(): # stop closing of ticket via edit. if form.status.data == 2: flash('You can not edit and close ticket.') # before we make any changes store the original post content in the history table if it has changed. if post.modified_id: history_id = post.modified_id else: history_id = post.user_id if post.content != form.content.data: history = FlicketHistory(original_content=post.content, post=post, date_modified=datetime.datetime.now(), user_id=history_id) db.session.add(history) # loop through the selected uploads for deletion. if len(form.uploads.data) > 0: for i in form.uploads.data: # get the upload document information from the database. query = FlicketUploads.query.filter_by(id=i).first() # define the full uploaded filename the_file = os.path.join(app.config['ticket_upload_folder'], query.filename) if os.path.isfile(the_file): # delete the file from the folder os.remove(the_file) db.session.delete(query) post.content = form.content.data post.modified = g.user post.date_modified = datetime.datetime.now() post.hours = form.hours.data if post.ticket.status_id != form.status.data: status = FlicketStatus.query.get(form.status.data) post.ticket.current_status = status add_action(post.ticket, 'status', data={ 'status_id': status.id, 'status': status.status }) if post.ticket.ticket_priority_id != form.priority.data: priority = FlicketPriority.query.get(form.priority.data) post.ticket.ticket_priority = priority add_action(post.ticket, 'priority', data={ 'priority_id': priority.id, 'priority': priority.priority }) files = request.files.getlist("file") upload_attachments = UploadAttachment(files) if upload_attachments.are_attachments(): upload_attachments.upload_files() # add files to database. upload_attachments.populate_db(post) db.session.commit() flash('Post successfully edited.', category='success') return redirect( url_for('flicket_bp.ticket_view', ticket_id=post.ticket_id)) form.content.data = post.content form.hours.data = post.hours form.status.data = post.ticket.status_id form.priority.data = post.ticket.ticket_priority_id return render_template('flicket_editpost.html', title='Edit Post', form=form)