Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2531f8c6
authored
Jul 02, 2013
by
Dudás Ádám
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
school: improve conformance of source
parent
6f6b9ee2
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
113 additions
and
143 deletions
+113
-143
school/admin.py
+5
-5
school/models.py
+16
-9
school/tests/test_models.py
+28
-20
school/tests/test_views.py
+48
-88
school/views.py
+16
-21
No files found.
school/admin.py
View file @
2531f8c6
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
)
school/models.py
View file @
2531f8c6
...
...
@@ -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"
"
\n
Reason:
%(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'
,
),
)
...
...
school/tests/test_models.py
View file @
2531f8c6
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
())
school/tests/test_views.py
View file @
2531f8c6
...
...
@@ -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
)
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
...
...
@@ -119,7 +110,6 @@ class ViewTestCase(TestCase):
p
=
Person
.
objects
.
get
(
user
=
self
.
user
)
self
.
assertEqual
(
lang_before
,
p
.
language
)
# language didn't change
def
test_language_without_person_for_user
(
self
):
self
.
login
()
Person
.
objects
.
get
(
user
=
self
.
user
)
.
delete
()
...
...
@@ -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
#
...
...
@@ -143,15 +132,14 @@ class ViewTestCase(TestCase):
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'
)
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,51 +156,42 @@ 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
})
new_member
=
Person
.
objects
.
get
(
user
=
self
.
user
)
...
...
@@ -223,7 +201,6 @@ 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,
...
...
@@ -235,11 +212,10 @@ class ViewTestCase(TestCase):
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
})
new_member
=
Person
.
objects
.
get
(
user
=
self
.
user
)
...
...
@@ -249,11 +225,10 @@ 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
})
new_member
=
Person
.
objects
.
get
(
user
=
self
.
user
)
...
...
@@ -266,11 +241,10 @@ 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
})
new_member_code
=
'ZXY012'
# this should be the ID of a
...
...
@@ -284,14 +258,13 @@ 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
()
...
...
@@ -303,7 +276,6 @@ class ViewTestCase(TestCase):
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,
...
...
@@ -315,11 +287,10 @@ class ViewTestCase(TestCase):
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
()
...
...
@@ -331,11 +302,10 @@ class ViewTestCase(TestCase):
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
()
...
...
@@ -349,11 +319,10 @@ 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!
...
...
@@ -364,21 +333,19 @@ class ViewTestCase(TestCase):
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,8 +392,8 @@ 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'
,
...
...
@@ -443,14 +406,13 @@ 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'
,
...
...
@@ -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
)
...
...
@@ -483,8 +444,8 @@ 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'
,
...
...
@@ -495,14 +456,13 @@ 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_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'
,
...
...
school/views.py
View file @
2531f8c6
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
%3
a
%2
f
%2
fcloud.ik.bme.hu
%2
f'
)
url
=
'/Shibboleth.sso/Logout?return=https
%3
a
%2
f
%2
fcloud.ik.bme.hu
%2
f'
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
(),
}))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment