1 import datetime
2 from six.moves.urllib.parse import urlparse
3 import pytz
4 import time
5 import markdown
6
7 import os
8 import re
9
10 from flask import Markup, url_for
11
12 from coprs import app
13 from coprs import helpers
14
15
16 @app.template_filter("remove_anchor")
17 -def remove_anchor(data):
23
26 if secs:
27 return time.strftime("%Y-%m-%d %H:%M:%S %Z", time.gmtime(secs))
28
29 return None
30
35
42
43
44 @app.template_filter("os_name_short")
45 -def os_name_short(os_name, os_version):
55
56
57 @app.template_filter('localized_time')
58 -def localized_time(time_in, timezone):
59 """ return time shifted into timezone (and printed in ISO format)
60
61 Input is in EPOCH (seconds since epoch).
62 """
63 if not time_in:
64 return "Not yet"
65 format_tz = "%Y-%m-%d %H:%M %Z"
66 utc_tz = pytz.timezone('UTC')
67 if timezone:
68 user_tz = pytz.timezone(timezone)
69 else:
70 user_tz = utc_tz
71 dt_aware = datetime.datetime.fromtimestamp(time_in).replace(tzinfo=utc_tz)
72 dt_my_tz = dt_aware.astimezone(user_tz)
73 return dt_my_tz.strftime(format_tz)
74
75
76 @app.template_filter('timestamp_diff')
77 -def timestamp_diff(time_in, until=None):
78 """ returns string with difference between two timestamps
79
80 Input is in EPOCH (seconds since epoch).
81 """
82 if time_in is None:
83 return " - "
84 if until is not None:
85 now = datetime.datetime.fromtimestamp(until)
86 else:
87 now = datetime.datetime.now()
88 diff = now - datetime.datetime.fromtimestamp(time_in)
89 return str(int(diff.total_seconds()))
90
91
92 @app.template_filter('time_ago')
93 -def time_ago(time_in, until=None):
94 """ returns string saying how long ago the time on input was
95
96 Input is in EPOCH (seconds since epoch).
97 """
98 if time_in is None:
99 return " - "
100 if until is not None:
101 now = datetime.datetime.fromtimestamp(until)
102 else:
103 now = datetime.datetime.now()
104 diff = now - datetime.datetime.fromtimestamp(time_in)
105 secdiff = int(diff.total_seconds())
106 if secdiff < 120:
107
108 return "1 minute"
109 elif secdiff < 7200:
110
111 return str(secdiff // 60) + " minutes"
112 elif secdiff < 172800:
113
114 return str(secdiff // 3600) + " hours"
115 elif secdiff < 5184000:
116
117 return str(secdiff // 86400) + " days"
118 elif secdiff < 63072000:
119
120 return str(secdiff // 2592000) + " months"
121 else:
122
123 return str(secdiff // 31536000) + " years"
124
128 if not data:
129 return ''
130
131 md = markdown.Markdown(
132 safe_mode="replace",
133 html_replacement_text="--RAW HTML NOT ALLOWED--")
134
135 return Markup(md.convert(data))
136
143
147 if pkg is not None:
148 return os.path.basename(pkg)
149 return pkg
150
154
155 description_map = {
156 "failed": "Build failed. See logs for more details.",
157 "succeeded": "Successfully built.",
158 "canceled": "The build has been cancelled manually.",
159 "running": "Build in progress.",
160 "pending": "Your build is waiting for a builder.",
161 "skipped": "This package has already been built previously.",
162 "starting": "Trying to acquire and configure builder for task.",
163 "importing": "The SRPM is being imported into Dist Git."
164 }
165
166 return description_map.get(state, "")
167
171 description_map = {
172 "unset": "No default source",
173 "srpm_link": "External link with SRPM",
174 "srpm_upload": "SRPM file upload",
175 "git_and_tito": "Tito build from a Git repository",
176 "mock_scm": "Mock build from a SCM repository",
177 "pypi": "Build from PyPI",
178 "rubygems": "Build from RubyGems",
179 }
180
181 return description_map.get(state, "")
182
187
192
193 @app.template_filter("repo_url")
194 -def repo_url(url):
195 """ render copr://<user>/prj to be rendered as copr projects pages """
196 parsed = urlparse(url)
197 if parsed.scheme == "copr":
198 user = parsed.netloc
199 prj = parsed.path.split("/")[1]
200 url = url_for("coprs_ns.copr_detail", username=user, coprname=prj)
201
202 return helpers.fix_protocol_for_frontend(url)
203
204 @app.template_filter("mailto")
205 -def mailto(url):
206 return url if urlparse(url).scheme else "mailto:{}".format(url)
207