Skip to content

Commit

Permalink
updated user apikey form
Browse files Browse the repository at this point in the history
  • Loading branch information
jirivrany committed Apr 2, 2024
1 parent 5f58318 commit 382a83e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
18 changes: 17 additions & 1 deletion flowapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ class MultiFormatDateTimeLocalField(DateTimeField):

def __init__(self, *args, **kwargs):
kwargs.setdefault("format", "%Y-%m-%dT%H:%M")
self.unlimited = kwargs.pop('unlimited', False)
self.pref_format = None
super().__init__(*args, **kwargs)

def process_formdata(self, valuelist):
if not valuelist:
return
return None
# with unlimited field we do not need to parse the empty value
if self.unlimited and len(valuelist) == 1 and len(valuelist[0]) == 0:
self.data = None
return None

date_str = " ".join((str(val) for val in valuelist))
result, pref_format = parse_api_time(date_str)
Expand Down Expand Up @@ -119,6 +124,17 @@ class ApiKeyForm(FlaskForm):
validators=[DataRequired(), IPAddress(message="provide valid IP address")],
)

comment = TextAreaField(
"Your comment for this key", validators=[Optional(), Length(max=255)]
)

expires = MultiFormatDateTimeLocalField(
"Key expiration. Leave blank for non expring key (not-recomended).",
format=FORM_TIME_PATTERN, validators=[Optional()], unlimited=True
)

readonly = BooleanField("Read only key", default=False)

key = HiddenField("GeneratedKey")


Expand Down
29 changes: 19 additions & 10 deletions flowapp/templates/forms/api_key.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
{% extends 'layouts/default.html' %}
{% from 'forms/macros.html' import render_field %}
{% from 'forms/macros.html' import render_field, render_checkbox_field %}
{% block title %}Add New Machine with ApiKey{% endblock %}
{% block content %}
<h2>Add new ApiKey for your machine</h2>

<div class="row">

<div class="col-sm-12">
<h6>ApiKey: {{ generated_key }}</h6>
</div>

<form action="{{ action_url }}" method="POST">
{{ form.hidden_tag() if form.hidden_tag }}
<div class="row">
<div class="col-sm-12">
<div class="col-sm-5">
{{ render_field(form.machine) }}
</div>
</div>

<div class="row">
<div class="col-sm-4">
ApiKey for this machine:
<div class="col-sm-2">
{{ render_checkbox_field(form.readonly) }}
</div>
<div class="col-sm-8">
{{ generated_key }}
<div class="col-sm-5">
{{ render_field(form.expires) }}
</div>
</div>

</div>

<div class="row">
<div class="col-sm-10">
{{ render_field(form.comment) }}
</div>

<div class="row">
<div class="col-sm-10">
Expand Down
2 changes: 1 addition & 1 deletion flowapp/templates/forms/macros.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{# Renders field for bootstrap 3 standards.
{# Renders field for bootstrap 5 standards.

Params:
field - WTForm field
Expand Down
26 changes: 22 additions & 4 deletions flowapp/templates/pages/api_key.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ <h1>Your machines and ApiKeys</h1>
<tr>
<th>Machine address</th>
<th>ApiKey</th>
<th>Expires</th>
<th>Read only</th>
<th>Action</th>
</tr>
{% for row in keys %}
Expand All @@ -17,10 +19,26 @@ <h1>Your machines and ApiKeys</h1>
{{ row.key }}
</td>
<td>
<a class="btn btn-danger btn-sm" href="{{ url_for('api_keys.delete', key_id=row.id) }}" role="button">
<i class="bi bi-x-lg"></i>
</a>
</td>
{{ row.expires }}
</td>
<td>
{% if row.readonly %}
<button type="button" class="btn btn-success btn-sm" title="Read Only">
<i class="bi bi-check-lg"></i>
</button>

{% endif %}
</td>
<td>
<a class="btn btn-danger btn-sm" href="{{ url_for('api_keys.delete', key_id=row.id) }}" role="button">
<i class="bi bi-x-lg"></i>
</a>
{% if row.comment %}
<button type="button" class="btn btn-info btn-sm" data-bs-toggle="tooltip" data-bs-placement="top" title="{{ row.comment }}">
<i class="bi bi-chat-left-text-fill"></i>
</button>
{% endif %}
</td>
</tr>
{% endfor %}
</table>
Expand Down
9 changes: 8 additions & 1 deletion flowapp/views/api_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ def add():
form = ApiKeyForm(request.form, key=generated)

if request.method == "POST" and form.validate():
print("Form validated")
# import ipdb; ipdb.set_trace()
model = ApiKey(
machine=form.machine.data, key=form.key.data, user_id=session["user_id"]
machine=form.machine.data,
key=form.key.data,
expires=form.expires.data,
readonly=form.readonly.data,
comment=form.comment.data,
user_id=session["user_id"]
)

db.session.add(model)
Expand Down

0 comments on commit 382a83e

Please sign in to comment.