<s> from django . contrib . auth . models import User <EOL> from django . contrib . sites . models import Site <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from tastypie . authentication import BasicAuthentication <EOL> from tastypie . authorization import Authorization <EOL> from tastypie . exceptions import Unauthorized <EOL> from tastypie import fields <EOL> from tastypie . resources import ModelResource <EOL> from . . models import AuthorProfile , Article <EOL> class PerUserAuthorization ( Authorization ) : <EOL> def create_list ( self , object_list , bundle ) : <EOL> return object_list <EOL> def create_detail ( self , object_list , bundle ) : <EOL> return True <EOL> def update_list ( self , object_list , bundle ) : <EOL> revised_list = [ ] <EOL> for article in object_list : <EOL> for profile in article . authors . all ( ) : <EOL> if bundle . request . user . pk == profile . user . pk : <EOL> revised_list . append ( article ) <EOL> return revised_list <EOL> def update_detail ( self , object_list , bundle ) : <EOL> if getattr ( bundle . obj , '<STR_LIT>' , None ) : <EOL> try : <EOL> object_list . get ( pk = bundle . obj . pk ) <EOL> for profile in bundle . obj . authors . all ( ) : <EOL> if bundle . request . user . pk == profile . user . pk : <EOL> return True <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> for profile in bundle . data [ '<STR_LIT>' ] : <EOL> if hasattr ( profile , '<STR_LIT>' ) : <EOL> if bundle . request . user . pk == profile [ '<STR_LIT:user>' ] . get ( '<STR_LIT:id>' ) : <EOL> return True <EOL> else : <EOL> if bundle . request . user . pk == profile . strip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] : <EOL> return True <EOL> raise Unauthorized ( "<STR_LIT>" ) <EOL> def delete_list ( self , object_list , bundle ) : <EOL> raise Unauthorized ( "<STR_LIT>" ) <EOL> def delete_detail ( self , object_list , bundle ) : <EOL> raise Unauthorized ( "<STR_LIT>" ) <EOL> class UserResource ( ModelResource ) : <EOL> class Meta : <EOL> queryset = User . objects . all ( ) <EOL> authentication = BasicAuthentication ( ) <EOL> authorization = Authorization ( ) <EOL> excludes = [ '<STR_LIT:email>' , '<STR_LIT:password>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class SiteResource ( ModelResource ) : <EOL> class Meta : <EOL> queryset = Site . objects . all ( ) <EOL> authentication = BasicAuthentication ( ) <EOL> authorization = Authorization ( ) <EOL> class AuthorProfileResource ( ModelResource ) : <EOL> user = fields . ToOneField ( UserResource , '<STR_LIT:user>' , full = True ) <EOL> sites = fields . ToManyField ( SiteResource , '<STR_LIT>' , related_name = '<STR_LIT>' , full = True ) <EOL> class Meta : <EOL> queryset = AuthorProfile . objects . all ( ) <EOL> authentication = BasicAuthentication ( ) <EOL> authorization = Authorization ( ) <EOL> class ArticleResource ( ModelResource ) : <EOL> authors = fields . ToManyField ( AuthorProfileResource , '<STR_LIT>' , related_name = '<STR_LIT>' , full = True ) <EOL> class Meta : <EOL> queryset = Article . objects . all ( ) <EOL> authentication = BasicAuthentication ( ) <EOL> authorization = PerUserAuthorization ( ) </s>
