Commit 3f8b9d45 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

Merge branch 'branch/beautify_views' into 'master'

Update master with new data model and guidance enhancements

See merge request GPN18Programmierspiel/website!5
parents 7b015579 b41262ce
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'thoch2Ohthahgheequo2boripooPeW2cohsh0ahRoh0Hei8ahN'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'snake',
'USER': 'snake',
'PASSWORD': 'snake',
'HOST': 'database',
'PORT': '3306',
}
}
STATIC_ROOT = '/static/'
ALLOWED_HOSTS = ['website', 'localhost']
......@@ -105,4 +105,8 @@ table.snake-list {
#login_form div.segment {
width: 350px;
}
#snake_edit_form {
clear: both;
}
\ No newline at end of file
......@@ -13,6 +13,7 @@
{% else %}
<p>
<a href="{% url 'snake_create' %}" class="ui secondary button">Start with a clean snake</a>
<a href="{% url 'snake_restart' %}" class="ui secondary {% if not has_active %}disabled {% endif %}button" id="restart_snake">Restart your snake</a>
<a href="{% url 'snake_disable' %}" class="ui secondary {% if not has_active %}disabled {% endif %}button" id="disable_snake">Disable your snake</a>
</p>
<table class="ui inverted table snake-list">
......@@ -61,7 +62,7 @@
url: "{% url 'snake_activate' 1 %}".replace('1', id),
success: function(data) {
$.growl.notice({ message: data.message });
$('#disable_snake').removeClass('disabled');
$('#disable_snake, #restart_snake').removeClass('disabled');
},
error: function(data) {
$.growl.error({ message: data.message });
......@@ -69,5 +70,20 @@
});
}
});
$('#restart_snake').click(function(e) {
let button_url = $(this).attr('href');
$.ajax({
url: button_url,
success: function(data) {
$.growl.notice({ message: data.message });
},
error: function(data) {
$.growl.error({ message: data.message });
}
});
e.preventDefault();
});
</script>
{% endblock %}
\ No newline at end of file
......@@ -8,4 +8,5 @@ urlpatterns = [
path('edit/<int:snake_id>', core_views.snake_edit, name='snake_edit'),
path('activate/<int:snake_id>', core_views.snake_activate, name='snake_activate'),
path('disable', core_views.snake_disable, name='snake_disable'),
path('restart', core_views.snake_restart, name='snake_restart'),
]
......@@ -7,7 +7,7 @@ from django.forms import ModelForm, TextInput
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect, get_object_or_404
from core.models import SnakeVersion, ActiveSnake
from core.models import SnakeVersion, ActiveSnake, ServerCommand
def signup(request):
......@@ -86,7 +86,7 @@ def snake_delete(request, snake_id=-1):
snake.delete()
return snake_list(request)
return redirect('snake')
@login_required
def snake_activate(request, snake_id=-1):
......@@ -94,14 +94,14 @@ def snake_activate(request, snake_id=-1):
return JsonResponse({'message': 'ohh'}, status=500)
try:
snake = SnakeVersion.objects.get(pk=snake_id)
snake = SnakeVersion.objects.filter(user=request.user).get(pk=snake_id)
except SnakeVersion.DoesNotExist:
return JsonResponse({'message': 'Snake could not activated'}, status=500)
if snake.user != request.user:
return JsonResponse({'message': 'Snake could not activated'}, status=500)
obj, _ = ActiveSnake.objects.get_or_create(defaults={'user': snake.user, 'version': snake})
obj, _ = ActiveSnake.objects.filter(user=request.user).get_or_create(defaults={'user': snake.user, 'version': snake})
obj.version = snake
obj.save()
......@@ -117,5 +117,21 @@ def snake_disable(request):
if request.is_ajax():
return JsonResponse(response)
else:
return snake_list(request)
return redirect('snake')
@login_required
def snake_restart(request):
obj = ActiveSnake.objects.filter(user=request.user)
if obj:
response = {'message': 'Restart signal send for Snake {}'.format(obj[0].version.version)}
cmd = ServerCommand(
user=request.user,
command='kill'
)
cmd.save()
if request.is_ajax():
return JsonResponse(response)
else:
return snake_list(request)
......@@ -4,3 +4,4 @@ Pillow==5.0.0
pytz==2017.3
sqlparse==0.2.4
django-widget-tweaks==1.4.2
gunicorn==19.7.1
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment