Fixed issue #4: Keep form values for filters

master^2
M3TIOR 2017-12-07 22:02:51 -08:00
parent a7cd9d1f7d
commit 11e73dba96
7 changed files with 100 additions and 36 deletions

2
manage.py Normal file → Executable file
View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import os
import sys

View File

@ -58,7 +58,7 @@ ROOT_URLCONF = 'minetestdb.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "minetestdb/../templates")],
'DIRS': [os.path.join(BASE_DIR, "templates/")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
@ -81,6 +81,7 @@ DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mtcd',
'TEST_NAME': 'mtcd_debug',
'USER': 'site',
'PASSWORD': 'agfh1085',
'HOST': 'localhost',

View File

@ -7,4 +7,4 @@ class NewServerForm(ModelForm):
class Meta:
model = Server
fields = ['name', 'address', 'website', 'description', 'mt_version', 'tags']
exclude = ['owner']
exclude = ['owner']

View File

@ -1,7 +1,9 @@
from django.db import models
from django.contrib.auth.models import User
import uuid
# NOTE: Consider depricating / removing
class Tag(models.Model):
title = models.CharField(max_length=20)
@ -10,12 +12,22 @@ class Tag(models.Model):
class Server(models.Model):
# this is a unique identifier to the database.
# don't try and label it uuid though, it breaks things like that
#object_id = models.UUIDField(primary_key=True, default=uuid.uuid4)
# So actually... when using IDs / UUIDs, these fields are autogenerated by default
#id = ...
#pk = ...
#
# So you can do:
# Server.objects.all()[n].id || Server.objects.all()[n].pk
name = models.CharField(max_length=200)
owner = models.ForeignKey(User, null=True)
owner = models.ForeignKey(User, None, null=True)
address = models.URLField(max_length=200, null=True)
website = models.URLField(max_length=200, null=True)
description = models.TextField(null=True)
votes = models.IntegerField(default=0, null=True)
mt_version = models.CharField(max_length=10, null=True)
tags = models.ManyToManyField(Tag, verbose_name='Tags', blank=True)

View File

@ -1,3 +1,13 @@
from django.test import TestCase
from servers.models import Server
# Create your tests here.
class UI(TestCase):
def setUp(self):
s = []; # Server List
s.push(Server(name="derp", version="1.2.3"))
s.push(Server(name="thingy", version="2.3.3"))
s.push(Server(name="floopbar", version="4.2.1"))
for entry in s:
entry.save()

View File

@ -7,26 +7,63 @@ from .forms import NewServerForm
def index(request):
current_server_list = Server.objects.all()
servers = Server.objects.all()
if request.method == 'POST':
post = request.POST
post = request.POST.dict()
""" #Not fiddle farting with this thing, it broke my code earlier
# if post.sort == 'Oldest':
# current_server_list = Server.objects.order_by('id')
# if post.sort == 'Newest':
# current_server_list = Server.objects.order_by('-id')
for tag in post:
if tag != 'csrfmiddlewaretoken':
current_server_list = current_server_list.filter(tags=tag)
servers = servers.filter(tags=tag)
"""
else:
post = {}
tags = Tag.objects.all()
mt_version = ["0.4.16", "0.5.0"]
filters = {'Tags': tags, 'Minetest Version': mt_version}
# NOTE:
# The models are essentially a python equivalent of sql tables
# but there's the added ability for you to work with them directly in them
# language
#
# So when you're testing things. Instead of making a new structure to debugger
# your code, you just add to the sql database. Which you can do by calling
#
# python manage.py shell; # Which will open a python shell. Then...
# from servers.models import Server # imports our database model
# a = Server(name="immagoodname", version="1.2.3", ...) #create a new entry
# a.save() #save the entry in our sql database
#
# # exit and restart manage.py shell:
# from servers.models import Servers
# servers = Server.objects.all() # this should now be populated with query objects
# # will return : <QuerySet [<Server: Server object (1)...>]>
#
# # If you're having trouble with the data for any reason or need to delete it:
# python manage.py flush
# or "depending on your django version"
# python manage.py reset <APPNAME>
#
#
# Also, Fuck you m8, I hate using spaces to indent code blocks...
# I mean for crap sakes, the tab button's there for a reason!
# Python works with tabs and spaces, and tab's display can be styled
# per user preference!
#
# servers = Server.objects.all() #[[[ Just so I remember it's here... ]]]#
#
print(post)
print(post.keys())
# This is just a rebinding of variables in python scope, to template scope.
context = {
'post': post, # Raw Post
'servers': servers # Top level
#'version': servers[n].version # we'll access this from template scope
# with the "servers" data structure above
}
context = {'current_server_list': current_server_list,
'filters': filters,
'post': post}
return render(request, 'servers/index.html', context)
@ -46,4 +83,4 @@ def new(request):
else:
form = NewServerForm()
return render(request, 'servers/create.html', {'form': form})
return render(request, 'servers/create.html', {'form': form})

View File

@ -26,27 +26,31 @@
</div>
</div>
</div>{% endcomment %}
{% for ftypename, ftype in filters.items %}
{% if ftype %}
<div class="form-group">
<div class="panel panel-default">
<div class="panel-heading">{{ ftypename }} </div>
<div class="panel-body">
{% for filter in ftype %}
<div class="checkbox">
<label><input type="checkbox"
name="{{ filter.id }}"
{% if filter.id in post.items %}
checked="checked"
{% endif %}
>{{ filter }}</label>
</div>
{% endfor %}
</div>
</div>
<div class="form-group">
<div class="panel panel-default">
<div class="panel-heading">Minetest Version </div>
<div class="panel-body">
{% for server in servers %}
<div class="checkbox">
<label><input type="checkbox"
name="{{ server.id }}"
{% comment %}
Here, I had to filter the value of server.id
because it's natively an interger.
The django template interpreter doesn't do any
type checking so we have to have both values we're
comparing be the same type, otherwise they won't
be properly compaired and our code misbehave.
{% endcomment %}
{% if server.id|stringformat:"i" in post.keys%}
checked
{% endif %}
>{{ server.mt_version }}</label>
</div>
{% endfor %}
</div>
{% endif %}
{% endfor %}
</div>
</div>
<input type="submit" class="btn btn-default" value="Apply Filters">
</form>
@ -78,4 +82,4 @@
</div>
</div>
</div>
{% endblock %}
{% endblock %}