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
46 if num is None:
47 return "unknown"
48 return ["pending", "succeeded", "failed"][num]
49
50
51 @app.template_filter("os_name_short")
52 -def os_name_short(os_name, os_version):
62
63
64 @app.template_filter('localized_time')
65 -def localized_time(time_in, timezone):
66 """ return time shifted into timezone (and printed in ISO format)
67
68 Input is in EPOCH (seconds since epoch).
69 """
70 if not time_in:
71 return "Not yet"
72 format_tz = "%Y-%m-%d %H:%M %Z"
73 utc_tz = pytz.timezone('UTC')
74 if timezone:
75 user_tz = pytz.timezone(timezone)
76 else:
77 user_tz = utc_tz
78 dt_aware = datetime.datetime.fromtimestamp(time_in).replace(tzinfo=utc_tz)
79 dt_my_tz = dt_aware.astimezone(user_tz)
80 return dt_my_tz.strftime(format_tz)
81
82
83 @app.template_filter('timestamp_diff')
84 -def timestamp_diff(time_in, until=None):
85 """ returns string with difference between two timestamps
86
87 Input is in EPOCH (seconds since epoch).
88 """
89 if time_in is None:
90 return " - "
91 if until is not None:
92 now = datetime.datetime.fromtimestamp(until)
93 else:
94 now = datetime.datetime.now()
95 diff = now - datetime.datetime.fromtimestamp(time_in)
96 return str(int(diff.total_seconds()))
97
98
99 @app.template_filter('time_ago')
100 -def time_ago(time_in, until=None):
101 """ returns string saying how long ago the time on input was
102
103 Input is in EPOCH (seconds since epoch).
104 """
105 if time_in is None:
106 return " - "
107 if until is not None:
108 now = datetime.datetime.fromtimestamp(until)
109 else:
110 now = datetime.datetime.now()
111 diff = now - datetime.datetime.fromtimestamp(time_in)
112 secdiff = int(diff.total_seconds())
113 if secdiff < 120:
114
115 return "1 minute"
116 elif secdiff < 7200:
117
118 return str(secdiff // 60) + " minutes"
119 elif secdiff < 172800:
120
121 return str(secdiff // 3600) + " hours"
122 elif secdiff < 5184000:
123
124 return str(secdiff // 86400) + " days"
125 elif secdiff < 63072000:
126
127 return str(secdiff // 2592000) + " months"
128 else:
129
130 return str(secdiff // 31536000) + " years"
131
135 if not data:
136 return ''
137
138 md = markdown.Markdown(
139 safe_mode="replace",
140 html_replacement_text="--RAW HTML NOT ALLOWED--")
141
142 return Markup(md.convert(data))
143
150
154 if pkg is not None:
155 return os.path.basename(pkg)
156 return pkg
157
161
162 description_map = {
163 "failed": "Build failed. See logs for more details.",
164 "succeeded": "Successfully built.",
165 "canceled": "The build has been cancelled manually.",
166 "running": "Build in progress.",
167 "pending": "Your build is waiting for a builder.",
168 "skipped": "This package has already been built previously.",
169 "starting": "Trying to acquire and configure builder for task.",
170 "importing": "The SRPM is being imported into Dist Git."
171 }
172
173 return description_map.get(state, "")
174
178 description_map = {
179 "unset": "No default source",
180 "srpm_link": "External link with SRPM",
181 "srpm_upload": "SRPM file upload",
182 "git_and_tito": "Tito build from a Git repository",
183 "mock_scm": "Mock build from a SCM repository",
184 "pypi": "Build from PyPI",
185 "rubygems": "Build from RubyGems",
186 }
187
188 return description_map.get(state, "")
189
194
199
200 @app.template_filter("repo_url")
201 -def repo_url(url):
202 """ render copr://<user>/prj to be rendered as copr projects pages """
203 parsed = urlparse(url)
204 if parsed.scheme == "copr":
205 user = parsed.netloc
206 prj = parsed.path.split("/")[1]
207 url = url_for("coprs_ns.copr_detail", username=user, coprname=prj)
208
209 return helpers.fix_protocol_for_frontend(url)
210
211 @app.template_filter("mailto")
212 -def mailto(url):
213 return url if urlparse(url).scheme else "mailto:{}".format(url)
214