Skip to content

Commit 5b953d8

Browse files
committed
Force Show Peers date to display on same format as the date picker by wrapping
it in a dummy input field without border. Limit date picker UI and backend accepted date range to dates between 7 days from today and ~10 years in the future, with fallback to default 365 days if an invalid value is given. Highlight the fallback in output. The end date value is automatically interpreted as until midnight and the defaults are now read from mig/shared/defaults.py both for UI and backend.
1 parent d18dbc0 commit 5b953d8

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

mig/assets/css/V3/style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,3 +816,8 @@ var, sampl, code {
816816
border: 1px solid #ff9900;
817817
margin-bottom: 20px;
818818
}
819+
820+
/* Disable border on e.g. input elements if specifically requested */
821+
.noborder {
822+
border: 0;
823+
}

mig/shared/defaults.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@
208208
# Number of days before expire that auto extend attempts kick in
209209
# NOTE: must be lower than all X_auto_extend_days values to avoid hammering
210210
attempt_auto_extend_days = 10
211+
# Enforce peers expire value (End date) to default/min/max days in the future
212+
peers_expire_default_days = generic_valid_days
213+
peers_expire_min_days = 7
214+
peers_expire_max_days = 3652
211215

212216
# Strictly ordered list of account status values to enable use of filemarks
213217
# for caching account status using integer timestamps outside user DB.

mig/shared/functionality/peers.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# peers - manage external collaboration partners, etc.
7-
# Copyright (C) 2003-2021 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH
88
#
99
# This file is part of MiG.
1010
#
@@ -41,7 +41,8 @@
4141
from mig.shared.base import pretty_format_user, fill_distinguished_name, \
4242
client_id_dir, force_native_str_rec
4343
from mig.shared.defaults import csrf_field, peers_filename, \
44-
pending_peers_filename, peers_fields, peer_kinds, default_pager_entries
44+
pending_peers_filename, peers_fields, peer_kinds, default_pager_entries, \
45+
peers_expire_min_days, peers_expire_max_days
4546
from mig.shared.functional import validate_input_and_cert
4647
from mig.shared.handlers import get_csrf_limit, make_csrf_token
4748
from mig.shared.htmlgen import man_base_js, man_base_html, html_post_helper
@@ -217,6 +218,12 @@ def main(client_id, user_arguments_dict):
217218
'csrf_field': csrf_field, 'csrf_limit': csrf_limit,
218219
'target_op': target_op, 'csrf_token': csrf_token,
219220
'expire_help': expire_help,
221+
# NOTE: allow select expire N days or more from now
222+
'min_peers_expire': datetime.date.today() + \
223+
datetime.timedelta(days=peers_expire_min_days),
224+
# NOTE: allow up to N days in the future
225+
'max_peers_expire': datetime.date.today() + \
226+
datetime.timedelta(days=peers_expire_max_days),
220227
'csv_header': csv_sep.join([i for i in peers_fields])}
221228
form_prefix_html = '''
222229
<form class="save_peers save_general" method="%(form_method)s"
@@ -257,7 +264,8 @@ def main(client_id, user_arguments_dict):
257264
</label>
258265
<input class="form-control themed-select html-select fill-width"
259266
type="date" name="peers_expire" required pattern="[0-9/-]+"
260-
title="Access expiry date" />
267+
min="%(min_peers_expire)s" max="%(max_peers_expire)s"
268+
title="Access expiry date"/>
261269
</div>
262270
</div>
263271
'''

mig/shared/functionality/peersaction.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# peersaction - handle management of peers
7-
# Copyright (C) 2003-2023 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH
88
#
99
# This file is part of MiG.
1010
#
@@ -41,7 +41,8 @@
4141
from mig.shared.base import client_id_dir, fill_distinguished_name, \
4242
extract_field
4343
from mig.shared.defaults import peers_filename, peer_kinds, peers_fields, \
44-
keyword_auto, csrf_field
44+
keyword_auto, csrf_field, peers_expire_default_days, \
45+
peers_expire_min_days, peers_expire_max_days
4546
from mig.shared.functional import validate_input, REJECT_UNSET
4647
from mig.shared.handlers import safe_handler, get_csrf_limit
4748
from mig.shared.htmlgen import html_post_helper
@@ -51,7 +52,6 @@
5152
from mig.shared.url import urlencode
5253
from mig.shared.useradm import get_full_user_map
5354

54-
default_expire_days = 7
5555
peer_actions = ['import', 'add', 'remove', 'update', 'accept', 'reject']
5656

5757

@@ -155,17 +155,20 @@ def main(client_id, user_arguments_dict):
155155
expire = now
156156
else:
157157
expire = datetime.datetime.strptime(raw_expire, '%Y-%m-%d')
158-
if now > expire:
159-
raise ValueError("specified expire value is in the past!")
158+
if now + datetime.timedelta(days=peers_expire_min_days) > expire:
159+
raise ValueError("specified expire is in the past!")
160+
if now + datetime.timedelta(days=peers_expire_max_days) < expire:
161+
raise ValueError("specified expire is too far in the future!")
160162
except Exception as exc:
161-
logger.error("expire %r could not be parsed into a (future) date" %
163+
logger.error("expire %r could not be parsed into a valid date" %
162164
raw_expire)
163165
output_objects.append(
164-
{'object_type': 'text', 'text':
165-
'No valid expire provided - using default: %d days' %
166-
default_expire_days})
166+
{'object_type': 'warning', 'text':
167+
'End date must be %d - %d days from now - using default %d days' %
168+
(peers_expire_min_days, peers_expire_max_days,
169+
peers_expire_default_days)})
167170
expire = now
168-
expire += datetime.timedelta(days=default_expire_days)
171+
expire += datetime.timedelta(days=peers_expire_default_days)
169172
expire = expire.date().isoformat()
170173

171174
peers_path = os.path.join(configuration.user_settings, client_dir,

mig/shared/output.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,11 @@ def html_format(configuration, ret_val, ret_msg, out_obj):
15601560
single_peer['state'] = single_peer.get('state', '')
15611561
if not single_peer['state']:
15621562
single_peer['state'] = 'NA'
1563+
single_peer['expire'] = single_peer.get('expire', '')
1564+
if single_peer['expire']:
1565+
# Make a dummy input field to force consistent date format
1566+
single_peer['expire'] = "<input class='noborder' type=date " + \
1567+
"value='%(expire)s' readonly=readonly />" % single_peer
15631568
lines.append('''<tr>
15641569
<td>%(full_name)s</td><td>%(organization)s</td><td>%(email)s</td>
15651570
<td>%(country)s</td><td>%(state)s</td><td>%(kind)s</td><td>%(label)s</td><td>%(expire)s</td>

0 commit comments

Comments
 (0)