Package coprs :: Module filters
[hide private]
[frames] | no frames]

Source Code for Module coprs.filters

  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):
18 if data: 19 data = re.sub("<.*?>", "", data) 20 data = re.sub("</a>", "", data) 21 return data 22 return None
23
24 @app.template_filter("date_from_secs") 25 -def date_from_secs(secs):
26 if secs: 27 return time.strftime("%Y-%m-%d %H:%M:%S %Z", time.gmtime(secs)) 28 29 return None
30
31 32 @app.template_filter("perm_type_from_num") 33 -def perm_type_from_num(num):
34 return helpers.PermissionEnum(num)
35
36 37 @app.template_filter("state_from_num") 38 -def state_from_num(num):
39 if num is None: 40 return "unknown" 41 return helpers.StatusEnum(num)
42
43 44 @app.template_filter("os_name_short") 45 -def os_name_short(os_name, os_version):
46 # TODO: make it models.MockChroot method or not? 47 if os_version: 48 if os_version == "rawhide": 49 return os_version 50 if os_name == "fedora": 51 return "fc.{0}".format(os_version) 52 elif os_name == "epel": 53 return "el{0}".format(os_version) 54 return os_name
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 # less than 2 minutes 108 return "1 minute" 109 elif secdiff < 7200: 110 # less than 2 hours 111 return str(secdiff // 60) + " minutes" 112 elif secdiff < 172800: 113 # less than 2 days 114 return str(secdiff // 3600) + " hours" 115 elif secdiff < 5184000: 116 # less than 2 months 117 return str(secdiff // 86400) + " days" 118 elif secdiff < 63072000: 119 # less than 2 years 120 return str(secdiff // 2592000) + " months" 121 else: 122 # more than 2 years 123 return str(secdiff // 31536000) + " years"
124
125 126 @app.template_filter("markdown") 127 -def markdown_filter(data):
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
137 138 @app.template_filter("pkg_name") 139 -def parse_package_name(pkg):
140 if pkg is not None: 141 return helpers.parse_package_name(os.path.basename(pkg)) 142 return pkg
143
144 145 @app.template_filter("basename") 146 -def parse_basename(pkg):
147 if pkg is not None: 148 return os.path.basename(pkg) 149 return pkg
150
151 152 @app.template_filter("build_state_description") 153 -def build_state_decoration(state):
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
168 169 @app.template_filter("build_source_description") 170 -def build_source_description(state):
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
183 184 @app.template_filter("fix_url_https_backend") 185 -def fix_url_https_backend(url):
186 return helpers.fix_protocol_for_backend(url)
187
188 189 @app.template_filter("fix_url_https_frontend") 190 -def fix_url_https_frontend(url):
191 return helpers.fix_protocol_for_frontend(url)
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