Exemplo n.º 1
0
class GomeeResource(UserResourceBase):
    
    implements(interface.IGomeeResource)
    
    def __init__(self, identity, gomeeIdentity, service):
        UserResourceBase.__init__(self, identity, service)
        self.gomeeService = IGomeeService(service)
        self.gomeeIdentity = gomeeIdentity
        
    def user_check_GET(self, authUser, user, request):
        return defer.succeed(True)
    
    def user_auth_GET(self, authUser, user, request):
        
        def complete(gm):
            return defer.succeed({
                                  "id" : gm.id,
                                  "owner" : gm.owner.id,
                                  "lat" : gm.lat,
                                  "lng" : gm.lng,
                                  "address" : gm.address,
                                  "description" : gm.description,
                                  "type" : gm.type
                                  })
        
        d = self.gomeeService.get_gomee(self.gomeeIdentity)
        d.addCallback(complete)
        return d
    
    def user_check_PUT(self, authUser, user, request):
        return defer.succeed(authUser.id == user.id)
    
    def user_auth_PUT(self, authUser, user, request):
        
        def save_gomee(_):
            gomee = json.loads(request.content.read())
            return self.gomeeService.save_gomee(self.gomeeIdentity,
                                               gomee["lat"],
                                               gomee["lng"],
                                               str(gomee["caption"]),
                                               str(gomee["address"]),
                                               str(gomee["description"]),
                                               gomee["type"])
        
        def complete(gm):
            return defer.succeed({
                                  "id" : gm.id,
                                  "owner" : gm.owner.id,
                                  "lat" : gm.lat,
                                  "lng" : gm.lng,
                                  "address" : gm.address,
                                  "description" : gm.description,
                                  "type" : gm.type
                                  })
            
        d = defer.Deferred()
        
        d.addCallback(save_gomee)
        d.addCallback(complete)
        
        reactor.callLater(0, d.callback, None)
        return d
    
    def user_check_DELETE(self, authUser, user, request):
        return defer.succeed(authUser.id == user.id)
    
    def user_auth_DELETE(self, authUser, user, request):
        
        def del_gomee(_):
            return self.gomeeService.del_gomee(self.gomeeIdentity)
        
        d = defer.Deferred()
        
        d.addCallback(del_gomee)
        
        reactor.callLater(0, d.callback, None)
        return d
Exemplo n.º 2
0
class GomeeResource(UserResourceBase):
    
    implements(interface.IGomeeResource)
    
    def __init__(self, identity, gomeeIdentity, service):
        UserResourceBase.__init__(self, identity, service)
        self.gomeeService = IGomeeService(service)
        self.tagService = ITagService(service)
        self.gomeeIdentity = gomeeIdentity
        
    def user_check_GET(self, authUser, user, request):
        return defer.succeed(True)
    
    def user_auth_GET(self, authUser, user, request):
        
        def get_tags(gm):
            
            def complete(tags):
                return defer.succeed((gm, tags))
            
            d = self.tagService.get_gomee_tags(gm)
            d.addCallback(complete)
            return d
        
        def complete((gm, tags)):
            return defer.succeed({
                                  "id" : gm.id,
                                  "owner" : gm.owner.id,
                                  "lat" : gm.lat,
                                  "lng" : gm.lng,
                                  "caption" : gm.caption,
                                  "address" : gm.address,
                                  "description" : gm.description,
                                  "type" : gm.type,
                                  "common" : gm.common,
                                  "tags": map(lambda t: t.tag.name, tags)
                                  })
        
        d = self.gomeeService.get_gomee(self.gomeeIdentity)
        d.addCallback(get_tags)
        d.addCallback(complete)
        return d
    
    def user_check_PUT(self, authUser, user, request):
        return defer.succeed(authUser.login != "guest" and authUser.id == user.id)
    
    def user_auth_PUT(self, authUser, user, request):
        
        def save_gomee(_):
            return self.gomeeService.save_gomee(self.gomeeIdentity, data)
        
        def save_tags(gm):
            
            def complete(tags):
                return defer.succeed((gm, tags))
            
            d = self.tagService.set_gomee_tags(gm, data)
            d.addCallback(complete)
            return d
            
        def complete((gm, tags)):
            return defer.succeed({
                                  "id" : gm.id,
                                  "owner" : gm.owner.id,
                                  "lat" : gm.lat,
                                  "lng" : gm.lng,
                                  "address" : gm.address,
                                  "description" : gm.description,
                                  "type" : gm.type,
                                  "common" : gm.common,
                                  "tags" : map(lambda t: t.tag.name, tags)
                                  })
        
        data = json.loads(request.content.read())
            
        d = defer.Deferred()
        
        d.addCallback(save_gomee)
        d.addCallback(save_tags)
        d.addCallback(complete)
        
        reactor.callLater(0, d.callback, None)
        return d
    
    def user_check_DELETE(self, authUser, user, request):
        return defer.succeed(authUser.login != "guest" and authUser.id == user.id)
    
    def user_auth_DELETE(self, authUser, user, request):
        
        def del_gomee(_):
            return self.gomeeService.del_gomee(self.gomeeIdentity)
        
        d = defer.Deferred()
        
        d.addCallback(del_gomee)
        
        reactor.callLater(0, d.callback, None)
        return d