Esempio n. 1
0
 def read(self, model, pk=None, **kwargs):
     """
     Read the database for a model instance by id.
     :param model: The model name to look for instances of.
     :type model: str
     :param pk: The primary key of the model instance to attempt to read.
     :param kwargs:
     :return: Collection representation of resource(s) retrieved from the database.
     """
     # letting self.models[model] raise a KeyError on purpose, see above
     response = Collection(
         href=self.app.config.get('API_ROOT'),
         template=self.models[model].get_collection_template())
     session = sessionmaker(bind=self.database)
     if pk is None:
         instances = session.query(self.models[model])
         if kwargs.get('order_by'):
             instances = instances.order_by(
                 getattr(self.models[model], kwargs['order_by']))
     else:
         instances = session.query(self.models[model]).get(pk)
         if instances is None:
             abort(404)
     for instance in instances:
         response.items.append(instance.get_collection_item())
     return response
Esempio n. 2
0
    def get(self):
        #get users from database
        users = g.con.get_users()

        template = {
            "data": [{
                'name': 'Username',
                'value': '',
                'prompt': ''
            }, {
                'name': 'Realname',
                'value': '',
                'prompt': ''
            }, {
                'name': 'Email',
                'value': '',
                'prompt': ''
            }]
        }

        links = [  #{"href": "www.sangz.com","rel": "home"},
            {
                "href": api.url_for(Songs),
                "rel": "Songs",
                'prompt': 'See the list of all songs'
            }, {
                "href": api.url_for(Playlist),
                "rel": "Playlist",
                "prompt": "See the current playlist"
            }, {
                "href": api.url_for(Chat),
                "rel": "Chat",
                "prompt": "See the conversation"
            }
        ]

        #links = {}
        collection = Collection(api.url_for(Users),
                                template=template,
                                links=links)
        collection.version = "1.0"
        for user in users:
            item = Item(api.url_for(User, userid=user[0]))
            item.data.append(Data("Id", user[0]))
            item.data.append(Data("nickname", user[1]))
            collection.items.append(item)

        string_data = str(collection)
        return Response(string_data,
                        200,
                        mimetype="application/vnd.collection+json" + ";" +
                        APIARY_PROFILES_URL)
Esempio n. 3
0
 def create(self, model, data, **kwargs):
     """
     Create a new instance of a model
     :param model: The model name to create an instance of
     :type model: str
     :param data: The data to provide to that instance, formatted as a Collection+JSON data array
     :type data: list
     :return: Collection representation of the created resource.
     """
     try:
         data = Template(data)
     except (TypeError, ValueError, IndexError):
         abort(400)
     # letting this raise a KeyError on purpose, flask returns HTTP 500 on python errors
     instance = self.models[model](data)
     session = sessionmaker(bind=self.database)
     session.add(instance)
     session.commit()
     return Collection(href=self.app.config.get('API_ROOT'),
                       items=[instance.get_collection_item()])
Esempio n. 4
0
    def update(self, model, data, pk=None, **kwargs):
        """
        Update a model instance in the database.
        :param model: The model name to look for an instance of.
        :param data: The data to provide to the instance, formatted as a Collection+JSON data array
        :param pk: The primary key of the model instance to modify.
        :param kwargs:
        :return: A Collection+JSON representation of the updated model instance.
        """
        try:
            data = Template(data)
        except (TypeError, ValueError, IndexError):
            abort(400)

        # letting self.models[model] raise a KeyError on purpose, see above
        instance = self.models[model].query.get_or_404(pk)
        instance.update(data)
        self.database.session.commit()
        return Collection(
            href=self.app.config.get('API_ROOT'),
            template=self.models[model].get_collection_template(),
            items=[instance.get_collection_item()])
Esempio n. 5
0
    def get(self):
        #connect to the db
        args = request.args
        try:
            user_id = int(args['user_id'])
        except (KeyError, ValueError):
            user_id = None

        # songs_db = g.con.get_songs() old non filtering db call
        songs_db = g.con.get_songs_filtered(user_id=user_id)

        links = [  #{"href": "www.sangz.com", "rel": "home"},
            {
                "href": api.url_for(Users),
                "rel": "Users",
                "prompt": "Get the list of all users"
            }, {
                "href": api.url_for(Playlist),
                "rel": "Playlist",
                "prompt": "See the current playlist"
            }, {
                "href": api.url_for(Chat),
                "rel": "Chat",
                "prompt": "See the conversation"
            }
        ]

        template = {
            "data":
            [{
                "prompt": "",
                "name": "user_id",
                "value": ""
            }, {
                "prompt": "",
                "name": "song_name",
                "value": ""
            }
             #{"prompt": "", "name":"media_location",
             #"value":""},
             #{"prompt": "", "name":"media_type",
             #"value":""},
             #{"prompt": "", "name":"artist_id",
             #"value":""},
             #{"prompt": "", "name":"album_id",
             #"value":""},
             #{"prompt": "", "name":"user_id",
             #"value":""}
             ]
        }
        collection = Collection(api.url_for(Songs),
                                template=template,
                                links=links)
        collection.version = "1.0"
        #create items
        print songs_db
        for song in songs_db:
            print song
            item = Item(api.url_for(Song, songid=song[0]))
            item.data.append(Data("ID", song[0]))
            item.data.append(Data("songname", song[1]))
            item.data.append(
                Data("uploader", api.url_for(User, userid=song[2])))
            #links to artist and album are unavailable because they are not implemented
            collection.items.append(item)

        string_data = str(collection)
        return Response(string_data,
                        200,
                        mimetype="application/vnd.collection+json" + ";" +
                        SANGZ_SONG_PROFILE)