def create(self, validated_data): appUser = AppUser.objects.create(name=validated_data['name'], bio=validated_data['bio']) appUser.save() photos_data = validated_data['photos'] for photo_data in photos_data: photo = Photo(url=photo_data['url']) photo.save() appUser.photos.add(photo) return appUser
def update(self, instance, validated_data): instance.bio = validated_data['bio'] instance.save() photos_data = validated_data['photos'] for photo_data in photos_data: photo = Photo(url=photo_data['url']) photo.save() instance.photos.add(photo) return instance
def search(self, query, category=None, orientation=None, page=1, per_page=10): """ Get a single page from a photo search. Optionally limit your search to a set of categories by supplying the category ID’s. Note: If supplying multiple category ID’s, the resulting photos will be those that match all of the given categories, not ones that match any category. :param query [string]: Search terms. :param category [string]: Category ID(‘s) to filter search. If multiple, comma-separated. (deprecated) :param orientation [string]: Filter search results by photo orientation. Valid values are landscape, portrait, and squarish. :param page [integer]: Page number to retrieve. (Optional; default: 1) :param per_page [integer]: Number of items per page. (Optional; default: 10) :return: [Array]: A single page of the curated Photo list. :raise UnsplashError: If the given orientation is not in the default orientation values. """ if orientation and orientation not in self.orientation_values: raise Exception() params = { "query": query, "category": category, "orientation": orientation, "page": page, "per_page": per_page } url = "/photos/search" result = self._get(url, params=params) return PhotoModel.parse_list(result)
def curated_photos(self, collection_id, page=1, per_page=10): """ Retrieve a collection’s curated photos. :param collection_id [string]: The collection’s ID. Required. :param page [integer]: Page number to retrieve. (Optional; default: 1) :param per_page [integer]: Number of items per page. (Optional; default: 10) :return: [Array]: A single page of the Photo list. """ url = "/collections/curated/%s/photos" % collection_id result = self._all(url, page=page, per_page=per_page) return PhotoModel.parse_list(result)
def unlike(self, photo_id): """ Remove a user’s like of a photo. Note: This action is idempotent; sending the DELETE request to a single photo multiple times has no additional effect. :param photo_id [string]: The photo’s ID. Required. :return: [Photo]: The Unsplash Photo. """ url = "/photos/%s/like" % photo_id result = self._delete(url) return PhotoModel.parse(result)
def photos(self, query, page=1, per_page=10): """ Get a single page of photo results for a query. :param query [string]: Search terms. :param page [integer]: Page number to retrieve. (Optional; default: 1) :param per_page [integer]: Number of items per page. (Optional; default: 10) :return: [dict]: {u'total': 0, u'total_pages': 0, u'results': [Photo]} """ url = "/search/photos" data = self._search(url, query, page=page, per_page=per_page) data["results"] = PhotoModel.parse_list(data.get("results")) return data
def like(self, photo_id): """ Like a photo on behalf of the logged-in user. This requires the 'write_likes' scope. Note: This action is idempotent; sending the POST request to a single photo multiple times has no additional effect. :param photo_id [string]: The photo’s ID. Required. :return: [Photo]: The Unsplash Photo. """ url = "/photos/%s/like" % photo_id result = self._post(url) return PhotoModel.parse(result)
def remove_photo(self, collection_id, photo_id): """ Remove a photo from one of the logged-in user’s collections. Requires the 'write_collections' scope. :param collection_id [string]: The collection’s ID. Required. :param photo_id [string]: The photo’s ID. Required. :return: [Tuple]: The Unsplash Collection and Photo """ url = "/collections/%s/remove" % collection_id data = {"collection_id": collection_id, "photo_id": photo_id} result = self._delete(url, data=data) or {} return CollectionModel.parse( result.get("collection")), PhotoModel.parse(result.get("photo"))
def add_photo(self, collection_id, photo_id): """ Add a photo to one of the logged-in user’s collections. Requires the 'write_collections' scope. Note: If the photo is already in the collection, this acion has no effect. :param collection_id [string]: The collection’s ID. Required. :param photo_id [string]: The photo’s ID. Required. :return: [Tuple]: The Unsplash Collection and Photo """ url = "/collections/%s/add" % collection_id data = {"collection_id": collection_id, "photo_id": photo_id} result = self._post(url, data=data) or {} return CollectionModel.parse( result.get("collection")), PhotoModel.parse(result.get("photo"))
def get(self, photo_id, width=None, height=None, rect=None): """ Retrieve a single photo. Note: Supplying the optional w or h parameters will result in the custom photo URL being added to the 'urls' object: :param photo_id [string]: The photo’s ID. Required. :param width [integer]: Image width in pixels. :param height [integer]: Image height in pixels. :param rect [string]: 4 comma-separated integers representing x, y, width, height of the cropped rectangle. :return: [Photo]: The Unsplash Photo. """ url = "/photos/%s" % photo_id params = {"w": width, "h": height, "rect": rect} result = self._get(url, params=params) return PhotoModel.parse(result)
def photos(self, username, page=1, per_page=10, order_by="latest"): """ Get a list of photos uploaded by a user. :param username [string]: The user’s username. Required. :param page [integer]: Page number to retrieve. (Optional; default: 1) :param per_page [integer]: Number of items per page. (Optional; default: 10) :param order_by [string]: How to sort the photos. Optional. (Valid values: latest, oldest, popular; default: latest) :return: [Array]: A single page of the Photo list. """ url = "/users/{username}/photos".format(username=username) result = self._photos(url, username, page=page, per_page=per_page, order_by=order_by) return PhotoModel.parse_list(result)
def random(self, count=1, **kwargs): """ Retrieve a single random photo, given optional filters. Note: If supplying multiple category ID’s, the resulting photos will be those that match all of the given categories, not ones that match any category. Note: You can’t use the collections and query parameters in the same request Note: When supplying a count parameter - and only then - the response will be an array of photos, even if the value of count is 1. All parameters are optional, and can be combined to narrow the pool of photos from which a random one will be chosen. :param count [integer]: The number of photos to return. (Default: 1; max: 30) :param category: Category ID(‘s) to filter selection. If multiple, comma-separated. (deprecated) :param collections: Public collection ID(‘s) to filter selection. If multiple, comma-separated :param featured: Limit selection to featured photos. :param username: Limit selection to a single user. :param query: Limit selection to photos matching a search term. :param w: Image width in pixels. :param h: Image height in pixels. :param orientation: Filter search results by photo orientation. Valid values are landscape, portrait, and squarish. :return: [Array] or [Photo]: A single page of the curated Photo list or The Unsplash Photo. . :raise UnsplashError: If the given orientation is not in the default orientation values. """ kwargs.update({"count": count}) orientation = kwargs.get("orientation", None) if orientation and orientation not in self.orientation_values: raise Exception() url = "/photos/random" result = self._get(url, params=kwargs) return PhotoModel.parse_list(result)
def _all(self, url, page=1, per_page=10, order_by="latest"): if order_by not in self.ordering_values: raise Exception() params = {"page": page, "per_page": per_page, "order_by": order_by} result = self._get(url, params=params) return PhotoModel.parse_list(result)