Commit 2531f8c6 by Dudás Ádám

school: improve conformance of source

parent 6f6b9ee2
from django.contrib import messages
from django.core.exceptions import ValidationError
from django import contrib
from django.utils.translation import ugettext_lazy as _
from school import models
import string
class GroupInline(contrib.admin.TabularInline):
model = models.Group
extra = 3
class CourseAdmin(contrib.admin.ModelAdmin):
model = models.Course
inlines = (GroupInline, )
......@@ -16,18 +14,20 @@ class CourseAdmin(contrib.admin.ModelAdmin):
list_display = ('code', 'name', 'short_name', 'owner_list')
list_editable = ('name', 'short_name')
class GroupAdmin(contrib.admin.ModelAdmin):
model = models.Group
filter_horizontal = ('owners', 'members', )
list_display = ('name', 'course', 'semester', 'owner_list', 'member_count')
list_filter = ('semester', 'course')
class SemesterAdmin(contrib.admin.ModelAdmin):
model = models.Semester
list_display = ('id', 'name', 'start', 'end')
list_editable = ('name', 'start', 'end')
contrib.admin.site.register(models.Course, CourseAdmin)
contrib.admin.site.register(models.Semester, SemesterAdmin)
contrib.admin.site.register(models.Group, GroupAdmin)
......@@ -14,6 +14,7 @@ LANGUAGE_CHOICES = (('hu', _('Hungarian')), ('en', _('English')))
logger = logging.getLogger(__name__)
def create_user_profile(sender, instance, created, **kwargs):
"""
User creation hook.
......@@ -34,20 +35,21 @@ def create_user_profile(sender, instance, created, **kwargs):
except Exception as e: # pragma: no cover
logger.warning("Couldn't create profile for user: %(username)s"
"\nReason: %(exception)s",
{"username": instance.username,
"exception": e})
{"username": instance.username, "exception": e})
return
p.clean()
p.save()
post_save.connect(create_user_profile, sender=User)
class Person(models.Model):
"""
Personal settings and attributes of a user.
"""
user = models.ForeignKey(User, null=True, blank=True, unique=True)
language = models.CharField(verbose_name=_('language'), blank=False,
max_length=10, choices=LANGUAGE_CHOICES, default=LANGUAGE_CODE)
max_length=10, choices=LANGUAGE_CHOICES,
default=LANGUAGE_CODE)
code = models.CharField(_('code'), max_length=30, unique=True)
def get_owned_shares(self):
......@@ -85,6 +87,7 @@ class Person(models.Model):
verbose_name = _('person')
verbose_name_plural = _('persons')
class Course(models.Model):
code = models.CharField(max_length=20, unique=True,
verbose_name=_('course code'))
......@@ -92,9 +95,10 @@ class Course(models.Model):
verbose_name=_('name'))
short_name = models.CharField(max_length=10, null=True, blank=True,
verbose_name=_('name'))
default_group = models.ForeignKey('Group', null=True, blank=True,
related_name='default_group_of', verbose_name=_('default group'),
help_text=_('New users will be automatically assigned to this group.'))
default_group = models.ForeignKey(
'Group', null=True, blank=True, related_name='default_group_of',
verbose_name=_('default group'), help_text=_('New users will be '
'automatically assigned to this group.'))
owners = models.ManyToManyField(Person, blank=True, null=True,
verbose_name=_('owners'))
......@@ -107,7 +111,8 @@ class Course(models.Model):
return self.default_group
else:
default_group = Group(name=_("%s (auto)") % self.short(),
semester=Semester.get_current(), course=self)
semester=Semester.get_current(),
course=self)
default_group.save()
self.default_group_id = default_group.id
self.save()
......@@ -175,9 +180,11 @@ class Group(models.Model):
semester = models.ForeignKey('Semester', null=False, blank=False,
verbose_name=_('semester'))
owners = models.ManyToManyField(Person, blank=True, null=True,
related_name='owned_groups', verbose_name=_('owners'))
related_name='owned_groups',
verbose_name=_('owners'))
members = models.ManyToManyField(Person, blank=True, null=True,
related_name='course_groups', verbose_name=_('members'))
related_name='course_groups',
verbose_name=_('members'))
class Meta:
unique_together = (('name', 'course', 'semester', ), )
......
from datetime import datetime, timedelta
from django.test import TestCase
from django.contrib.auth.models import User, Group as AuthGroup
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from ..models import create_user_profile, Person, Course, Semester, Group
class CreateUserProfileTestCase(TestCase):
def setUp(self):
self.user = User(username="testuser", password="testpass",
email="test@mail.com", first_name="Test", last_name="User")
email="test@mail.com", first_name="Test",
last_name="User")
Person.objects.all().delete()
with self.assertRaises(Person.DoesNotExist):
Person.objects.get(code=self.user.username)
......@@ -23,6 +25,7 @@ class CreateUserProfileTestCase(TestCase):
create_user_profile(self.user.__class__, self.user, True)
self.assertIsNotNone(Person.objects.get(code=self.user.username))
class PersonTestCase(TestCase):
"""Test 'static' Person facts."""
def test_language_code_in_choices(self):
......@@ -32,11 +35,13 @@ class PersonTestCase(TestCase):
choice_codes = [code for (code, _) in language_field.choices]
self.assertIn(language_field.default, choice_codes)
class PersonWithUserTestCase(TestCase):
"""Test Person entities which have their user attribute set."""
def setUp(self):
self.user = User(username="testuser", password="testpass",
email="test@mail.com", first_name="Test", last_name="User")
email="test@mail.com", first_name="Test",
last_name="User")
Person.objects.all().delete()
self.person = Person.objects.create(code='testcode', user=self.user)
......@@ -60,6 +65,7 @@ class PersonWithUserTestCase(TestCase):
self.person.user.last_name = None
self.assertIsNotNone(self.person.__unicode__())
class PersonWithoutUserTestCase(TestCase):
"""Test Person entities which doesn't have their user attribute set."""
def setUp(self):
......@@ -78,6 +84,7 @@ class PersonWithoutUserTestCase(TestCase):
def test_unicode(self):
self.assertIsNotNone(self.person.__unicode__())
class CourseTestCase(TestCase):
def setUp(self):
now = datetime.now()
......@@ -85,10 +92,10 @@ class CourseTestCase(TestCase):
delta = timedelta(weeks=7)
self.testperson1 = Person.objects.create(code="testperson1")
self.testperson2 = Person.objects.create(code="testperson2")
self.testsemester = Semester.objects.create(name="testsemester",
start=date-delta, end=date+delta)
self.testcourse = Course.objects.create(code="testcode",
name="testname", short_name="tn")
self.testsemester = Semester.objects.create(
name="testsemester", start=date-delta, end=date+delta)
self.testcourse = Course.objects.create(
code="testcode", name="testname", short_name="tn")
self.testcourse.owners.add(self.testperson1, self.testperson2)
def test_get_or_create_default_group(self):
......@@ -118,18 +125,19 @@ class CourseTestCase(TestCase):
self.testcourse.short_name = None
self.assertIsNotNone(self.testcourse.short())
class SemesterTestCase(TestCase):
def setUp(self):
now = datetime.now()
date = now.date()
self.now = now
delta = timedelta(weeks=7)
self.last_semester = Semester.objects.create(name="testsem1",
start=date-3*delta, end=date-delta)
self.current_semester = Semester.objects.create(name="testsem2",
start=date-delta, end=date+delta)
self.next_semester = Semester.objects.create(name="testsem3",
start=date+delta, end=date+3*delta)
self.last_semester = Semester.objects.create(
name="testsem1", start=date-3*delta, end=date-delta)
self.current_semester = Semester.objects.create(
name="testsem2", start=date-delta, end=date+delta)
self.next_semester = Semester.objects.create(
name="testsem3", start=date+delta, end=date+3*delta)
def test_is_on(self):
self.assertFalse(self.last_semester.is_on(self.now))
......@@ -146,16 +154,17 @@ class SemesterTestCase(TestCase):
def test_unicode(self):
self.current_semester.__unicode__()
class GroupTestCase(TestCase):
def setUp(self):
date = datetime.now().date()
delta = timedelta(weeks=7)
semester = Semester.objects.create(name="testsem",
start=date-delta, end=date+delta)
self.testcourse = Course.objects.create(code="testcode",
name="testname", short_name="tn")
self.testgroup = Group.objects.create(name="testgrp",
semester=semester, course=self.testcourse)
semester = Semester.objects.create(
name="testsem", start=date-delta, end=date+delta)
self.testcourse = Course.objects.create(
code="testcode", name="testname", short_name="tn")
self.testgroup = Group.objects.create(
name="testgrp", semester=semester, course=self.testcourse)
def test_owner_list(self):
self.assertIsNotNone(self.testgroup.owner_list())
......@@ -184,4 +193,3 @@ class GroupTestCase(TestCase):
def test_get_absolute_url(self):
self.assertIsNotNone(self.testgroup.get_absolute_url())
from datetime import datetime
from itertools import chain
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User, Group as AGroup
from django.contrib import messages
from django.core import signing
from django.core.exceptions import PermissionDenied, ValidationError
from django.core.mail import mail_managers, send_mail
from django.core.urlresolvers import reverse
from django.db import transaction
from django.forms import ModelForm, Textarea
from django.http import Http404
from django.shortcuts import (render, render_to_response, get_object_or_404,
from django.http import HttpResponse
from django.shortcuts import (render_to_response, get_object_or_404,
redirect)
from django.template import RequestContext
from django.template.loader import render_to_string
from django.utils.decorators import method_decorator
from django.utils.http import is_safe_url
from django.utils.translation import get_language as lang
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.decorators.http import *
from django.views.generic import *
from one.models import *
from school.models import *
from one.models import Template, UserCloudDetails
from school.models import Person, Semester, Course, Group
import django.contrib.auth as auth
import logging
import json
import re
logger = logging.getLogger(__name__)
neptun_re = re.compile('^[a-zA-Z][a-zA-Z0-9]{5}$')
def logout(request):
auth.logout(request)
return redirect('/Shibboleth.sso/Logout?return=https%3a%2f%2fcloud.ik.bme.hu%2f')
url = '/Shibboleth.sso/Logout?return=https%3a%2f%2fcloud.ik.bme.hu%2f'
return redirect(url)
@ensure_csrf_cookie
......@@ -54,7 +47,9 @@ def login(request):
try:
user.email = request.META['email']
except KeyError:
messages.error(request, _("The identity provider did not pass the mandatory e-mail data."))
messages.error(request,
_("The identity provider did not pass the mandatory "
"e-mail data."))
raise PermissionDenied()
user.save()
p, created = Person.objects.get_or_create(code=user.username)
......@@ -76,8 +71,7 @@ def login(request):
try:
g.members.add(p)
g.save()
messages.info(request,
_('Course "%s" added.') % g.course)
messages.info(request, _('Course "%s" added.') % g.course)
logger.info('Django Course "%s" added.' % g.course)
except Exception as e:
messages.error(request,
......@@ -98,7 +92,8 @@ def login(request):
_('Course "%s" ownership added.') % g.course)
except Exception as e:
messages.error(request,
_('Failed to add course "%s" ownership.') % g.course)
_('Failed to add course "%s" ownership.')
% g.course)
logger.warning("Django ex %s" % e)
co.save()
g.save()
......@@ -158,7 +153,7 @@ def group_show(request, gid):
user = request.user
group = get_object_or_404(Group, id=gid)
mytemplates = Template.objects.filter(owner=request.user, state='READY')
mytemplates = Template.objects.filter(owner=user, state='READY')
for t in mytemplates:
t.myshares = t.share_set.filter(group=group)
......@@ -175,7 +170,7 @@ def group_show(request, gid):
'mytemplates': mytemplates,
'publictemplates': publictemplates,
'noshare': not has_share,
'userdetails': UserCloudDetails.objects.get(user=request.user),
'userdetails': UserCloudDetails.objects.get(user=user),
'owners': group.owners.all(),
}))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment