Commit 47e7c475 authored by Ola Hermann Opheim's avatar Ola Hermann Opheim
Browse files

Merge branch 'fix/sorting' into 'master'

Fix/sorting

See merge request olahop/agreelance123!7
parents 639f0a73 d6bcdda3
# Generated by Django 2.1.7 on 2020-03-13 15:53
import datetime
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('projects', '0005_taskoffer_edit'),
]
operations = [
migrations.AddField(
model_name='project',
name='timestamp',
field=models.DateTimeField(default=datetime.datetime(2020, 3, 13, 15, 53, 54, 128210)),
),
migrations.AlterField(
model_name='projects',
name='sort_by',
field=models.CharField(choices=[('t', 'Title A-Z'), ('!t', 'Title Z-A'), ('d', 'Date A-Z'), ('!d', 'Date Z-A'), ('o', 'Owner A-Z'), ('!o', 'Owner Z-A')], default='t', max_length=11),
),
]
......@@ -3,20 +3,21 @@ from user.models import Profile
from django.core.files.storage import FileSystemStorage
from django.conf import settings
from datetime import datetime
import os
class Projects(models.Model):
TITLEASC = 't'
TITLEDESC = '!t'
#DATEASC = 'd'
#DATEDESC = '!d'
DATEASC = 'd'
DATEDESC = '!d'
OWNERASC = 'o'
OWNERDESC = '!o'
SORT_CHOICES = (
(TITLEASC, 'Title A-Z'),
(TITLEDESC, 'Title Z-A'),
#(DATEASC, 'Date A-Z'),
#(DATEDESC, 'Date Z-A'),
(DATEASC, 'Date A-Z'),
(DATEDESC, 'Date Z-A'),
(OWNERASC, 'Owner A-Z'),
(OWNERDESC, 'Owner Z-A'),
)
......@@ -61,6 +62,7 @@ class Project(models.Model):
participants = models.ManyToManyField(Profile, related_name='project_participants')
category = models.ForeignKey(ProjectCategory, on_delete=models.CASCADE, related_name='project_category')
edit = models.BooleanField(default=False)
timestamp = models.DateTimeField(default=datetime.now())
OPEN = 'o'
INPROG = 'i'
FINISHED = 'f'
......
......@@ -8,25 +8,35 @@ from django.contrib.auth.models import User
def projects(request):
def get_username(elem):
return elem.user.user.username.lower()
def get_title(elem):
return elem.title.lower()
def get_timestamp(elem):
return elem.timestamp
projects = Project.objects.all()
project_categories = ProjectCategory.objects.all()
sort_form = SortForm()
if request.method == 'POST' and 'sort_form' in request.POST:
sort_form = SortForm(request.POST)
if sort_form.is_valid():
sort_by = sort_form.save(commit=False)
if request.POST.getlist('sort_by')[0] == 't':
projects = Project.objects.order_by('title').all()
projects = sorted(projects, key=get_title)
elif request.POST.getlist('sort_by')[0] == '!t':
projects = Project.objects.order_by('-title').all()
projects = sorted(projects, key=get_title, reverse=True)
elif request.POST.getlist('sort_by')[0] == 'o':
projects = Project.objects.order_by('user').all()
projects = sorted(projects, key=get_username)
elif request.POST.getlist('sort_by')[0] == '!o':
projects = Project.objects.order_by('-user').all()
sort_form = SortForm(request.POST)
if sort_form.is_valid():
sort_by = sort_form.save(commit=False)
else:
projects = Project.objects.order_by('title').all()
sort_form = SortForm()
project_categories = ProjectCategory.objects.all()
projects = sorted(projects, key=get_username, reverse=True)
elif request.POST.getlist('sort_by')[0] == 'd':
projects = sorted(projects, key=get_timestamp)
elif request.POST.getlist('sort_by')[0] == '!d':
projects = sorted(projects, key=get_timestamp, reverse=True)
return render(request,
'projects/projects.html',
......
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