Added Django models

This commit is contained in:
teras 2017-10-07 14:35:13 +03:00
parent ec914ab7a8
commit 01cded8763
7 changed files with 84 additions and 24 deletions

4
.gitignore vendored
View File

@ -99,3 +99,7 @@ ENV/
# mypy
.mypy_cache/
db.sqlite3
workspace.xml
tasks.xml

View File

@ -1,4 +1,6 @@
from django.contrib import admin
from challenges.models import *
admin.site.register(Challenge)
admin.site.register(Challenge)
admin.site.register(ChallengeTag)
admin.site.register(UserChallenge)

View File

@ -1,20 +1,27 @@
from django.db import models
from django.contrib.auth.models import User
'''class User:
user
class ChallengeTag:
id =
name
description
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 UserChallenge:
id
user
challenge
'''
class Challenge(models.Model):
#id =
name = models.CharField(256)
description = models.TextField()
#tags
id = models.AutoField(primary_key=True)
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)

View File

@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'challenges'
]
MIDDLEWARE = [

View File

@ -23,5 +23,6 @@ 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),
] + static(settings.STATIC_URL, document_root=settings.STATICFILES_DIRS)

View File

@ -1,13 +1,28 @@
from django.http import HttpResponse
from django.template import loader, RequestContext
from django.shortcuts import render_to_response, render
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
from django.contrib.auth import authenticate, login, logout
from .models import *
def index(request):
if request.method == 'GET':
return HttpResponse('test')
user = 'not logged in'
if request.user.is_authenticated:
user = request.user.username
challenges = Challenge.objects.all()
challenge_pair = UserChallenge.objects.all()
print(challenge_pair)
for item in challenge_pair:
print(item.user.username)
return render(request, 'index.html', {'logged_in': user,
'challenges': challenges,
'challenge_pair': challenge_pair})
@csrf_protect
@ -15,8 +30,8 @@ def register(request):
if request.method == 'GET':
return render(request, 'register.html')
elif request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
username = request.POST['user']
password = request.POST['pw']
password_confirmation = request.POST['password_confirmation']
if len(password) < 8:
@ -42,8 +57,8 @@ def login_view(request):
auth_user = request.user.username
return render(request, 'login.html', {'auth_user': auth_user})
elif request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
username = request.POST['user']
password = request.POST['pw']
user = authenticate(username=username, password=password)
@ -52,3 +67,8 @@ def login_view(request):
return HttpResponse('request suq')
else:
return HttpResponse('invalid username or password')
def logout_view(request):
logout(request)
return HttpResponse('logged out')

25
templates/index.html Normal file
View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<h1>Logged in user{{ logged_int }}</h1>
<ul>
{% for o in challenges %}
<tr class="row">
{{ o }}
</tr>
{% endfor %}
</ul>
<ul>
{% for i in challenge_pair %}
<tr class="row">
{{ i.user.username }} - {{ i.challenge }}
</tr>
{% endfor %}
</ul>
</body>
</html>