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)
def create_ticket(title=None, user=None, content=None, priority=None, category=None, files=None): """ :param title: :param user: :param content: :param priority: :param category: :param files: :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_attachements(): 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, ticket_priority=ticket_priority, category=ticket_category) 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 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)
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.ticket.status_id = form.status.data files = request.files.getlist("file") upload_attachments = UploadAttachment(files) if upload_attachments.are_attachements(): 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 return render_template('flicket_editpost.html', title='Flicket - Edit Post', form=form)
def edit_ticket(ticket_id): form = EditTicketForm(ticket_id=ticket_id) ticket = FlicketTicket.query.filter_by(id=ticket_id).first() if not ticket: flash('Could not find ticket.', 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(ticket.current_status.status): return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket.id)) # check user is authorised to edit ticket. Currently, only admin or author can do this. not_authorised = True if ticket.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=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 ticket.modified_id: history_id = ticket.modified_id else: history_id = ticket.started_id if ticket.content != form.content.data: 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.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) 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() ticket.content = form.content.data ticket.title = form.title.data ticket.modified = g.user ticket.date_modified = datetime.datetime.now() ticket.current_status = ticket_status ticket.ticket_priority = ticket_priority ticket.category = ticket_category files = request.files.getlist("file") upload_attachments = UploadAttachment(files) if upload_attachments.are_attachements(): upload_attachments.upload_files() # add files to database. upload_attachments.populate_db(ticket) db.session.commit() flash('Ticket successfully edited.', category='success') return redirect(url_for('flicket_bp.ticket_view', ticket_id=ticket_id)) form.content.data = ticket.content form.priority.data = ticket.ticket_priority_id form.title.data = ticket.title form.category.data = ticket.category_id return render_template('flicket_edittopic.html', title='Flicket - Edit Ticket', form=form)
def edit_ticket(ticket=None, title=None, user=None, content=None, priority=None, category=None, files=None, form_uploads=None): """ :param ticket: :param title: :param user: :param content: :param priority: :param category: :param files: :param form_uploads: :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 files = files upload_attachments = UploadAttachment(files) if upload_attachments.are_attachements(): upload_attachments.upload_files() # add files to database. upload_attachments.populate_db(ticket) db.session.commit() return ticket.id