[damned-lies] Added links to downloadable files in diff view headers



commit 8e24ebe60045debb659788683795d20823f061bc
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Dec 27 14:15:36 2014 +0100

    Added links to downloadable files in diff view headers
    
    Fixes #741973.

 stats/models.py         |    1 +
 vertimus/models.py      |    4 ++++
 vertimus/tests/tests.py |    2 +-
 vertimus/views.py       |   42 +++++++++++++++++++++++++++++-------------
 4 files changed, 35 insertions(+), 14 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index a5efcd2..02b3c63 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -1185,6 +1185,7 @@ class PoFile(models.Model):
     def __unicode__(self):
         return "%s (%s/%s/%s)" % (self.path, self.translated, self.fuzzy, self.untranslated)
 
+    @property
     def url(self):
         return utils.url_join(settings.MEDIA_URL, settings.UPLOAD_DIR, os.path.basename(self.path))
 
diff --git a/vertimus/models.py b/vertimus/models.py
index f352541..68702f0 100644
--- a/vertimus/models.py
+++ b/vertimus/models.py
@@ -368,6 +368,10 @@ class ActionAbstract(models.Model):
     def description(self):
         return dict(ACTION_NAMES).get(self.name, None)
 
+    @property
+    def most_uptodate_file(self):
+        return self.merged_file if self.merged_file else self.file
+
     def get_filename(self):
         if self.file:
             return os.path.basename(self.file.name)
diff --git a/vertimus/tests/tests.py b/vertimus/tests/tests.py
index 16472a1..78a6418 100644
--- a/vertimus/tests/tests.py
+++ b/vertimus/tests/tests.py
@@ -262,7 +262,7 @@ class VertimusTest(TeamsAndRolesTests):
         action.apply_on(state, {'send_to_ml': action.send_mail_to_ml})
 
         self.assertEqual(action.file.url, '/media/upload/gedit-gnome-2-24-po-fr-%d.po' % state.id)
-        self.assertEqual(action.merged_file.url(), '/media/upload/gedit-gnome-2-24-po-fr-%d.merged.po' % 
state.id)
+        self.assertEqual(action.merged_file.url, '/media/upload/gedit-gnome-2-24-po-fr-%d.merged.po' % 
state.id)
         # Merged file will not be really produced as no pot file exists on the file system
         self.files_to_clean.append(action.file.path)
 
diff --git a/vertimus/views.py b/vertimus/views.py
index add6779..f749a5e 100644
--- a/vertimus/views.py
+++ b/vertimus/views.py
@@ -18,6 +18,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, see <http://www.gnu.org/licenses/>.
 
+import difflib
 import os
 
 from django.conf import settings
@@ -149,7 +150,6 @@ def vertimus(request, branch, domain, language, stats=None, level="0"):
 
 def vertimus_diff(request, action_id_1, action_id_2, level):
     """Show a diff between current action po file and previous file"""
-    import difflib
     if int(level) != 0:
         ActionReal = ActionArchived
     else:
@@ -157,7 +157,7 @@ def vertimus_diff(request, action_id_1, action_id_2, level):
     action_1 = get_object_or_404(ActionReal, pk=action_id_1)
     state = action_1.state_db
 
-    file_path_1 = action_1.merged_file and action_1.merged_file.path or action_1.file.path
+    file_path_1 = action_1.most_uptodate_file.path
     reduced = is_po_reduced(file_path_1)
     if not os.path.exists(file_path_1):
         raise Http404("File not found")
@@ -167,14 +167,21 @@ def vertimus_diff(request, action_id_1, action_id_2, level):
     except UnicodeDecodeError:
         return render(request, 'error.html',
                       {'error': _("Error: The file %s contains invalid characters.") % 
file_path_1.split('/')[-1]})
-    descr_1 = _("Uploaded file by %(name)s on %(date)s") % { 'name': action_1.person.name,
-                                                             'date': action_1.created }
+    descr_1 = _('<a href="%(url)s">Uploaded file</a> by %(name)s on %(date)s') % {
+        'url': action_1.most_uptodate_file.url,
+        'name': action_1.person.name,
+        'date': action_1.created
+    }
+
     if action_id_2 not in (None, "0"):
         # 1) id_2 specified in URL
         action_2 = get_object_or_404(ActionReal, pk=action_id_2)
-        file_path_2 = action_2.merged_file and action_2.merged_file.path or action_2.file.path
-        descr_2 = _("Uploaded file by %(name)s on %(date)s") % { 'name': action_2.person.name,
-                                                                 'date': action_2.created }
+        file_path_2 = action_2.most_uptodate_file.path
+        descr_2 = _('<a href="%(url)s">Uploaded file</a> by %(name)s on %(date)s') % {
+            'url': action_2.most_uptodate_file.url,
+            'name': action_2.person.name,
+            'date': action_2.created,
+        }
     else:
         action_2 = None
         if action_id_2 is None:
@@ -182,17 +189,26 @@ def vertimus_diff(request, action_id_1, action_id_2, level):
             action_2 = action_1.get_previous_action_with_po()
 
         if action_2:
-            file_path_2 = action_2.merged_file and action_2.merged_file.path or action_2.file.path
-            descr_2 = _("Uploaded file by %(name)s on %(date)s") % { 'name': action_2.person.name,
-                                                                     'date': action_2.created }
+            file_path_2 = action_2.most_uptodate_file.path
+            descr_2 = _('<a href="%(url)s">Uploaded file</a> by %(name)s on %(date)s') % {
+                'url': action_2.most_uptodate_file.url,
+                'name': action_2.person.name,
+                'date': action_2.created,
+            }
         else:
              # 3) Lastly, the file should be the more recently committed file (merged)
             try:
                 stats = Statistics.objects.get(branch=state.branch, domain=state.domain, 
language=state.language)
-                descr_2 = _("Latest committed file for %(lang)s") % {'lang': state.language.get_name()}
+                descr_2 = _('<a href="%(url)s">Latest committed file</a> for %(lang)s') % {
+                    'url': stats.po_url(),
+                    'lang': state.language.get_name(),
+                }
             except Statistics.DoesNotExist:
                 stats = get_object_or_404(Statistics, branch=state.branch, domain=state.domain, 
language=None)
-                descr_2 = _("Latest POT file")
+                descr_2 = '<a href="%(url)s">%(text)s</a>' % {
+                    'url': stats.pot_url(),
+                    'text': _("Latest POT file"),
+                }
             file_path_2 = stats.po_path(reduced=reduced)
     if not os.path.exists(file_path_2):
         raise Http404("File not found")
@@ -222,7 +238,7 @@ def latest_uploaded_po(request, module_name, branch_name, domain_name, locale_na
                                           file__endswith=".po").order_by('-created')[:1]
     if not latest_upload:
         raise Http404
-    return HttpResponseRedirect(latest_upload[0].merged_file.url())
+    return HttpResponseRedirect(latest_upload[0].merged_file.url)
 
 def activity_by_language(request, locale):
     language = get_object_or_404(Language, locale=locale)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]