Git Product home page Git Product logo

django-gallery-api's Introduction

Gallery API

This is very simple gallery app that provides only JSON API for creating gallery, adding, removing images and getting image thumbnails of specified sizes.

Administration of the galleries can be also done from standard Django admin interface.

It is written in Django 3.0, with Django REST framework.

NOTE: This project is only for presentation purposes! Don't use it nowhere!

Running the project in local enviroment

docker-compose

The most convenient way, how to run this project is to use Docker with docker-compose tool.

In project directory (where you see docker-compose.yml) run commad:

docker-compose up

Building the image takes quite a long time. When the container is running, local server is listening on port :80 and app is accessible on http://localhost:80.

Docker

If you don't have docker-compose, you can use standard Docker commands to build image and run container. Those commands are part of Makefile.

# build image
make build

# run container based on builded image
make run

# stop running container
make stop

# start existing container
make start

# remove container
make rm

# remove image
make rm-image

The pure Python way (without Docker)

To run this project without docker You will need to have installed:

  • python v.3
  • pipenv
  • sqlite v.3

In src/ subdirectory (where you see Pipfile) run these commands:

# install dependencies
pipenv --three install

# run database migrations
pipenv run python manage.py migrate

# create default admin user
pipenv run python manage.py create_default_superuser --username admin --password admin

Now, you need to run local development server with

pipenv run python manage.py runserver 80

NOTE: You need to have admin privilegies to start development server on port :80

Try running app in browser on url http://localhost:80/.

NOTE: This project depends on Pillow library, which can be difficult to install for a first time, because Pillow depends on external packages which must be installed globally in your operating system. For more details, reffer Pillow installation guide.

Testing the API

On http://localhost:80/ is API schema with all the links and details.

In a nutshell:

  • /gallery
    • list of the galleries or create one, providing name in request body
  • /gallery/{path}
    • GET mothod returns detail of the gallery with all the images. When you use fullpath of the image as a path parameter of this request, server returns original image.
    • POST method to this endpoint is used to upload images to selected gallery. You can upload several images in one request. Uploading photos are authenticated with Facebook (see below).
    • DELETE method deletes gallery with all the images and generated thumbnails. DELETE method combined with fullpath of the image removes image from gallery.
  • images/{x_size}x{y_size}/{gallery_path}/{image_path}/
    • returns resized image. Image is defined by {gallery_path}/{image_path}, but it is same as a fullpath attribude from the detail of image. The resizing method does not maintain aspect ratio, only when one of the size parameter is 0.

You can also try those links from browser, for example:

  • http://localhost:80/gallery

Django REST framework will render its own views for the endpoints.

NOTE: API endpoints are not authorized! Authentication and authorization is explicitely disabled for the simplicity of project presentation! The only request, that is authorized is photo upload.

Administration from backend (Django Admin)

This Django project comes with prepopulated sqlite database in file src/db.sqlite3. This allows without any special effort run the project and use. You can administrate application from standard Django admin on url http://localhost:80/admin.

Default superuser is:

  • name: admin
  • password:admin

Facebook authorization

Request, for the photo upload is authenticated with Facebook OAuth API. All the settings are predefined in file /src/app/settings_fb.py. Default application ID belongs to "Programator.sk" Facebook App. Redirect URI is setted to https://localhost/token.

NOTE: Development server can't handle https:// requests, so after redirect from Facebook API, url needs to be corrected to http:// manually!

How to send request

Request is authorized with Bearer token, which is part of Authorization HTTP header.

Example:

Authorization=Bearer EAAO922hDT5UBAJVuZBMMeMZAmJCLZC6MUVLLxzVHwPDKPZAEac3ZBuYTIfy3B1v0wB5Jffhe1DNlaws5enNkWwjK3KmFxHA7I2zRa7ScNuzX1W9QbROyicwNzvGIdonMwchg7CJAtt3IPQqq0NosqK8aXnZAguQKliUGlO6vZCNDiyDpCSb8ym6sNrATqTqXlwZD

If the access token is not valid, server responds with 401 Unauthorized and with redirect_url in body. This url can be used for renew token or grant access from the Facebook API.

Example:

{
  "detail": "Ivalid token",
  "redirect_url": "https://www.facebook.com/v6.0/dialog/oauth?response_type=token&client_id=1053174974861205&redirect_uri=https%3A%2F%2Flocalhost%2Ftoken&state=jnSfZdMYQjeyzEnXyXxISsVgS1Xe6T"
}

The backend server provides helper redirect url http://localhost/token (NOT https://). This view parses response URL and prints Access token and few other details.

django-gallery-api's People

Contributors

jurajkavka avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.