1
+ # -*- coding: utf-8 -*-
2
+
1
3
from tastypie .resources import ModelResource , ALL_WITH_RELATIONS
2
4
from tastypie import fields
3
5
from handball .models import *
4
6
from django .contrib .auth .models import User
5
7
from tastypie .authorization import DjangoAuthorization , Authorization
6
8
from tastypie .authentication import Authentication , ApiKeyAuthentication
7
9
from django .http import HttpResponse , HttpResponseBadRequest
10
+ from tastypie .http import HttpUnauthorized
8
11
from tastypie .serializers import Serializer
9
12
from tastypie .utils .mime import determine_format
10
13
from auth .api import UserResource
14
+ from django .core .mail import send_mail
11
15
12
16
13
17
class UnionResource (ModelResource ):
@@ -67,11 +71,40 @@ class Meta:
67
71
authentication = Authentication ()
68
72
69
73
74
+ class PersonResource (ModelResource ):
75
+ user = fields .OneToOneField (UserResource , 'user' , blank = True , null = True , related_name = 'handball_profile' )
76
+
77
+ class Meta :
78
+ queryset = Person .objects .all ()
79
+ authorization = Authorization ()
80
+ authentication = Authentication ()
81
+ excludes = ['activation_key' , 'key_expires' ]
82
+ filtering = {
83
+ 'user' : ALL_WITH_RELATIONS ,
84
+ # 'clubs': ALL_WITH_RELATIONS,
85
+ 'clubs_managed' : ALL_WITH_RELATIONS ,
86
+ # 'teams': ALL_WITH_RELATIONS,
87
+ 'teams_managed' : ALL_WITH_RELATIONS ,
88
+ # 'teams_coached': ALL_WITH_RELATIONS,
89
+ 'first_name' : ['exact' ],
90
+ 'last_name' : ['exact' ]
91
+ }
92
+ always_return_data = True
93
+
94
+ def dehydrate (self , bundle ):
95
+ bundle .data ['display_name' ] = str (bundle .obj )
96
+
97
+ bundle .data ['clubs' ] = []
98
+ resource = ClubResource ()
99
+ for membership in ClubMemberRelation .objects .filter (member = bundle .obj ):
100
+ clubBundle = resource .build_bundle (obj = membership .club , request = bundle .request )
101
+ bundle .data ['clubs' ].append (resource .full_dehydrate (clubBundle ))
102
+
103
+ return bundle
104
+
105
+
70
106
class ClubResource (ModelResource ):
71
107
district = fields .ForeignKey (DistrictResource , 'district' , full = True )
72
- # teams = fields.ToManyField('handball.api.TeamResource', 'teams')
73
- # members = fields.ToManyField('handball.api.PersonResource', 'members', blank=True)
74
- managers = fields .ToManyField ('handball.api.PersonResource' , 'managers' , blank = True )
75
108
76
109
class Meta :
77
110
queryset = Club .objects .all ()
@@ -83,35 +116,39 @@ class Meta:
83
116
'managers' : ALL_WITH_RELATIONS
84
117
}
85
118
86
- def obj_create (self , bundle , request = None , ** kwargs ):
87
- # The user to create a club becomes its first manager (for lack of other people)
88
- bundle .data ['managers' ] = ['/handball/api/v1/person/' + str (request .user .get_profile ().id ) + '/' ]
89
- return super (ClubResource , self ).obj_create (bundle , request )
90
-
91
- def dehydrate (self , bundle ):
92
- del bundle .data ['managers' ]
93
- return bundle
119
+ # def obj_create(self, bundle, request=None, **kwargs):
120
+ # # The user to create a club becomes its first manager (for lack of other people)
121
+ # try:
122
+ # person = Person.objects.get(user=request.user)
123
+ # person_resource = PersonResource()
124
+ # bundle.data['managers'] = [person_resource.get_resource_uri(person)]
125
+ # except Person.DoesNotExist:
126
+ # pass
127
+ # return super(ClubResource, self).obj_create(bundle, request)
94
128
95
129
96
130
class TeamResource (ModelResource ):
97
131
club = fields .ForeignKey (ClubResource , 'club' , full = True )
98
- # players = fields.ManyToManyField('handball.api.PersonResource', 'players')
99
- # coaches = fields.ManyToManyField('handball.api.PersonResource', 'coaches')
100
- managers = fields .ManyToManyField ('handball.api.PersonResource' , 'managers' )
101
132
102
133
class Meta :
103
134
queryset = Team .objects .all ()
104
135
allowed_methods = ['get' , 'post' , 'put' ]
105
136
authorization = Authorization ()
106
137
authentication = Authentication ()
107
138
filtering = {
108
- 'club' : ALL_WITH_RELATIONS
139
+ 'club' : ALL_WITH_RELATIONS ,
140
+ 'managers' : ALL_WITH_RELATIONS
109
141
}
110
142
111
- def obj_create (self , bundle , request = None , ** kwargs ):
112
- # The user to create a team becomes its first manager (for lack of other people)
113
- bundle .data ['managers' ] = ['/handball/api/v1/person/' + str (request .user .get_profile ().id ) + '/' ]
114
- return super (TeamResource , self ).obj_create (bundle , request )
143
+ # def obj_create(self, bundle, request=None, **kwargs):
144
+ # # The user to create a team becomes its first manager (for lack of other people)
145
+ # try:
146
+ # person = Person.objects.get(user=request.user)
147
+ # person_resource = PersonResource()
148
+ # bundle.data['managers'] = [person_resource.get_resource_uri(person)]
149
+ # except Person.DoesNotExist:
150
+ # pass
151
+ # return super(TeamResource, self).obj_create(bundle, request)
115
152
116
153
def dehydrate (self , bundle ):
117
154
bundle .data ['display_name' ] = str (bundle .obj )
@@ -124,43 +161,6 @@ def dehydrate(self, bundle):
124
161
return bundle
125
162
126
163
127
- class PersonResource (ModelResource ):
128
- user = fields .OneToOneField (UserResource , 'user' , blank = True , null = True , related_name = 'handball_profile' )
129
- # clubs = fields.ManyToManyField(ClubResource, 'clubs', blank=True)
130
- clubs_managed = fields .ManyToManyField (ClubResource , 'clubs_managed' , blank = True )
131
- # teams = fields.ManyToManyField(TeamResource, 'teams', blank=True)
132
- teams_managed = fields .ManyToManyField (TeamResource , 'teams_managed' , blank = True )
133
- # teams_coached = fields.ManyToManyField(TeamResource, 'teams_coached', blank=True)
134
-
135
- class Meta :
136
- queryset = Person .objects .all ()
137
- authorization = Authorization ()
138
- authentication = Authentication ()
139
- excludes = ['activation_key' , 'key_expires' ]
140
- filtering = {
141
- 'user' : ALL_WITH_RELATIONS ,
142
- # 'clubs': ALL_WITH_RELATIONS,
143
- 'clubs_managed' : ALL_WITH_RELATIONS ,
144
- # 'teams': ALL_WITH_RELATIONS,
145
- 'teams_managed' : ALL_WITH_RELATIONS ,
146
- # 'teams_coached': ALL_WITH_RELATIONS,
147
- 'first_name' : ['exact' ],
148
- 'last_name' : ['exact' ]
149
- }
150
- always_return_data = True
151
-
152
- def dehydrate (self , bundle ):
153
- bundle .data ['display_name' ] = str (bundle .obj )
154
-
155
- bundle .data ['clubs' ] = []
156
- resource = ClubResource ()
157
- for membership in ClubMemberRelation .objects .filter (member = bundle .obj ):
158
- clubBundle = resource .build_bundle (obj = membership .club , request = bundle .request )
159
- bundle .data ['clubs' ].append (resource .full_dehydrate (clubBundle ))
160
-
161
- return bundle
162
-
163
-
164
164
class GameTypeResource (ModelResource ):
165
165
class Meta :
166
166
queryset = GameType .objects .all ()
@@ -262,6 +262,112 @@ class Meta:
262
262
}
263
263
264
264
265
+ class TeamCoachRelationResource (ModelResource ):
266
+ team = fields .ForeignKey (TeamResource , 'team' , full = True )
267
+ coach = fields .ForeignKey (PersonResource , 'coach' , full = True )
268
+
269
+ class Meta :
270
+ queryset = TeamCoachRelation .objects .all ()
271
+ authorization = Authorization ()
272
+ authentication = Authentication ()
273
+ always_return_data = True
274
+ filtering = {
275
+ 'coach' : ALL_WITH_RELATIONS ,
276
+ 'team' : ALL_WITH_RELATIONS
277
+ }
278
+
279
+
280
+ class ClubManagerRelationResource (ModelResource ):
281
+ club = fields .ForeignKey (ClubResource , 'club' , full = True )
282
+ manager = fields .ForeignKey (PersonResource , 'manager' , full = True )
283
+ appointed_by = fields .ForeignKey (UserResource , 'appointed_by' , null = True , blank = True )
284
+
285
+ class Meta :
286
+ queryset = ClubManagerRelation .objects .all ()
287
+ authorization = Authorization ()
288
+ authentication = Authentication ()
289
+ always_return_data = True
290
+ filtering = {
291
+ 'club' : ALL_WITH_RELATIONS ,
292
+ 'manager' : ALL_WITH_RELATIONS
293
+ }
294
+
295
+ def obj_create (self , bundle , request = None , ** kwargs ):
296
+ if request .user :
297
+ user_resource = UserResource ()
298
+ bundle .data ['appointed_by' ] = user_resource .get_resource_uri (request .user )
299
+
300
+ return super (ClubManagerRelationResource , self ).obj_create (bundle , request )
301
+
302
+
303
+ class TeamManagerRelationResource (ModelResource ):
304
+ team = fields .ForeignKey (TeamResource , 'team' , full = True )
305
+ manager = fields .ForeignKey (PersonResource , 'manager' , full = True )
306
+ appointed_by = fields .ForeignKey (UserResource , 'appointed_by' , null = True , blank = True )
307
+
308
+ class Meta :
309
+ queryset = TeamManagerRelation .objects .all ()
310
+ authorization = Authorization ()
311
+ authentication = Authentication ()
312
+ always_return_data = True
313
+ filtering = {
314
+ 'team' : ALL_WITH_RELATIONS ,
315
+ 'manager' : ALL_WITH_RELATIONS
316
+ }
317
+
318
+ def obj_create (self , bundle , request = None , ** kwargs ):
319
+ if request .user :
320
+ user_resource = UserResource ()
321
+ bundle .data ['appointed_by' ] = user_resource .get_resource_uri (request .user )
322
+
323
+ return super (TeamManagerRelationResource , self ).obj_create (bundle , request )
324
+
325
+
326
+ # class LeagueManagerRelationResource(ModelResource):
327
+ # league = fields.ForeignKey(LeagueResource, 'league', full=True)
328
+ # manager = fields.ForeignKey(PersonResource, 'manager', full=True)
329
+
330
+ # class Meta:
331
+ # queryset = LeagueManagerRelation.objects.all()
332
+ # authorization = Authorization()
333
+ # authentication = Authentication()
334
+ # always_return_data = True
335
+ # filtering = {
336
+ # 'league': ALL_WITH_RELATIONS,
337
+ # 'manager': ALL_WITH_RELATIONS
338
+ # }
339
+
340
+
341
+ # class DistrictManagerRelationResource(ModelResource):
342
+ # district = fields.ForeignKey(DistrictResource, 'club', full=True)
343
+ # manager = fields.ForeignKey(PersonResource, 'manager', full=True)
344
+
345
+ # class Meta:
346
+ # queryset = DistrictManagerRelation.objects.all()
347
+ # authorization = Authorization()
348
+ # authentication = Authentication()
349
+ # always_return_data = True
350
+ # filtering = {
351
+ # 'district': ALL_WITH_RELATIONS,
352
+ # 'manager': ALL_WITH_RELATIONS
353
+ # }
354
+
355
+
356
+ # class UnionManagerRelationResource(ModelResource):
357
+ # union = fields.ForeignKey(ClubResource, 'union', full=True)
358
+ # manager = fields.ForeignKey(PersonResource, 'manager', full=True)
359
+
360
+ # class Meta:
361
+ # queryset = ClubManagerRelation.objects.all()
362
+ # authorization = Authorization()
363
+ # authentication = Authentication()
364
+ # always_return_data = True
365
+ # filtering = {
366
+ # 'union': ALL_WITH_RELATIONS,
367
+ # 'manager': ALL_WITH_RELATIONS
368
+ # }
369
+
370
+
265
371
class SiteResource (ModelResource ):
266
372
class Meta :
267
373
queryset = Site .objects .all ()
@@ -300,3 +406,36 @@ def is_unique(request):
300
406
format = determine_format (request , serializer , default_format = 'application/json' )
301
407
302
408
return HttpResponse (serializer .serialize (data , format , {}))
409
+
410
+
411
+ def send_invitation (request ):
412
+ if request .user .is_authenticated () and request .user .is_active :
413
+ if 'email' in request .POST :
414
+ email = request .POST ['email' ]
415
+ else :
416
+ return HttpResponseBadRequest ('Mandatory email parameter not provided.' )
417
+
418
+ if 'message' in request .POST :
419
+ message = request .POST ['message' ]
420
+ else :
421
+ message = 'Tritt Score.it bei und sehe deine Handballergebnisse online!'
422
+
423
+ profile = None
424
+ if 'profile' in request .POST :
425
+ serializer = Serializer ()
426
+ profile = serializer .deserialize (request .POST ['profile' ])
427
+
428
+ subject = '{0} {1} lädt dich zu Score.it ein!' .format (request .user .first_name , request .user .last_name )
429
+
430
+ if profile :
431
+ profile_link = 'http://score-it.de/?a=invite&p={0}' .format (profile .id )
432
+ body = '{0} {1} hat ein Spielerprofil bei Score.it für dich erstellt. Melde dich jetzt bei Score.it an, um deine Handballergebnisse online abzurufen! Zum anmelden, klicke einfach folgenden Link: {3}' .format (request .user .first_name , request .user .last_name , profile_link )
433
+ else :
434
+ body = '{0} {1} hat dir eine Einladung zu der Sportplatform Score.it geschickt:<br>{2}Um dich anzumelden, besuche einfach http://score-it.de/!' .format (request .user .first_name , request .user .last_name , message )
435
+
436
+ sender = 'noreply@score-it.de'
437
+ recipients = [email ]
438
+ send_mail (subject , body , sender , recipients )
439
+ return HttpResponse ('' )
440
+ else :
441
+ return HttpUnauthorized ('Authentication through active user required.' )
0 commit comments