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 import contrib
from django.utils.translation import ugettext_lazy as _
from school import models from school import models
import string
class GroupInline(contrib.admin.TabularInline): class GroupInline(contrib.admin.TabularInline):
model = models.Group model = models.Group
extra = 3 extra = 3
class CourseAdmin(contrib.admin.ModelAdmin): class CourseAdmin(contrib.admin.ModelAdmin):
model = models.Course model = models.Course
inlines = (GroupInline, ) inlines = (GroupInline, )
...@@ -16,18 +14,20 @@ class CourseAdmin(contrib.admin.ModelAdmin): ...@@ -16,18 +14,20 @@ class CourseAdmin(contrib.admin.ModelAdmin):
list_display = ('code', 'name', 'short_name', 'owner_list') list_display = ('code', 'name', 'short_name', 'owner_list')
list_editable = ('name', 'short_name') list_editable = ('name', 'short_name')
class GroupAdmin(contrib.admin.ModelAdmin): class GroupAdmin(contrib.admin.ModelAdmin):
model = models.Group model = models.Group
filter_horizontal = ('owners', 'members', ) filter_horizontal = ('owners', 'members', )
list_display = ('name', 'course', 'semester', 'owner_list', 'member_count') list_display = ('name', 'course', 'semester', 'owner_list', 'member_count')
list_filter = ('semester', 'course') list_filter = ('semester', 'course')
class SemesterAdmin(contrib.admin.ModelAdmin): class SemesterAdmin(contrib.admin.ModelAdmin):
model = models.Semester model = models.Semester
list_display = ('id', 'name', 'start', 'end') list_display = ('id', 'name', 'start', 'end')
list_editable = ('name', 'start', 'end') list_editable = ('name', 'start', 'end')
contrib.admin.site.register(models.Course, CourseAdmin) contrib.admin.site.register(models.Course, CourseAdmin)
contrib.admin.site.register(models.Semester, SemesterAdmin) contrib.admin.site.register(models.Semester, SemesterAdmin)
contrib.admin.site.register(models.Group, GroupAdmin) contrib.admin.site.register(models.Group, GroupAdmin)
...@@ -14,6 +14,7 @@ LANGUAGE_CHOICES = (('hu', _('Hungarian')), ('en', _('English'))) ...@@ -14,6 +14,7 @@ LANGUAGE_CHOICES = (('hu', _('Hungarian')), ('en', _('English')))
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def create_user_profile(sender, instance, created, **kwargs): def create_user_profile(sender, instance, created, **kwargs):
""" """
User creation hook. User creation hook.
...@@ -33,32 +34,33 @@ def create_user_profile(sender, instance, created, **kwargs): ...@@ -33,32 +34,33 @@ def create_user_profile(sender, instance, created, **kwargs):
p = Person.objects.create(code=instance.username) p = Person.objects.create(code=instance.username)
except Exception as e: # pragma: no cover except Exception as e: # pragma: no cover
logger.warning("Couldn't create profile for user: %(username)s" logger.warning("Couldn't create profile for user: %(username)s"
"\nReason: %(exception)s", "\nReason: %(exception)s",
{"username": instance.username, {"username": instance.username, "exception": e})
"exception": e})
return return
p.clean() p.clean()
p.save() p.save()
post_save.connect(create_user_profile, sender=User) post_save.connect(create_user_profile, sender=User)
class Person(models.Model): class Person(models.Model):
""" """
Personal settings and attributes of a user. Personal settings and attributes of a user.
""" """
user = models.ForeignKey(User, null=True, blank=True, unique=True) user = models.ForeignKey(User, null=True, blank=True, unique=True)
language = models.CharField(verbose_name=_('language'), blank=False, 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) code = models.CharField(_('code'), max_length=30, unique=True)
def get_owned_shares(self): def get_owned_shares(self):
"""Get the shares of the groups which the person owns.""" """Get the shares of the groups which the person owns."""
return one.models.Share.objects.filter( return one.models.Share.objects.filter(
group__in=self.owned_groups.all()) group__in=self.owned_groups.all())
def get_shares(self): def get_shares(self):
"""Get the shares of the groups which the person is a member of.""" """Get the shares of the groups which the person is a member of."""
return one.models.Share.objects.filter( return one.models.Share.objects.filter(
group__in=self.course_groups.all()) group__in=self.course_groups.all())
def short_name(self): def short_name(self):
if self.user: if self.user:
...@@ -74,8 +76,8 @@ class Person(models.Model): ...@@ -74,8 +76,8 @@ class Person(models.Model):
if self.user.last_name and self.user.first_name: if self.user.last_name and self.user.first_name:
# TRANSLATORS: full name format used in enumerations # TRANSLATORS: full name format used in enumerations
return _("%(first)s %(last)s") % { return _("%(first)s %(last)s") % {
'first': self.user.first_name, 'first': self.user.first_name,
'last': self.user.last_name} 'last': self.user.last_name}
else: else:
return self.user.username return self.user.username
else: else:
...@@ -85,18 +87,20 @@ class Person(models.Model): ...@@ -85,18 +87,20 @@ class Person(models.Model):
verbose_name = _('person') verbose_name = _('person')
verbose_name_plural = _('persons') verbose_name_plural = _('persons')
class Course(models.Model): class Course(models.Model):
code = models.CharField(max_length=20, unique=True, code = models.CharField(max_length=20, unique=True,
verbose_name=_('course code')) verbose_name=_('course code'))
name = models.CharField(max_length=80, null=True, blank=True, name = models.CharField(max_length=80, null=True, blank=True,
verbose_name=_('name')) verbose_name=_('name'))
short_name = models.CharField(max_length=10, null=True, blank=True, short_name = models.CharField(max_length=10, null=True, blank=True,
verbose_name=_('name')) verbose_name=_('name'))
default_group = models.ForeignKey('Group', null=True, blank=True, default_group = models.ForeignKey(
related_name='default_group_of', verbose_name=_('default group'), 'Group', null=True, blank=True, related_name='default_group_of',
help_text=_('New users will be automatically assigned to this group.')) verbose_name=_('default group'), help_text=_('New users will be '
'automatically assigned to this group.'))
owners = models.ManyToManyField(Person, blank=True, null=True, owners = models.ManyToManyField(Person, blank=True, null=True,
verbose_name=_('owners')) verbose_name=_('owners'))
class Meta: class Meta:
verbose_name = _('course') verbose_name = _('course')
...@@ -107,7 +111,8 @@ class Course(models.Model): ...@@ -107,7 +111,8 @@ class Course(models.Model):
return self.default_group return self.default_group
else: else:
default_group = Group(name=_("%s (auto)") % self.short(), default_group = Group(name=_("%s (auto)") % self.short(),
semester=Semester.get_current(), course=self) semester=Semester.get_current(),
course=self)
default_group.save() default_group.save()
self.default_group_id = default_group.id self.default_group_id = default_group.id
self.save() self.save()
...@@ -143,7 +148,7 @@ class Course(models.Model): ...@@ -143,7 +148,7 @@ class Course(models.Model):
class Semester(models.Model): class Semester(models.Model):
name = models.CharField(max_length=20, unique=True, null=False, name = models.CharField(max_length=20, unique=True, null=False,
verbose_name=_('name')) verbose_name=_('name'))
start = models.DateField(verbose_name=_('start')) start = models.DateField(verbose_name=_('start'))
end = models.DateField(verbose_name=_('end')) end = models.DateField(verbose_name=_('end'))
...@@ -171,13 +176,15 @@ class Semester(models.Model): ...@@ -171,13 +176,15 @@ class Semester(models.Model):
class Group(models.Model): class Group(models.Model):
name = models.CharField(max_length=80, verbose_name=_('name')) name = models.CharField(max_length=80, verbose_name=_('name'))
course = models.ForeignKey('Course', null=True, blank=True, course = models.ForeignKey('Course', null=True, blank=True,
verbose_name=_('course')) verbose_name=_('course'))
semester = models.ForeignKey('Semester', null=False, blank=False, semester = models.ForeignKey('Semester', null=False, blank=False,
verbose_name=_('semester')) verbose_name=_('semester'))
owners = models.ManyToManyField(Person, blank=True, null=True, 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, members = models.ManyToManyField(Person, blank=True, null=True,
related_name='course_groups', verbose_name=_('members')) related_name='course_groups',
verbose_name=_('members'))
class Meta: class Meta:
unique_together = (('name', 'course', 'semester', ), ) unique_together = (('name', 'course', 'semester', ), )
......
from datetime import datetime, timedelta from datetime import datetime, timedelta
from django.test import TestCase 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 django.core.exceptions import ValidationError
from ..models import create_user_profile, Person, Course, Semester, Group from ..models import create_user_profile, Person, Course, Semester, Group
class CreateUserProfileTestCase(TestCase): class CreateUserProfileTestCase(TestCase):
def setUp(self): def setUp(self):
self.user = User(username="testuser", password="testpass", 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() Person.objects.all().delete()
with self.assertRaises(Person.DoesNotExist): with self.assertRaises(Person.DoesNotExist):
Person.objects.get(code=self.user.username) Person.objects.get(code=self.user.username)
...@@ -23,6 +25,7 @@ class CreateUserProfileTestCase(TestCase): ...@@ -23,6 +25,7 @@ class CreateUserProfileTestCase(TestCase):
create_user_profile(self.user.__class__, self.user, True) create_user_profile(self.user.__class__, self.user, True)
self.assertIsNotNone(Person.objects.get(code=self.user.username)) self.assertIsNotNone(Person.objects.get(code=self.user.username))
class PersonTestCase(TestCase): class PersonTestCase(TestCase):
"""Test 'static' Person facts.""" """Test 'static' Person facts."""
def test_language_code_in_choices(self): def test_language_code_in_choices(self):
...@@ -32,11 +35,13 @@ class PersonTestCase(TestCase): ...@@ -32,11 +35,13 @@ class PersonTestCase(TestCase):
choice_codes = [code for (code, _) in language_field.choices] choice_codes = [code for (code, _) in language_field.choices]
self.assertIn(language_field.default, choice_codes) self.assertIn(language_field.default, choice_codes)
class PersonWithUserTestCase(TestCase): class PersonWithUserTestCase(TestCase):
"""Test Person entities which have their user attribute set.""" """Test Person entities which have their user attribute set."""
def setUp(self): def setUp(self):
self.user = User(username="testuser", password="testpass", 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() Person.objects.all().delete()
self.person = Person.objects.create(code='testcode', user=self.user) self.person = Person.objects.create(code='testcode', user=self.user)
...@@ -60,6 +65,7 @@ class PersonWithUserTestCase(TestCase): ...@@ -60,6 +65,7 @@ class PersonWithUserTestCase(TestCase):
self.person.user.last_name = None self.person.user.last_name = None
self.assertIsNotNone(self.person.__unicode__()) self.assertIsNotNone(self.person.__unicode__())
class PersonWithoutUserTestCase(TestCase): class PersonWithoutUserTestCase(TestCase):
"""Test Person entities which doesn't have their user attribute set.""" """Test Person entities which doesn't have their user attribute set."""
def setUp(self): def setUp(self):
...@@ -78,6 +84,7 @@ class PersonWithoutUserTestCase(TestCase): ...@@ -78,6 +84,7 @@ class PersonWithoutUserTestCase(TestCase):
def test_unicode(self): def test_unicode(self):
self.assertIsNotNone(self.person.__unicode__()) self.assertIsNotNone(self.person.__unicode__())
class CourseTestCase(TestCase): class CourseTestCase(TestCase):
def setUp(self): def setUp(self):
now = datetime.now() now = datetime.now()
...@@ -85,10 +92,10 @@ class CourseTestCase(TestCase): ...@@ -85,10 +92,10 @@ class CourseTestCase(TestCase):
delta = timedelta(weeks=7) delta = timedelta(weeks=7)
self.testperson1 = Person.objects.create(code="testperson1") self.testperson1 = Person.objects.create(code="testperson1")
self.testperson2 = Person.objects.create(code="testperson2") self.testperson2 = Person.objects.create(code="testperson2")
self.testsemester = Semester.objects.create(name="testsemester", self.testsemester = Semester.objects.create(
start=date-delta, end=date+delta) name="testsemester", start=date-delta, end=date+delta)
self.testcourse = Course.objects.create(code="testcode", self.testcourse = Course.objects.create(
name="testname", short_name="tn") code="testcode", name="testname", short_name="tn")
self.testcourse.owners.add(self.testperson1, self.testperson2) self.testcourse.owners.add(self.testperson1, self.testperson2)
def test_get_or_create_default_group(self): def test_get_or_create_default_group(self):
...@@ -118,18 +125,19 @@ class CourseTestCase(TestCase): ...@@ -118,18 +125,19 @@ class CourseTestCase(TestCase):
self.testcourse.short_name = None self.testcourse.short_name = None
self.assertIsNotNone(self.testcourse.short()) self.assertIsNotNone(self.testcourse.short())
class SemesterTestCase(TestCase): class SemesterTestCase(TestCase):
def setUp(self): def setUp(self):
now = datetime.now() now = datetime.now()
date = now.date() date = now.date()
self.now = now self.now = now
delta = timedelta(weeks=7) delta = timedelta(weeks=7)
self.last_semester = Semester.objects.create(name="testsem1", self.last_semester = Semester.objects.create(
start=date-3*delta, end=date-delta) name="testsem1", start=date-3*delta, end=date-delta)
self.current_semester = Semester.objects.create(name="testsem2", self.current_semester = Semester.objects.create(
start=date-delta, end=date+delta) name="testsem2", start=date-delta, end=date+delta)
self.next_semester = Semester.objects.create(name="testsem3", self.next_semester = Semester.objects.create(
start=date+delta, end=date+3*delta) name="testsem3", start=date+delta, end=date+3*delta)
def test_is_on(self): def test_is_on(self):
self.assertFalse(self.last_semester.is_on(self.now)) self.assertFalse(self.last_semester.is_on(self.now))
...@@ -146,16 +154,17 @@ class SemesterTestCase(TestCase): ...@@ -146,16 +154,17 @@ class SemesterTestCase(TestCase):
def test_unicode(self): def test_unicode(self):
self.current_semester.__unicode__() self.current_semester.__unicode__()
class GroupTestCase(TestCase): class GroupTestCase(TestCase):
def setUp(self): def setUp(self):
date = datetime.now().date() date = datetime.now().date()
delta = timedelta(weeks=7) delta = timedelta(weeks=7)
semester = Semester.objects.create(name="testsem", semester = Semester.objects.create(
start=date-delta, end=date+delta) name="testsem", start=date-delta, end=date+delta)
self.testcourse = Course.objects.create(code="testcode", self.testcourse = Course.objects.create(
name="testname", short_name="tn") code="testcode", name="testname", short_name="tn")
self.testgroup = Group.objects.create(name="testgrp", self.testgroup = Group.objects.create(
semester=semester, course=self.testcourse) name="testgrp", semester=semester, course=self.testcourse)
def test_owner_list(self): def test_owner_list(self):
self.assertIsNotNone(self.testgroup.owner_list()) self.assertIsNotNone(self.testgroup.owner_list())
...@@ -184,4 +193,3 @@ class GroupTestCase(TestCase): ...@@ -184,4 +193,3 @@ class GroupTestCase(TestCase):
def test_get_absolute_url(self): def test_get_absolute_url(self):
self.assertIsNotNone(self.testgroup.get_absolute_url()) self.assertIsNotNone(self.testgroup.get_absolute_url())
from datetime import datetime
from itertools import chain from itertools import chain
from django.conf import settings from django.conf import settings
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User, Group as AGroup from django.contrib.auth.models import User, Group as AGroup
from django.contrib import messages from django.contrib import messages
from django.core import signing
from django.core.exceptions import PermissionDenied, ValidationError from django.core.exceptions import PermissionDenied, ValidationError
from django.core.mail import mail_managers, send_mail
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db import transaction from django.http import HttpResponse
from django.forms import ModelForm, Textarea from django.shortcuts import (render_to_response, get_object_or_404,
from django.http import Http404 redirect)
from django.shortcuts import (render, render_to_response, get_object_or_404,
redirect)
from django.template import RequestContext 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.http import is_safe_url
from django.utils.translation import get_language as lang
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import ensure_csrf_cookie from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.decorators.http import * from one.models import Template, UserCloudDetails
from django.views.generic import * from school.models import Person, Semester, Course, Group
from one.models import *
from school.models import *
import django.contrib.auth as auth import django.contrib.auth as auth
import logging import logging
import json import json
import re import re
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
neptun_re = re.compile('^[a-zA-Z][a-zA-Z0-9]{5}$') neptun_re = re.compile('^[a-zA-Z][a-zA-Z0-9]{5}$')
def logout(request): def logout(request):
auth.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 @ensure_csrf_cookie
...@@ -54,7 +47,9 @@ def login(request): ...@@ -54,7 +47,9 @@ def login(request):
try: try:
user.email = request.META['email'] user.email = request.META['email']
except KeyError: 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() raise PermissionDenied()
user.save() user.save()
p, created = Person.objects.get_or_create(code=user.username) p, created = Person.objects.get_or_create(code=user.username)
...@@ -76,12 +71,11 @@ def login(request): ...@@ -76,12 +71,11 @@ def login(request):
try: try:
g.members.add(p) g.members.add(p)
g.save() g.save()
messages.info(request, messages.info(request, _('Course "%s" added.') % g.course)
_('Course "%s" added.') % g.course)
logger.info('Django Course "%s" added.' % g.course) logger.info('Django Course "%s" added.' % g.course)
except Exception as e: except Exception as e:
messages.error(request, messages.error(request,
_('Failed to add course "%s".') % g.course) _('Failed to add course "%s".') % g.course)
logger.warning("Django ex %s" % e) logger.warning("Django ex %s" % e)
held = request.META['niifEduPersonHeldCourse'] held = request.META['niifEduPersonHeldCourse']
...@@ -95,10 +89,11 @@ def login(request): ...@@ -95,10 +89,11 @@ def login(request):
co.owners.add(p) co.owners.add(p)
g.owners.add(p) g.owners.add(p)
messages.info(request, messages.info(request,
_('Course "%s" ownership added.') % g.course) _('Course "%s" ownership added.') % g.course)
except Exception as e: except Exception as e:
messages.error(request, 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) logger.warning("Django ex %s" % e)
co.save() co.save()
g.save() g.save()
...@@ -119,7 +114,7 @@ def login(request): ...@@ -119,7 +114,7 @@ def login(request):
logger.info("Django affiliation group %s added to %s" % (a, p)) logger.info("Django affiliation group %s added to %s" % (a, p))
except Exception as e: except Exception as e:
logger.warning("Django FAILed to add affiliation group %s to %s." logger.warning("Django FAILed to add affiliation group %s to %s."
" Reason: %s" % (a, p, e)) " Reason: %s" % (a, p, e))
user.save() user.save()
p.save() p.save()
...@@ -158,7 +153,7 @@ def group_show(request, gid): ...@@ -158,7 +153,7 @@ def group_show(request, gid):
user = request.user user = request.user
group = get_object_or_404(Group, id=gid) 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: for t in mytemplates:
t.myshares = t.share_set.filter(group=group) t.myshares = t.share_set.filter(group=group)
...@@ -175,7 +170,7 @@ def group_show(request, gid): ...@@ -175,7 +170,7 @@ def group_show(request, gid):
'mytemplates': mytemplates, 'mytemplates': mytemplates,
'publictemplates': publictemplates, 'publictemplates': publictemplates,
'noshare': not has_share, 'noshare': not has_share,
'userdetails': UserCloudDetails.objects.get(user=request.user), 'userdetails': UserCloudDetails.objects.get(user=user),
'owners': group.owners.all(), '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