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.
......@@ -33,32 +34,33 @@ def create_user_profile(sender, instance, created, **kwargs):
p = Person.objects.create(code=instance.username)
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})
"\nReason: %(exception)s",
{"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):
"""Get the shares of the groups which the person owns."""
return one.models.Share.objects.filter(
group__in=self.owned_groups.all())
group__in=self.owned_groups.all())
def get_shares(self):
"""Get the shares of the groups which the person is a member of."""
return one.models.Share.objects.filter(
group__in=self.course_groups.all())
group__in=self.course_groups.all())
def short_name(self):
if self.user:
......@@ -74,8 +76,8 @@ class Person(models.Model):
if self.user.last_name and self.user.first_name:
# TRANSLATORS: full name format used in enumerations
return _("%(first)s %(last)s") % {
'first': self.user.first_name,
'last': self.user.last_name}
'first': self.user.first_name,
'last': self.user.last_name}
else:
return self.user.username
else:
......@@ -85,18 +87,20 @@ 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'))
verbose_name=_('course code'))
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,
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.'))
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.'))
owners = models.ManyToManyField(Person, blank=True, null=True,
verbose_name=_('owners'))
verbose_name=_('owners'))
class Meta:
verbose_name = _('course')
......@@ -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()
......@@ -143,7 +148,7 @@ class Course(models.Model):
class Semester(models.Model):
name = models.CharField(max_length=20, unique=True, null=False,
verbose_name=_('name'))
verbose_name=_('name'))
start = models.DateField(verbose_name=_('start'))
end = models.DateField(verbose_name=_('end'))
......@@ -171,13 +176,15 @@ class Semester(models.Model):
class Group(models.Model):
name = models.CharField(max_length=80, verbose_name=_('name'))
course = models.ForeignKey('Course', null=True, blank=True,
verbose_name=_('course'))
verbose_name=_('course'))
semester = models.ForeignKey('Semester', null=False, blank=False,
verbose_name=_('semester'))
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())
......@@ -2,30 +2,30 @@ from datetime import datetime, timedelta
from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User, Group as AuthGroup
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.utils.datastructures import MultiValueDictKeyError
from ..models import create_user_profile, Person, Course, Semester, Group
from ..models import Person, Course, Semester, Group
from one.models import (UserCloudDetails, Disk, InstanceType, Network,
Template)
Template)
import json
class ViewTestCase(TestCase):
def setUp(self):
self.client = Client()
date = datetime.now().date()
delta = timedelta(weeks=7)
self.semester = Semester.objects.create(name="testsem",
start=date-delta, end=date+delta)
course1 = Course.objects.create(code='tccode1',
name='testcourse1', short_name='tc1')
course2 = Course.objects.create(code='tccode2',
name='testcourse2', short_name='tc2')
self.semester = Semester.objects.create(
name="testsem", start=date-delta, end=date+delta)
course1 = Course.objects.create(
code='tccode1', name='testcourse1', short_name='tc1')
course2 = Course.objects.create(
code='tccode2', name='testcourse2', short_name='tc2')
nonexistent_course_code1 = 'vacationspotscouting'
nonexistent_course_code2 = 'caringforunicorns'
affiliation1 = AuthGroup.objects.create(name='chalktrademanager')
self.group1 = Group.objects.create(name=affiliation1.name,
semester=self.semester, course=course1)
self.group1 = Group.objects.create(
name=affiliation1.name, semester=self.semester, course=course1)
self.http_headers = {
'niifPersonOrgID': 'ABUZER',
'niifEduPersonHeldCourse': ';'.join(
......@@ -37,7 +37,6 @@ class ViewTestCase(TestCase):
'email': 'test.user@testsite.hu',
'affiliation': ';'.join([affiliation1.name])}
def login(self, follow=False):
url = reverse('login')
resp = self.client.get(url, follow=follow, **self.http_headers)
......@@ -50,7 +49,6 @@ class ViewTestCase(TestCase):
pass
return resp
#
# school.views.logout
#
......@@ -58,7 +56,6 @@ class ViewTestCase(TestCase):
resp = self.client.get(reverse('logout'), follow=False)
self.assertEqual(302, resp.status_code)
#
# school.views.login
#
......@@ -66,7 +63,6 @@ class ViewTestCase(TestCase):
resp = self.login(follow=True)
self.assertEqual(200, resp.status_code)
def test_login_without_id(self):
del self.http_headers['niifPersonOrgID']
resp = self.login(follow=True)
......@@ -74,25 +70,21 @@ class ViewTestCase(TestCase):
(url, _) = resp.redirect_chain[0]
self.assertIn('/admin', url)
def test_login_without_email(self):
del self.http_headers['email']
resp = self.login(follow=True)
self.assertEqual(403, resp.status_code)
def test_login_without_affiliation(self):
del self.http_headers['affiliation']
resp = self.login(follow=True)
self.assertEqual(200, resp.status_code)
def test_login_without_group_for_affiliation(self):
self.group1.delete()
resp = self.login(follow=True)
self.assertEqual(200, resp.status_code)
#
# school.views.language
#
......@@ -107,7 +99,6 @@ class ViewTestCase(TestCase):
p = Person.objects.get(user=self.user)
self.assertEqual(lang, p.language)
def test_language_with_invalid_parameter(self):
self.login()
lang_before = Person.objects.get(user=self.user).language
......@@ -117,8 +108,7 @@ class ViewTestCase(TestCase):
resp = self.client.get(url, follow=False, **self.http_headers)
self.assertEqual(302, resp.status_code)
p = Person.objects.get(user=self.user)
self.assertEqual(lang_before, p.language) # language didn't change
self.assertEqual(lang_before, p.language) # language didn't change
def test_language_without_person_for_user(self):
self.login()
......@@ -129,7 +119,6 @@ class ViewTestCase(TestCase):
resp = self.client.get(url, follow=False, **self.http_headers)
self.assertEqual(302, resp.status_code)
#
# school.views.group_show
#
......@@ -140,18 +129,17 @@ class ViewTestCase(TestCase):
ucd.save()
disk = Disk.objects.create(name="testdsk1")
insttype = InstanceType.objects.create(name="testtype", CPU=4,
RAM=4096, credit=4)
RAM=4096, credit=4)
ntwrk = Network.objects.create(name="testntwrk", nat=False,
public=True)
tmplt = Template.objects.create(name="testtmplt1", disk=disk,
instance_type=insttype, network=ntwrk, owner=self.user,
state='READY')
public=True)
Template.objects.create(
name="testtmplt1", disk=disk, instance_type=insttype,
network=ntwrk, owner=self.user, state='READY')
gid = self.group1.id
url = reverse('school.views.group_show', kwargs={'gid': gid})
resp = self.client.get(url)
self.assertEqual(200, resp.status_code)
def test_group_show_with_nonexistent_group_id(self):
self.login()
gid = 1337 # this should be the ID of a non-existent group,
......@@ -160,7 +148,6 @@ class ViewTestCase(TestCase):
resp = self.client.get(url)
self.assertEqual(404, resp.status_code)
#
# school.views.group_new
#
......@@ -169,53 +156,44 @@ class ViewTestCase(TestCase):
url = reverse('school.views.group_new')
member1 = Person.objects.create(code="A1B2C3")
members = [member1]
data = {
'name': 'myNewGrp',
data = {'name': 'myNewGrp',
'semester': Semester.get_current().id,
'members': '\n'.join([m.code for m in members]),
}
resp = self.client.post(url, data)
'members': '\n'.join([m.code for m in members])}
self.client.post(url, data)
group = Group.objects.get(name=data['name'])
self.assertEqual(Semester.get_current(), group.semester)
for member in members:
self.assertIn(member, group.members.all())
def test_group_new_without_members(self):
self.login()
url = reverse('school.views.group_new')
data = {
'name': 'myNewGrp',
data = {'name': 'myNewGrp',
'semester': Semester.get_current().id,
'members': '',
}
resp = self.client.post(url, data)
'members': ''}
self.client.post(url, data)
group = Group.objects.get(name=data['name'])
self.assertEqual(Semester.get_current(), group.semester)
self.assertFalse(group.members.exists())
def test_group_new_with_invalid_neptun(self):
self.login()
url = reverse('school.views.group_new')
data = {
'name': 'myNewGrp',
data = {'name': 'myNewGrp',
'semester': Semester.get_current().id,
'members': '1ABC123', # invalid neptun
}
resp = self.client.post(url, data)
'members': '1ABC123'} # invalid neptun
self.client.post(url, data)
self.assertFalse(Group.objects.filter(name=data['name']).exists())
#
# school.views.group_ajax_add_new_member
#
def test_group_ajax_add_new_member(self):
self.login()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
url = reverse('school.views.group_ajax_add_new_member',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
new_member = Person.objects.get(user=self.user)
data = {'neptun': new_member.code}
resp = self.client.post(url, data)
......@@ -223,25 +201,23 @@ class ViewTestCase(TestCase):
group = Group.objects.get(id=group.id)
self.assertIn(new_member, group.members.all())
def test_group_ajax_add_new_member_with_nonexistent_group_id(self):
self.login()
gid = 1337 # this should be the ID of a non-existent group,
Group.objects.filter(id=gid).delete() # so if it exists, delete it!
url = reverse('school.views.group_ajax_add_new_member',
kwargs={'gid': gid})
kwargs={'gid': gid})
new_member = Person.objects.get(user=self.user)
data = {'neptun': new_member.code}
resp = self.client.post(url, data)
self.assertEqual(404, resp.status_code)
def test_group_ajax_add_new_member_without_neptun(self):
self.login()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
url = reverse('school.views.group_ajax_add_new_member',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
new_member = Person.objects.get(user=self.user)
data = {}
with self.assertRaises(MultiValueDictKeyError):
......@@ -249,13 +225,12 @@ class ViewTestCase(TestCase):
group = Group.objects.get(id=group.id)
self.assertNotIn(new_member, group.members.all())
def test_group_ajax_add_new_member_with_invalid_neptun(self):
self.login()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
url = reverse('school.views.group_ajax_add_new_member',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
new_member = Person.objects.get(user=self.user)
self.assertNotIn(new_member, group.members.all())
data = {'neptun': '1' + new_member.code}
......@@ -266,13 +241,12 @@ class ViewTestCase(TestCase):
group = Group.objects.get(id=group.id)
self.assertNotIn(new_member, group.members.all())
def test_group_ajax_add_new_member_with_nonexistent_member(self):
self.login()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
url = reverse('school.views.group_ajax_add_new_member',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
new_member_code = 'ZXY012' # this should be the ID of a
# non-existent person, so if it exists,
Person.objects.filter(code=new_member_code).delete() # delete it!
......@@ -284,63 +258,59 @@ class ViewTestCase(TestCase):
group = Group.objects.get(id=group.id)
self.assertIn(new_member, group.members.all())
#
# school.views.group_ajax_remove_member
#
def test_group_ajax_remove_member(self):
self.login()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
member = Person.objects.get(user=self.user)
group.members.add(member)
group.save()
url = reverse('school.views.group_ajax_remove_member',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
data = {'neptun': member.code}
resp = self.client.post(url, data)
self.assertEqual(200, resp.status_code)
group = Group.objects.get(id=group.id)
self.assertNotIn(member, group.members.all())
def test_group_ajax_remove_member_with_nonexistent_group_id(self):
self.login()
gid = 1337 # this should be the ID of a non-existent group,
Group.objects.filter(id=gid).delete() # so if it exists, delete it!
member = Person.objects.get(user=self.user)
url = reverse('school.views.group_ajax_remove_member',
kwargs={'gid': gid})
kwargs={'gid': gid})
data = {'neptun': member.code}
resp = self.client.post(url, data)
self.assertEqual(404, resp.status_code)
def test_group_ajax_remove_member_without_neptun(self):
self.login()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
member = Person.objects.get(user=self.user)
group.members.add(member)
group.save()
url = reverse('school.views.group_ajax_remove_member',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
data = {}
with self.assertRaises(MultiValueDictKeyError):
self.client.post(url, data)
group = Group.objects.get(id=group.id)
self.assertIn(member, group.members.all())
def test_group_ajax_remove_member_with_invalid_neptun(self):
self.login()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
member = Person.objects.get(user=self.user)
group.members.add(member)
group.save()
url = reverse('school.views.group_ajax_remove_member',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
data = {'neptun': '1' + member.code} # invalid Neptun code
resp = self.client.post(url, data)
self.assertEqual(200, resp.status_code)
......@@ -349,36 +319,33 @@ class ViewTestCase(TestCase):
group = Group.objects.get(id=group.id)
self.assertIn(member, group.members.all())
def test_group_ajax_remove_member_with_nonexistent_member(self):
self.login()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
member_code = 'ZXY012' # this should be the ID of a non-existent
# person, so if it exists,
Person.objects.filter(code=member_code).delete() # delete it!
url = reverse('school.views.group_ajax_remove_member',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
data = {'neptun': member_code}
with self.assertRaises(Person.DoesNotExist):
self.client.post(url, data)
self.assertFalse(Person.objects.filter(code=member_code).exists())
#
# school.views.group_ajax_delete
#
def test_group_ajax_delete(self):
self.login()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
url = reverse('school.views.group_ajax_delete')
data = {'gid': group.id}
resp = self.client.post(url, data)
self.assertEqual(200, resp.status_code)
self.assertFalse(Group.objects.filter(id=group.id).exists())
def test_group_ajax_delete_without_gid(self):
self.login()
url = reverse('school.views.group_ajax_delete')
......@@ -386,7 +353,6 @@ class ViewTestCase(TestCase):
with self.assertRaises(MultiValueDictKeyError):
self.client.post(url, data)
def test_group_ajax_delete_with_nonexistent_group_id(self):
self.login()
gid = 1337 # this should be the ID of a non-existent group,
......@@ -396,7 +362,6 @@ class ViewTestCase(TestCase):
resp = self.client.post(url, data)
self.assertEqual(404, resp.status_code)
#
# school.views.group_ajax_owner_autocomplete
#
......@@ -412,7 +377,6 @@ class ViewTestCase(TestCase):
'neptun': self.user.username}
self.assertIn(user_data, content)
def test_group_ajax_owner_autocomplete_without_query(self):
self.login()
url = reverse('school.views.group_ajax_owner_autocomplete')
......@@ -420,7 +384,6 @@ class ViewTestCase(TestCase):
with self.assertRaises(MultiValueDictKeyError):
self.client.post(url, data)
#
# school.views.group_ajax_add_new_owner
#
......@@ -429,12 +392,12 @@ class ViewTestCase(TestCase):
user_details = UserCloudDetails.objects.get(user=self.user)
user_details.share_quota = 10
user_details.save()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
new_owner = Person.objects.get(code=self.user.username)
self.assertNotIn(new_owner, group.owners.all())
url = reverse('school.views.group_ajax_add_new_owner',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
data = {'neptun': new_owner.code}
resp = self.client.post(url, data)
self.assertEqual(200, resp.status_code)
......@@ -443,18 +406,17 @@ class ViewTestCase(TestCase):
group = Group.objects.get(id=group.id)
self.assertIn(new_owner, group.owners.all())
def test_group_ajax_add_new_owner_without_enough_share_quota(self):
self.login()
user_details = UserCloudDetails.objects.get(user=self.user)
user_details.share_quota = 0
user_details.save()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
new_owner = Person.objects.get(code=self.user.username)
self.assertNotIn(new_owner, group.owners.all())
url = reverse('school.views.group_ajax_add_new_owner',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
data = {'neptun': new_owner.code}
resp = self.client.post(url, data)
self.assertEqual(200, resp.status_code)
......@@ -463,7 +425,6 @@ class ViewTestCase(TestCase):
group = Group.objects.get(id=group.id)
self.assertNotIn(new_owner, group.owners.all())
def test_group_ajax_add_new_owner_with_nonexistent_group_id(self):
self.login()
user_details = UserCloudDetails.objects.get(user=self.user)
......@@ -473,7 +434,7 @@ class ViewTestCase(TestCase):
Group.objects.filter(id=gid).delete() # so if it exists, delete it!
new_owner = Person.objects.get(code=self.user.username)
url = reverse('school.views.group_ajax_add_new_owner',
kwargs={'gid': gid})
kwargs={'gid': gid})
data = {'neptun': new_owner.code}
resp = self.client.post(url, data)
self.assertEqual(404, resp.status_code)
......@@ -483,30 +444,29 @@ class ViewTestCase(TestCase):
user_details = UserCloudDetails.objects.get(user=self.user)
user_details.share_quota = 10
user_details.save()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
new_owner = Person.objects.get(code=self.user.username)
self.assertNotIn(new_owner, group.owners.all())
url = reverse('school.views.group_ajax_add_new_owner',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
data = {}
with self.assertRaises(MultiValueDictKeyError):
self.client.post(url, data)
group = Group.objects.get(id=group.id)
self.assertNotIn(new_owner, group.owners.all())
def test_group_ajax_add_new_owner_with_invalid_neptun(self):
self.login()
user_details = UserCloudDetails.objects.get(user=self.user)
user_details.share_quota = 10
user_details.save()
group = Group.objects.create(name="mytestgroup",
semester=Semester.get_current())
group = Group.objects.create(
name="mytestgroup", semester=Semester.get_current())
new_owner = Person.objects.get(code=self.user.username)
self.assertNotIn(new_owner, group.owners.all())
url = reverse('school.views.group_ajax_add_new_owner',
kwargs={'gid': group.id})
kwargs={'gid': group.id})
data = {'neptun': '1' + new_owner.code}
resp = self.client.post(url, data)
self.assertEqual(200, resp.status_code)
......
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,
redirect)
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,12 +71,11 @@ 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,
_('Failed to add course "%s".') % g.course)
_('Failed to add course "%s".') % g.course)
logger.warning("Django ex %s" % e)
held = request.META['niifEduPersonHeldCourse']
......@@ -95,10 +89,11 @@ def login(request):
co.owners.add(p)
g.owners.add(p)
messages.info(request,
_('Course "%s" ownership added.') % g.course)
_('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()
......@@ -119,7 +114,7 @@ def login(request):
logger.info("Django affiliation group %s added to %s" % (a, p))
except Exception as e:
logger.warning("Django FAILed to add affiliation group %s to %s."
" Reason: %s" % (a, p, e))
" Reason: %s" % (a, p, e))
user.save()
p.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