Improved admin page

This commit is contained in:
Harvey Tindall 2020-05-05 11:37:13 +01:00
parent a2e7ff4def
commit 81a80afdf2
4 changed files with 41 additions and 10 deletions

View File

@ -40,11 +40,14 @@ function addItem(invite) {
codeCopy.onclick = function(){toClipboard(inviteCode)}; codeCopy.onclick = function(){toClipboard(inviteCode)};
codeCopy.classList.add('fa', 'fa-clipboard'); codeCopy.classList.add('fa', 'fa-clipboard');
listCode.appendChild(codeCopy); listCode.appendChild(codeCopy);
console.log(invite[3]);
if (typeof(invite[3]) != 'undefined') { if (typeof(invite[3]) != 'undefined') {
var sentTo = document.createElement('span'); var sentTo = document.createElement('span');
sentTo.setAttribute('style', 'color: grey; margin-left: 2%; font-style: italic; font-size: 75%;'); sentTo.setAttribute('style', 'color: grey; margin-left: 2%; font-style: italic; font-size: 75%;');
sentTo.appendChild(document.createTextNode('Sent to ' + invite[3])); if (invite[3].includes('Failed to send to')) {
sentTo.appendChild(document.createTextNode(invite[3]));
} else {
sentTo.appendChild(document.createTextNode('Sent to ' + invite[3]));
}
listCode.appendChild(sentTo); listCode.appendChild(sentTo);
}; };
var listDelete = document.createElement('button'); var listDelete = document.createElement('button');
@ -159,7 +162,13 @@ function toClipboard(str) {
document.getSelection().addRange(selected); document.getSelection().addRange(selected);
} }
}; };
$("form#inviteForm").submit(function() { $("form#inviteForm").submit(function() {
var button = document.getElementById('generateSubmit');
button.disabled = true;
button.innerHTML =
'<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true" style="margin-right: 0.5rem;"></span>' +
'Loading...';
var send_object = $("form#inviteForm").serializeObject(); var send_object = $("form#inviteForm").serializeObject();
if (document.getElementById('send_to_address_enabled').checked) { if (document.getElementById('send_to_address_enabled').checked) {
send_object['email'] = document.getElementById('send_to_address').value; send_object['email'] = document.getElementById('send_to_address').value;
@ -175,7 +184,11 @@ $("form#inviteForm").submit(function() {
beforeSend : function (xhr) { beforeSend : function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":")); xhr.setRequestHeader("Authorization", "Basic " + btoa(window.token + ":"));
}, },
success: function() { generateInvites(); }, success: function() {
button.textContent = 'Generate';
button.disabled = false;
generateInvites();
},
}); });
return false; return false;
@ -183,6 +196,13 @@ $("form#inviteForm").submit(function() {
$("form#loginForm").submit(function() { $("form#loginForm").submit(function() {
window.token = ""; window.token = "";
var details = $("form#loginForm").serializeObject(); var details = $("form#loginForm").serializeObject();
var errorArea = document.getElementById('loginErrorArea');
errorArea.textContent = '';
var button = document.getElementById('loginSubmit');
button.disabled = true;
button.innerHTML =
'<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true" style="margin-right: 0.5rem;"></span>' +
'Loading...';
$.ajax('/getToken', { $.ajax('/getToken', {
type : 'GET', type : 'GET',
dataType : 'json', dataType : 'json',
@ -196,12 +216,13 @@ $("form#loginForm").submit(function() {
data: { get_param: 'value' }, data: { get_param: 'value' },
complete: function(data) { complete: function(data) {
if (data['status'] == 401) { if (data['status'] == 401) {
var formBody = document.getElementById('formBody'); button.disabled = false;
button.textContent = 'Login';
var wrongPassword = document.createElement('div'); var wrongPassword = document.createElement('div');
wrongPassword.classList.add('alert', 'alert-danger'); wrongPassword.classList.add('alert', 'alert-danger');
wrongPassword.setAttribute('role', 'alert'); wrongPassword.setAttribute('role', 'alert');
wrongPassword.appendChild(document.createTextNode('Incorrect username or password.')); wrongPassword.appendChild(document.createTextNode('Incorrect username or password.'));
formBody.appendChild(wrongPassword); errorArea.appendChild(wrongPassword);
} else { } else {
window.token = JSON.parse(data['responseText'])['token']; window.token = JSON.parse(data['responseText'])['token'];
generateInvites(); generateInvites();
@ -219,6 +240,10 @@ $("form#loginForm").submit(function() {
return false; return false;
}); });
document.getElementById('openUsers').onclick = function () { document.getElementById('openUsers').onclick = function () {
this.disabled = true;
this.innerHTML =
'<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true" style="margin-right: 0.5rem;"></span>' +
'Loading...';
$.ajax('/getUsers', { $.ajax('/getUsers', {
type : 'GET', type : 'GET',
dataType : 'json', dataType : 'json',
@ -282,7 +307,6 @@ document.getElementById('openUsers').onclick = function () {
send[name] = address send[name] = address
}; };
}; };
console.log(send);
send = JSON.stringify(send); send = JSON.stringify(send);
$.ajax('/modifyUsers', { $.ajax('/modifyUsers', {
data : send, data : send,
@ -304,10 +328,13 @@ document.getElementById('openUsers').onclick = function () {
entry.appendChild(editButton); entry.appendChild(editButton);
list.appendChild(entry); list.appendChild(entry);
}; };
var button = document.getElementById('openUsers');
button.disabled = false;
button.innerHTML = 'Users <i class="fa fa-user"></i>';
$('#users').modal('show');
}; };
} }
}); });
$('#users').modal('show');
}; };
generateInvites(empty = true); generateInvites(empty = true);
$("#login").modal('show'); $("#login").modal('show');

View File

@ -66,9 +66,10 @@
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required> <input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
</div> </div>
</form> </form>
<div id="loginErrorArea"></div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<input type="submit" class="btn btn-primary" value="Login" form="loginForm"> <button type="submit" id="loginSubmit" class="btn btn-primary" form="loginForm">Login</button>
</div> </div>
</div> </div>
</div> </div>
@ -130,7 +131,7 @@
<input type="email" class="form-control" placeholder="example@example.com" id="send_to_address" disabled> <input type="email" class="form-control" placeholder="example@example.com" id="send_to_address" disabled>
</div> </div>
</div> </div>
<input type="submit" class="btn btn-primary" value="Generate"> <button type="submit" id="generateSubmit" class="btn btn-primary">Generate</button>
</form> </form>
</div> </div>
</div> </div>

View File

@ -181,3 +181,4 @@ class Smtp(Email):
log.error(e.smtp_error) log.error(e.smtp_error)
except: except:
pass pass
return False

View File

@ -197,7 +197,9 @@ def generateInvite():
email = Smtp(address) email = Smtp(address)
email.construct_invite({'expiry': valid_till, email.construct_invite({'expiry': valid_till,
'code': invite['code']}) 'code': invite['code']})
email.send() response = email.send()
if response is False or type(response) != bool:
invite['email'] = f'Failed to send to {address}'
try: try:
with open(config['files']['invites'], 'r') as f: with open(config['files']['invites'], 'r') as f:
invites = json.load(f) invites = json.load(f)