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,15 +319,12 @@ $(document).ready(function() {
// //
// Modals // Modals
// //
// hide modal add user // hide modal add user
function hide_modal() { function hide_modal() { $('#modal_add_user').fadeOut('fast'); }
$('#modal_add_user').fadeOut('fast');
}
// modal #add_user // modal #add_user
$('#show_add_user').click(function() { $('#show_add_user').click(function() {
@ -336,10 +332,7 @@ $(document).ready(function() {
$(document).on('click', function(event) { $(document).on('click', function(event) {
if ($('#modal_add_user').is(':visible')) { if ($('#modal_add_user').is(':visible')) {
let target = $(event.target); if ($(event.target).attr('id') === 'modal_add_user') { hide_modal(); }
if (target.attr('id') === 'modal_add_user') {
hide_modal();
}
} }
}); });
@ -352,16 +345,15 @@ $(document).ready(function() {
}); });
// button #close_modal_add_user // button #close_modal_add_user
$('#close_modal_add_user').click(function() { $('#close_modal_add_user').click(function() { hide_modal(); });
hide_modal();
});
// button #add_user // button #add_user
$('#add_user').click(function() { $('#add_user').click(function() {
let name = encodeURIComponent($('#text_user').val()); let name = encodeURIComponent($('#text_user')
let color = encodeURIComponent($('#text_color').val()); .val().trim().substring(0, 19).replace(' ', '-'));
let color = encodeURIComponent($('#text_color')
.val().trim().substring(0, 9 ).replace(' ', '-'));
$.ajax({ $.ajax({
type: 'GET', type: 'GET',
@ -381,8 +373,8 @@ $(document).ready(function() {
} }
}); });
}); });
}); });
// end modal #add_user
// //
@ -393,12 +385,11 @@ $(document).ready(function() {
let name = cookie.get('coinc-name'); let name = cookie.get('coinc-name');
let color = cookie.get('coinc-color'); let color = cookie.get('coinc-color');
if (name != '') { if (name != '' && color != '') {
render_login(name, color); render_login(name, color);
} else { } else {
name = $('#found_by option:first-child').html(); name = $('#found_by option:first-child').html();
color = $('#found_by option:first-child').val(); color = $('#found_by option:first-child').attr('color');
render_login(name, color); render_login(name, color);
} }

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=''):