Create API Key page

This commit is contained in:
Son NK 2019-11-28 22:14:55 +00:00
parent 6c4a173de5
commit 4f874eec43
7 changed files with 180 additions and 6 deletions

View File

@ -1 +1,10 @@
from .views import index, pricing, setting, custom_alias, billing, alias_log, unsubscribe
from .views import (
index,
pricing,
setting,
custom_alias,
billing,
alias_log,
unsubscribe,
api_key,
)

View File

@ -0,0 +1,98 @@
{% extends 'default.html' %}
{% block title %}
API Key
{% endblock %}
{% block head %}
{% endblock %}
{% block default_content %}
<div class="row">
<div class="col-md-8 offset-md-2">
<h1 class="h3"> API Key </h1>
<p>
The API Key is used inside the Chrome/Firefox extension.
Please copy and paste the API key below into the browser extension to get started.
</p>
<p class="text-danger">
Your API Keys are secret and should be treated as passwords.
</p>
{% for api_key in api_keys %}
<div class="card" style="max-width: 50rem">
<div class="card-body">
<h5 class="card-title">{{ api_key.name }}</h5>
<h6 class="card-subtitle mb-2 text-muted">
{% if api_key.last_used %}
Last used: {{ api_key.last_used | dt }} <br>
Used: {{ api_key.times }} times.
{% else %}
Never used
{% endif %}
</h6>
<textarea class="card-text w-100" id="apikey-{{ api_key.id }}" readonly>{{ api_key.code }}</textarea>
<br>
<div class="row">
<div class="col">
<button class="clipboard btn btn-primary" data-clipboard-action="copy"
data-clipboard-target="#apikey-{{ api_key.id }}">
Copy &nbsp; &nbsp; <i class="dropdown-icon fe fe-clipboard"></i>
</button>
</div>
<div class="col">
<form method="post">
<input type="hidden" name="form-name" value="delete">
<input type="hidden" name="api-key-id" value="{{ api_key.id }}">
<span class="card-link btn btn-link float-right delete-api-key">
Delete
</span>
</form>
</div>
</div>
</div>
</div>
{% endfor %}
<hr>
<form method="post">
{{ new_api_key_form.csrf_token }}
<input type="hidden" name="form-name" value="create">
<label class="form-label">Api Key Name</label>
<small>Name of the api key, e.g. where it will be used.</small>
{{ new_api_key_form.name(class="form-control", placeholder="Chrome, Firefox") }}
{{ render_field_errors(new_api_key_form.name) }}
<button class="btn btn-primary">Create</button>
</form>
</div>
</div>
{% endblock %}
{% block script %}
<script>
$(".delete-api-key").on("click", function (e) {
notie.confirm({
text: "If this api key is currently in use, you need to replace it with another api key, " +
" please confirm",
cancelCallback: () => {
// nothing to do
},
submitCallback: () => {
$(this).closest("form").submit();
}
});
});
</script>
{% endblock %}

View File

@ -1,7 +1,5 @@
{% extends 'default.html' %}
{% set active_page = "dashboard" %}
{% block title %}
Billing
{% endblock %}

View File

@ -1,7 +1,5 @@
{% extends 'default.html' %}
{% set active_page = "dashboard" %}
{% block title %}
Setting
{% endblock %}
@ -13,7 +11,7 @@
{{ form.csrf_token }}
<input type="hidden" name="form-name" value="update-profile">
<h3>Profile</h3>
<h1 class="h3">Profile</h1>
<div class="form-group">
<label class="form-label">Email</label>

View File

@ -0,0 +1,59 @@
from flask import render_template, request, redirect, url_for, flash
from flask_login import login_required, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, validators
from app.dashboard.base import dashboard_bp
from app.extensions import db
from app.models import ApiKey
class NewApiKeyForm(FlaskForm):
name = StringField("Name", validators=[validators.DataRequired()])
@dashboard_bp.route("/api_key", methods=["GET", "POST"])
@login_required
def api_key():
api_keys = ApiKey.query.filter(ApiKey.user_id == current_user.id).all()
new_api_key_form = NewApiKeyForm()
if request.method == "POST":
if request.form.get("form-name") == "delete":
api_key_id = request.form.get("api-key-id")
api_key = ApiKey.get(api_key_id)
if not api_key:
flash("Unknown error. Refresh the page", "warning")
return redirect(url_for("dashboard.api_key"))
elif api_key.user_id != current_user.id:
flash("You cannot delete this api key", "warning")
return redirect(url_for("dashboard.api_key"))
name = api_key.name
ApiKey.delete(api_key_id)
db.session.commit()
flash(f"API Key {name} has been deleted successfully", "success")
return redirect(url_for("dashboard.api_key"))
elif request.form.get("form-name") == "create":
if new_api_key_form.validate():
new_api_key = ApiKey.create(
name=new_api_key_form.name.data, user_id=current_user.id
)
db.session.commit()
flash(
f"New API Key {new_api_key.name} has been created successfully",
"success",
)
return redirect(url_for("dashboard.api_key"))
return redirect(url_for("dashboard.api_key"))
return render_template(
"dashboard/api_key.html", api_keys=api_keys, new_api_key_form=new_api_key_form
)

View File

@ -118,7 +118,15 @@ def fake_data():
)
db.session.commit()
ApiKey.create(user_id=user.id, name="Chrome")
GenEmail.create_new_gen_email(user_id=user.id)
GenEmail.create_new_gen_email(user_id=user.id)
GenEmail.create_new_gen_email(user_id=user.id)
GenEmail.create_new_gen_email(user_id=user.id, custom=True)
GenEmail.create_new_gen_email(user_id=user.id, custom=True)
GenEmail.create_new_gen_email(user_id=user.id, custom=True)
# Create a client
client1 = Client.create_new(name="Demo", user_id=user.id)

View File

@ -46,6 +46,10 @@
<i class="dropdown-icon fe fe-settings"></i> Settings
</a>
<a class="dropdown-item" href="{{ url_for('dashboard.api_key') }}">
<i class="dropdown-icon fe fe-chrome"></i> API Key
</a>
{% if current_user.is_premium() %}
<a class="dropdown-item" href="{{ url_for('dashboard.billing') }}">
<i class="dropdown-icon fe fe-dollar-sign"></i> Billing