Changed project name

This commit is contained in:
teras
2017-11-16 15:13:29 +02:00
committed by Lauri Võsandi
parent c9b41b6ea6
commit d20b2cb3e4
11 changed files with 48 additions and 14 deletions

0
kspace/__init__.py Normal file
View File

8
kspace/admin.py Normal file
View File

@@ -0,0 +1,8 @@
from django.contrib import admin
from kspace.models import *
admin.site.register(Challenge)
admin.site.register(ChallengeTag)
admin.site.register(UserChallenge)
admin.site.register(Profile)

54
kspace/models.py Normal file
View File

@@ -0,0 +1,54 @@
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
import os
def get_image_path(instance, filename):
return os.path.join('icons', str(instance.id), filename)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
icon = models.ImageField(upload_to=get_image_path, default='default_icon.png')
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
class ChallengeTag(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
description = models.TextField(blank=True)
def __str__(self):
return self.name
class Challenge(models.Model):
id = models.AutoField(primary_key=True)
creator = models.ForeignKey(User)
name = models.CharField(max_length=256)
description = models.TextField(blank=True)
tags = models.ManyToManyField(ChallengeTag, blank=True)
def __str__(self):
return self.name
class UserChallenge(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User)
challenge = models.ForeignKey(Challenge)

129
kspace/settings.py Normal file
View File

@@ -0,0 +1,129 @@
"""
Django settings for kspace project.
Generated by 'django-admin startproject' using Django 1.11.6.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'gze3mreum6caz26_k2zq8(zn+)v3pdfaup+-e20eu@vca5st=b'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['k-space.ee', 'kspace', '127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'kspace'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'kspace.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'kspace.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'staticfiles')
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

37
kspace/urls.py Normal file
View File

@@ -0,0 +1,37 @@
"""challenges URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.storage import staticfiles_storage
from django.views.generic.base import RedirectView
from kspace import views
urlpatterns = [
url(r'^$', views.index),
url(r'^admin/', admin.site.urls),
#url(r'^login/', views.login_view),
#url(r'^logout/', views.logout_view),
#url(r'^register/', views.register),
url(r'^challenge/(?P<id>[0-9]+)', views.challenge),
url(r'^challenges/', views.challenges),
url(r'^halloffame/', views.hall_of_fame),
url(r'^profile/(?P<username>[\w.-]+)', views.profile),
url(r'^favicon.ico$', RedirectView.as_view(url=staticfiles_storage.url('favicon.ico'), permanent=False), name='favicon')
] + static(settings.STATIC_URL, document_root=settings.STATICFILES_DIRS) + \
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

106
kspace/views.py Normal file
View File

@@ -0,0 +1,106 @@
from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from kspace.models import *
def index(request):
if request.method == 'GET':
data = {
'challenges': Challenge.objects.all()[:4]
}
return render(request, 'index.html', data)
@csrf_protect
def register(request):
if request.method == 'GET':
return render(request, 'register.html')
elif request.method == 'POST':
username = request.POST['user']
password = request.POST['pw']
password_confirmation = request.POST['password_confirmation']
if len(password) < 8:
return HttpResponse("password too short")
if password != password_confirmation:
return HttpResponse("passwords do not match")
if not User.objects.filter(username=username).exists():
user = User.objects.create_user(username, password=password)
user.save()
else:
return HttpResponse("user exist")
return HttpResponse("User {} created".format(username))
@csrf_protect
def login_view(request):
if request.method == 'GET':
auth_user = 'no user'
if request.user.is_authenticated:
auth_user = request.user.username
return render(request, 'login.html', {'auth_user': auth_user})
elif request.method == 'POST':
username = request.POST['user']
password = request.POST['pw']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return HttpResponse('request suq')
else:
return HttpResponse('invalid username or password')
def logout_view(request):
logout(request)
return HttpResponse('logged out')
def challenge(request, id):
if request.method == 'GET':
data = {
'challenge': Challenge.objects.get(id=id)
}
return render(request, 'challenge.html', data)
elif request.method == 'POST':
if not request.user.is_authenticated:
return HttpResponse('not logged in')
challenge_name = request.POST['challenge_name']
challenge_description = request.POST['challenge_discription']
tags = []
new_challenge = Challenge(creator=request.user, name=challenge_name, description=challenge_description, tags=tags)
new_challenge.save()
def challenges(request):
if request.method == 'GET':
data = {
'challenges': Challenge.objects.all()
}
return render(request, 'challenges.html', data)
def hall_of_fame(request):
if request.method == 'GET':
data = {
'users': sorted(User.objects.all(), key=lambda x: len(UserChallenge.objects.filter(user=x)), reverse=True)
}
return render(request, 'hall_of_fame.html', data)
def profile(request, username=''):
if request.method == 'GET':
user = User.objects.get(username=username)
user_challenges = UserChallenge.objects.filter(user=user)
data = {
'user': user,
'challenges': user_challenges
}
return render(request, 'profile.html', data)

16
kspace/wsgi.py Normal file
View File

@@ -0,0 +1,16 @@
"""
WSGI config for kspace project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kspace.settings")
application = get_wsgi_application()