From 784dde8c255335faae591ef9b0dce235de17403a Mon Sep 17 00:00:00 2001 From: Mark Reynolds Date: Dec 06 2016 09:03:31 +0000 Subject: Issue 1597- RFE - add a file attachment section to Issues page It is common for many files/patches/images, etc, to be attached to a single Issue. Having a section listing all the attachments is a nice convenience, especially for issues with many comments. The attachment section will list all the attachments(as links). The attachment row will include a link to the file, the date it was added, and a link to the Comment that the file was attached to. If there are no attachments then do not display the "attachments section" https://pagure.io/pagure/issue/1597 --- diff --git a/pagure/lib/model.py b/pagure/lib/model.py index 3c79a56..f7cfbac 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -15,6 +15,7 @@ import datetime import logging import json import operator +import re import sqlalchemy as sa @@ -713,6 +714,49 @@ class Issue(BASE): ) @property + def attachments(self): + ''' Return a list of attachment tuples: (LINK, DATE, COMMENT_ID) ''' + + def extract_md_link(text): + pattern = re.compile('^\[\!(.*)\]') + try: + result = pattern.search(text).group(1) + if result is None: + # No match, return the original string + result = text + except: + # Search failed, return the original string + result = text + return result + + attachments = [] + if self.content: + # Check the initial issue description for attachments + lines = self.content.split('\n') + for line in lines: + if line and line != "" and line.startswith("[!["): + link = extract_md_link(line) + attachments.append( + (link, self.date_created.strftime('%Y-%m-%d %H:%M:%S'), + "")) + if self.comments: + # Check the comments for attachments + for comment in self.comments: + if comment.id == 0: + comment_text = comment.content + else: + comment_text = comment.comment + lines = comment_text.split('\n') + for line in lines: + if line and line != "" and line.startswith("[!["): + link = extract_md_link(line) + attachments.append( + (link, + comment.date_created.strftime('%Y-%m-%d %H:%M:%S'), + str(comment.id))) + return attachments + + @property def isa(self): ''' A string to allow finding out that this is an issue. ''' return 'issue' diff --git a/pagure/templates/_formhelper.html b/pagure/templates/_formhelper.html index 0b5cf8a..09509bf 100644 --- a/pagure/templates/_formhelper.html +++ b/pagure/templates/_formhelper.html @@ -207,3 +207,30 @@ {% endmacro %} + +{% macro show_attachments(attachments) %} +
+
+ Attachments +
+ +
+
+
+ + {% for attachment in attachments %} + {% if attachment[2] == "" %} + {% set attachment_md = "**" + attachment[0] + "**`` ``*(" + + attachment[1] + "`` - ``From Issue description)*" %} + {% else %} + {% set attachment_md = "**" + attachment[0] + "**`` ``*(" + + attachment[1] + "`` - ``[Comment](#comment-" + attachment[2] + "))*" %} + {% endif %} + {{ attachment_md | markdown | noJS | safe }} + {% endfor %} + +
+
+
+
+{% endmacro %} diff --git a/pagure/templates/issue.html b/pagure/templates/issue.html index 5417da9..0e8ac65 100644 --- a/pagure/templates/issue.html +++ b/pagure/templates/issue.html @@ -1,7 +1,7 @@ {% extends "repo_master.html" %} {% from "_formhelper.html" import render_field, render_bootstrap_field, - show_comment, show_initial_comment %} + show_comment, show_initial_comment, show_attachments %} {% block title %}Issue #{{ issueid }}: {{issue.title | noJS(ignore="img") | safe }} - {{ repo.name }}{% endblock %} {% set tag = "home"%} @@ -47,6 +47,13 @@ {{ show_initial_comment(issue, username, repo,issueid, form) }} + {% if attachments %} +
+ {{ show_attachments(attachments) }} +
+
+ {% endif %} +
{% if issue.comments %} {% for comment in issue.comments %} diff --git a/pagure/ui/issues.py b/pagure/ui/issues.py index baa4e1a..6b104e9 100644 --- a/pagure/ui/issues.py +++ b/pagure/ui/issues.py @@ -889,6 +889,7 @@ def view_issue(repo, issueid, username=None, namespace=None): form=form, knowns_keys=knowns_keys, subscribers=pagure.lib.get_watch_list(SESSION, issue), + attachments=issue.attachments, )