1
- from tastypie .resources import ModelResource
1
+ from tastypie .resources import ModelResource , ALL , ALL_WITH_RELATIONS
2
2
from tastypie import fields
3
3
from handball .models import *
4
4
from django .contrib .auth .models import User
5
5
from tastypie .authorization import DjangoAuthorization , Authorization
6
- from tastypie .authentication import BasicAuthentication
6
+ from tastypie .authentication import BasicAuthentication , Authentication
7
7
8
8
9
9
class UnionResource (ModelResource ):
10
- clubs = fields .ToManyField ('handball.api.ClubResource' , 'clubs' , full = True )
10
+ # clubs = fields.ToManyField('handball.api.ClubResource', 'clubs', full=True)
11
+ managers = fields .ToManyField ('handball.api.PersonResource' , 'managers' )
11
12
12
13
class Meta :
13
14
queryset = Union .objects .all ()
14
- allowed_methods = ['get' , 'post' , 'put' ]
15
- authorization = DjangoAuthorization ()
15
+ allowed_methods = ['get' , 'post' , 'put' , 'patch' ]
16
+ authorization = Authorization ()
16
17
authentication = BasicAuthentication ()
18
+ filtering = {
19
+ 'name' : ('exact' )
20
+ }
17
21
22
+ def obj_create (self , bundle , request = None , ** kwargs ):
23
+ # The user to create a union becomes its first manager (for lack of other people)
24
+ bundle .data ['managers' ] = ['/api/v1/person/' + str (request .user .get_profile ().id ) + '/' ]
25
+ return super (UnionResource , self ).obj_create (bundle , request )
18
26
19
- class ClubResource (ModelResource ):
20
- union = fields .ForeignKey (UnionResource , 'union' )
21
- teams = fields .ToManyField ('handball.api.TeamResource' , 'teams' )
22
27
28
+ class LeagueResource (ModelResource ):
23
29
class Meta :
24
- queryset = Club .objects .all ()
30
+ queryset = Team .objects .all ()
25
31
allowed_methods = ['get' , 'post' , 'put' ]
26
- authorization = DjangoAuthorization ()
32
+ authentication = Authentication ()
33
+ authorization = Authorization ()
27
34
28
35
29
- class LeagueResource (ModelResource ):
36
+ class ClubResource (ModelResource ):
37
+ union = fields .ForeignKey (UnionResource , 'union' )
38
+ # teams = fields.ToManyField('handball.api.TeamResource', 'teams')
39
+
30
40
class Meta :
31
- queryset = Team .objects .all ()
41
+ queryset = Club .objects .all ()
32
42
allowed_methods = ['get' , 'post' , 'put' ]
33
- authorization = DjangoAuthorization ()
43
+ authorization = Authorization ()
44
+ authentication = Authentication ()
45
+ filtering = {
46
+ 'union' : ALL_WITH_RELATIONS
47
+ }
48
+
49
+ def obj_create (self , bundle , request = None , ** kwargs ):
50
+ # The user to create a club becomes its first manager (for lack of other people)
51
+ bundle .data ['managers' ] = ['/api/v1/person/' + str (request .user .get_profile ().id ) + '/' ]
52
+ return super (ClubResource , self ).obj_create (bundle , request )
34
53
35
54
36
55
class TeamResource (ModelResource ):
@@ -39,7 +58,14 @@ class TeamResource(ModelResource):
39
58
40
59
class Meta :
41
60
queryset = Team .objects .all ()
42
- authorization = DjangoAuthorization ()
61
+ allowed_methods = ['get' , 'post' , 'put' ]
62
+ authorization = Authorization ()
63
+ authentication = Authentication ()
64
+
65
+ def obj_create (self , bundle , request = None , ** kwargs ):
66
+ # The user to create a team becomes its first manager (for lack of other people)
67
+ bundle .data ['managers' ] = ['/api/v1/person/' + str (request .user .get_profile ().id ) + '/' ]
68
+ return super (TeamResource , self ).obj_create (bundle , request )
43
69
44
70
45
71
class UserResource (ModelResource ):
@@ -52,11 +78,12 @@ class Meta:
52
78
53
79
class PersonResource (ModelResource ):
54
80
user = fields .ForeignKey (UserResource , 'user' )
55
- teams = fields .ManyToManyField (TeamResource , 'teams' )
81
+ # teams = fields.ManyToManyField(TeamResource, 'teams')
56
82
57
83
class Meta :
58
84
queryset = Person .objects .all ()
59
85
authorization = DjangoAuthorization ()
86
+ excludes = ['activation_key' , 'key_expires' ]
60
87
61
88
62
89
class GameTypeResource (ModelResource ):
0 commit comments