Skip to content

Commit 37392cd

Browse files
committed
Some model and resource adjustments
1 parent 3e0a1a1 commit 37392cd

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

api.py

+42-15
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,55 @@
1-
from tastypie.resources import ModelResource
1+
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
22
from tastypie import fields
33
from handball.models import *
44
from django.contrib.auth.models import User
55
from tastypie.authorization import DjangoAuthorization, Authorization
6-
from tastypie.authentication import BasicAuthentication
6+
from tastypie.authentication import BasicAuthentication, Authentication
77

88

99
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')
1112

1213
class Meta:
1314
queryset = Union.objects.all()
14-
allowed_methods = ['get', 'post', 'put']
15-
authorization = DjangoAuthorization()
15+
allowed_methods = ['get', 'post', 'put', 'patch']
16+
authorization = Authorization()
1617
authentication = BasicAuthentication()
18+
filtering = {
19+
'name': ('exact')
20+
}
1721

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)
1826

19-
class ClubResource(ModelResource):
20-
union = fields.ForeignKey(UnionResource, 'union')
21-
teams = fields.ToManyField('handball.api.TeamResource', 'teams')
2227

28+
class LeagueResource(ModelResource):
2329
class Meta:
24-
queryset = Club.objects.all()
30+
queryset = Team.objects.all()
2531
allowed_methods = ['get', 'post', 'put']
26-
authorization = DjangoAuthorization()
32+
authentication = Authentication()
33+
authorization = Authorization()
2734

2835

29-
class LeagueResource(ModelResource):
36+
class ClubResource(ModelResource):
37+
union = fields.ForeignKey(UnionResource, 'union')
38+
# teams = fields.ToManyField('handball.api.TeamResource', 'teams')
39+
3040
class Meta:
31-
queryset = Team.objects.all()
41+
queryset = Club.objects.all()
3242
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)
3453

3554

3655
class TeamResource(ModelResource):
@@ -39,7 +58,14 @@ class TeamResource(ModelResource):
3958

4059
class Meta:
4160
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)
4369

4470

4571
class UserResource(ModelResource):
@@ -52,11 +78,12 @@ class Meta:
5278

5379
class PersonResource(ModelResource):
5480
user = fields.ForeignKey(UserResource, 'user')
55-
teams = fields.ManyToManyField(TeamResource, 'teams')
81+
# teams = fields.ManyToManyField(TeamResource, 'teams')
5682

5783
class Meta:
5884
queryset = Person.objects.all()
5985
authorization = DjangoAuthorization()
86+
excludes = ['activation_key', 'key_expires']
6087

6188

6289
class GameTypeResource(ModelResource):

models.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from django.contrib.auth.models import User
55
from django.db.models.signals import post_save
66
from django.utils.translation import ugettext as _
7+
from tastypie.models import create_api_key
78

89

910
class Person(models.Model):
10-
user = models.OneToOneField(User)
11+
user = models.OneToOneField(User, blank=True)
1112

1213
# Fields used for user activation after signup
13-
activation_key = models.CharField(max_length=40, null=True)
14-
key_expires = models.DateTimeField(null=True)
14+
activation_key = models.CharField(max_length=40, blank=True)
15+
key_expires = models.DateTimeField(null=True, blank=True)
1516

1617
first_name = models.CharField(max_length=50)
1718
last_name = models.CharField(max_length=50)
@@ -25,7 +26,7 @@ class Person(models.Model):
2526
is_player = models.BooleanField(default=True)
2627
is_exec = models.BooleanField(default=False)
2728

28-
teams = models.ManyToManyField('Team')
29+
teams = models.ManyToManyField('Team', blank=True)
2930

3031
def __unicode__(self):
3132
return self.first_name + ' ' + self.last_name
@@ -34,9 +35,9 @@ def __unicode__(self):
3435
class Team(models.Model):
3536
name = models.CharField(max_length=50)
3637

37-
league = models.ForeignKey('League')
38+
league = models.ForeignKey('League', related_name='league')
3839
club = models.ForeignKey('Club', related_name='teams')
39-
managers = models.ManyToManyField('Person')
40+
managers = models.ManyToManyField('Person', blank=True)
4041

4142
def __unicode__(self):
4243
return self.club.name + ' ' + self.name
@@ -46,7 +47,7 @@ class Club(models.Model):
4647
name = models.CharField(max_length=50)
4748

4849
union = models.ForeignKey('Union', related_name='clubs')
49-
managers = models.ManyToManyField('Person')
50+
managers = models.ManyToManyField('Person', blank=True)
5051

5152
def __unicode__(self):
5253
return self.name
@@ -63,7 +64,7 @@ def __unicode__(self):
6364
class Union(models.Model):
6465
name = models.CharField(max_length=50)
6566

66-
managers = models.ManyToManyField('Person')
67+
managers = models.ManyToManyField('Person', blank=True)
6768

6869
def __unicode__(self):
6970
return self.name
@@ -145,4 +146,7 @@ def create_user_profile(sender, instance, created, **kwargs):
145146
if created:
146147
Person.objects.create(user=instance, first_name=instance.first_name, last_name=instance.last_name)
147148

148-
post_save.connect(create_user_profile, sender=User)
149+
# post_save.connect(create_user_profile, sender=User)
150+
151+
# Create API key for a new user
152+
post_save.connect(create_api_key, sender=User)

0 commit comments

Comments
 (0)