This commit is contained in:
koksnuss 2020-05-11 14:57:12 +02:00
parent fb09a02a22
commit 5d088f0461
3 changed files with 74 additions and 89 deletions

View File

@ -28,9 +28,9 @@ year_now = int(datetime.now().year)
class User(Model): class User(Model):
name = CharField('Name', max_length=50, default='Anonym') name = CharField('Name', max_length=20, default='Anonym')
color = CharField('Farbe', max_length=20, default='white') color = CharField('Farbe', max_length=20, default='purple')
def __str__(self): def __str__(self):

View File

@ -44,13 +44,11 @@ function get_datetime() {
// manage user login // manage user login
function render_login(input_name, input_color) { function render_login(name, color) {
let color = decodeURIComponent(input_color);
let name = decodeURIComponent(input_name);
new Cookie('coinc-name', name) new Cookie('coinc-name', name)
render_login_color(color); render_login_color(color);
let option = $('nav.navbar option[value=' + name + ']'); let option = $('nav.navbar option[value=' + name + ']');
if (option.length == 0) { if (option.length == 0) {
$('nav.navbar select').append($('<option>', { $('nav.navbar select').append($('<option>', {
@ -172,6 +170,7 @@ function save_settings() {
} }
// begin document.ready
$(document).ready(function() { $(document).ready(function() {
// //
@ -320,89 +319,81 @@ $(document).ready(function() {
//
// Modals
//
// // hide modal add user
// Modals function hide_modal() { $('#modal_add_user').fadeOut('fast'); }
//
// hide modal add user // modal #add_user
function hide_modal() { $('#show_add_user').click(function() {
$('#modal_add_user').fadeOut('fast'); $('#modal_add_user').fadeIn('fast');
}
// modal #add_user $(document).on('click', function(event) {
$('#show_add_user').click(function() { if ($('#modal_add_user').is(':visible')) {
$('#modal_add_user').fadeIn('fast'); if ($(event.target).attr('id') === 'modal_add_user') { hide_modal(); }
}
});
$(document).on('click', function(event) { // detect ESC keypress
if ($('#modal_add_user').is(':visible')) { $(document).keypress(function(event) {
let target = $(event.target); if (event.keyCode == 27) { // ESC is pressed
if (target.attr('id') === 'modal_add_user') { hide_modal();
hide_modal(); $(document).off('keypress');
} }
} });
});
// button #close_modal_add_user
// detect ESC keypress $('#close_modal_add_user').click(function() { hide_modal(); });
$(document).keypress(function(event) {
if (event.keyCode == 27) { // ESC is pressed // button #add_user
hide_modal(); $('#add_user').click(function() {
$(document).off('keypress');
} let name = encodeURIComponent($('#text_user')
}); .val().trim().substring(0, 19).replace(' ', '-'));
let color = encodeURIComponent($('#text_color')
// button #close_modal_add_user .val().trim().substring(0, 9 ).replace(' ', '-'));
$('#close_modal_add_user').click(function() {
hide_modal(); $.ajax({
type: 'GET',
dataType: 'json',
url: 'add/user/' + name + '/color/' + color,
success: function(response) {
if (response.status === 0) {
$('#response').fadeOut('fast');
hide_modal();
render_login(name, color);
} else {
$('#response').html(response.message);
$('#response').css('color', 'red');
}
}
});
});
}); });
// end modal #add_user
// button #add_user //
$('#add_user').click(function() { // Main routine
//
let name = encodeURIComponent($('#text_user').val()); let cookie = new Cookie();
let color = encodeURIComponent($('#text_color').val()); let name = cookie.get('coinc-name');
let color = cookie.get('coinc-color');
$.ajax({ if (name != '' && color != '') {
type: 'GET', render_login(name, color);
dataType: 'json', } else {
url: 'add/user/' + name + '/color/' + color, name = $('#found_by option:first-child').html();
success: function(response) { color = $('#found_by option:first-child').attr('color');
render_login(name, color);
}
if (response.status === 0) { save_settings()
$('#response').fadeOut('fast'); render_found();
hide_modal();
render_login(name, color);
} else {
$('#response').html(response.message);
$('#response').css('color', 'red');
}
}
});
});
});
//
// Main routine
//
let cookie = new Cookie();
let name = cookie.get('coinc-name');
let color = cookie.get('coinc-color');
if (name != '') {
render_login(name, color);
} else {
name = $('#found_by option:first-child').html();
color = $('#found_by option:first-child').val();
render_login(name, color);
}
save_settings()
render_found();
}); });

View File

@ -186,23 +186,17 @@ def show_coin(country, year, stamp, value):
def add_user(request, username, color): def add_user(request, username, color):
''' add a user ''' ''' add a user '''
status = 0
message = ''
if username and color: if username and color:
try: try:
existing_user = User.objects.get(name=username) existing_user = User.objects.get(name=username)
status = 1 return response(1, f"'{username}' ist schon vergeben.")
message = f"'{username}' ist schon vergeben."
except User.DoesNotExist: except User.DoesNotExist:
User.objects.create(name=username, color=color) User.objects.create(name=username, color=color)
message = 'success'
else: else:
status = 1 return response(1, 'Du musst einen Namen und Farbe angeben.')
message = 'Du musst einen Namen und Farbe angeben.'
return HttpResponse(dumps({ 'status': status, 'message': message })) return response()
def response(status=0, message=''): def response(status=0, message=''):