All Projects → pylover → Sqlalchemy Media

pylover / Sqlalchemy Media

Licence: other
Another attachment extension for SqlAlchemy to manage assets which are associated with database models but you don't want to store them into the database

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Sqlalchemy Media

Sqlalchemy Imageattach
SQLAlchemy extension for attaching images to entities.
Stars: ✭ 107 (+55.07%)
Mutual labels:  orm, sqlalchemy
Ormar
python async mini orm with fastapi in mind and pydantic validation
Stars: ✭ 155 (+124.64%)
Mutual labels:  orm, sqlalchemy
Tornado Sqlalchemy
SQLAlchemy support for Tornado
Stars: ✭ 112 (+62.32%)
Mutual labels:  orm, sqlalchemy
Sqlalchemy Hana
SQLAlchemy Dialect for SAP HANA
Stars: ✭ 75 (+8.7%)
Mutual labels:  orm, sqlalchemy
Architect
A set of tools which enhances ORMs written in Python with more features
Stars: ✭ 320 (+363.77%)
Mutual labels:  orm, sqlalchemy
Sandman2
Automatically generate a RESTful API service for your legacy database. No code required!
Stars: ✭ 1,765 (+2457.97%)
Mutual labels:  orm, sqlalchemy
Sqlservice
The missing SQLAlchemy ORM interface.
Stars: ✭ 159 (+130.43%)
Mutual labels:  orm, sqlalchemy
Gino
GINO Is Not ORM - a Python asyncio ORM on SQLAlchemy core.
Stars: ✭ 2,299 (+3231.88%)
Mutual labels:  orm, sqlalchemy
Python For Entrepreneurs Course Demos
Contains all the "handout" materials for Talk Python's Python for Entrepreneurs course. This includes notes and the final version of the website code.
Stars: ✭ 247 (+257.97%)
Mutual labels:  orm, sqlalchemy
Awesome Sqlalchemy
A curated list of awesome tools for SQLAlchemy
Stars: ✭ 2,316 (+3256.52%)
Mutual labels:  orm, sqlalchemy
Qb
The database toolkit for go
Stars: ✭ 524 (+659.42%)
Mutual labels:  orm, sqlalchemy
Sqlalchemy Mixins
Active Record, Django-like queries, nested eager load and beauty __repr__ for SQLAlchemy
Stars: ✭ 441 (+539.13%)
Mutual labels:  orm, sqlalchemy
Records
SQL for Humans™
Stars: ✭ 6,761 (+9698.55%)
Mutual labels:  orm, sqlalchemy
Sqlite orm
❤️ SQLite ORM light header only library for modern C++
Stars: ✭ 1,121 (+1524.64%)
Mutual labels:  orm
Ouzo
Ouzo Framework - PHP MVC ORM
Stars: ✭ 66 (-4.35%)
Mutual labels:  orm
Vuex Orm Examples Nuxt
The example Nuxt application to demonstrate the use case of the Vuex ORM.
Stars: ✭ 63 (-8.7%)
Mutual labels:  orm
Flexicms
Flexible site management system Flexi CMS
Stars: ✭ 61 (-11.59%)
Mutual labels:  orm
Dbx
A neat codegen-based database wrapper written in Go
Stars: ✭ 68 (-1.45%)
Mutual labels:  orm
Cakephp
CakePHP: The Rapid Development Framework for PHP - Official Repository
Stars: ✭ 8,453 (+12150.72%)
Mutual labels:  orm
Layr
Dramatically simplify full‑stack development
Stars: ✭ 1,111 (+1510.14%)
Mutual labels:  orm

sqlalchemy-media

.. image:: http://img.shields.io/pypi/v/sqlalchemy-media.svg :target: https://pypi.python.org/pypi/sqlalchemy-media

.. image:: https://requires.io/github/pylover/sqlalchemy-media/requirements.svg?branch=master :target: https://requires.io/github/pylover/sqlalchemy-media/requirements/?branch=master :alt: Requirements Status

.. image:: https://travis-ci.org/pylover/sqlalchemy-media.svg?branch=master :target: https://travis-ci.org/pylover/sqlalchemy-media

.. image:: https://coveralls.io/repos/github/pylover/sqlalchemy-media/badge.svg?branch=master :target: https://coveralls.io/github/pylover/sqlalchemy-media?branch=master

.. image:: https://img.shields.io/badge/license-MIT-brightgreen.svg :target: https://github.com/pylover/sqlalchemy-media/blob/master/LICENSE

.. image:: https://img.shields.io/gitter/room/pylover/sqlalchemy-media.svg :target: https://gitter.im/pylover/sqlalchemy-media

Documentation

See the documentation_ for full description.

Why ?

Nowadays, most of the database applications are used to allow users to upload and attach files of various types to ORM models.

Handling those jobs is not simple if you have to care about Security, High-Availability, Scalability, CDN and more things you may have already been concerned. Accepting a file from public space, analysing, validating, processing(Normalizing) and making it available to public space again is the main goal of this project.

Sql-Alchemy is the best platform for implementing this stuff. It has the SqlAlchemyMutable_ types facility to manipulate the objects with any type in-place. why not ?

.. note:: The main idea comes from dahlia's SQLAlchemyImageAttach_.

Overview

  • Storing and locating any file, tracking it by sqlalchemy models.
  • Storage layer is completely separated from data model, with a simple api: (put, delete, open, locate)
  • Using any SqlAlchemy data type which interfaces Python dictionary. This is achieved by using the SqlAlchemyTypeDecorators_ and SqlAlchemyMutable_.
  • Offering delete_orphan flag to automatically delete files which orphaned via attribute set or delete from collections, or objects leaved in memory alone! by setting it's last pointer to None.
  • Attaching files from Url, LocalFileSystem and Streams.
  • Extracting the file's mimetype from the backend stream if possible, using magic module.
  • Limiting file size(min, max), to prevent DOS attacks.
  • Adding timestamp in url to help caching.
  • Auto generating thumbnails, using width, height and or ratio.
  • Analyzing files & images using magic and wand.
  • Validating mimetype, width, height and image ratio.
  • Automatically resize & reformat images before store.
  • Thanks @YukSeungChan <https://github.com/YukSeungChan>_, for implementing the S3Store and OS2Store.
  • SSH/SFTP Store

Quick Start

Here is a simple example to see how to use this library:

.. code-block:: python

import functools import json from os.path import exists, join

from sqlalchemy import create_engine, TypeDecorator, Unicode, Column, Integer from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy_media import StoreManager, FileSystemStore, Image, ImageAnalyzer, ImageValidator, ImageProcessor

TEMP_PATH = '/tmp/sqlalchemy-media' Base = declarative_base() engine = create_engine('sqlite:///:memory:', echo=False) session_factory = sessionmaker(bind=engine)

StoreManager.register( 'fs', functools.partial(FileSystemStore, TEMP_PATH, 'http://static.example.org/'), default=True )

class Json(TypeDecorator): impl = Unicode

  def process_bind_param(self, value, engine):
      return json.dumps(value)

  def process_result_value(self, value, engine):
      if value is None:
          return None

      return json.loads(value)

class ProfileImage(Image): pre_processors = [ ImageAnalyzer(), ImageValidator( minimum=(80, 80), maximum=(800, 600), min_aspect_ratio=1.2, content_types=['image/jpeg', 'image/png'] ), ImageProcessor( fmt='jpeg', width=120, crop=dict( left='10%', top='10%', width='80%', height='80%', ) ) ]

class Person(Base): tablename = 'person'

  id = Column(Integer, primary_key=True)
  name = Column(Unicode(100))
  image = Column(ProfileImage.as_mutable(Json))

  def __repr__(self):
      return "<%s id=%s>" % (self.name, self.id)

Base.metadata.create_all(engine, checkfirst=True)

if name == 'main': session = session_factory()

  with StoreManager(session):
      person1 = Person()
      person1.image = ProfileImage.create_from('https://www.python.org/static/img/[email protected]')
      session.add(person1)
      session.commit()

      print('Content type:', person1.image.content_type)
      print('Extension:', person1.image.extension)
      print('Length:', person1.image.length)
      print('Original filename:', person1.image.original_filename)

      thumbnail = person1.image.get_thumbnail(width=32, auto_generate=True)
      print(thumbnail.height)
      assert exists(join(TEMP_PATH, thumbnail.path))

      thumbnail = person1.image.get_thumbnail(ratio=.3, auto_generate=True)
      print(thumbnail.width, thumbnail.height)
      assert exists(join(TEMP_PATH, thumbnail.path))

      person1.image.attach('https://www.python.org/static/img/python-logo.png')
      session.commit()

      print('Content type:', person1.image.content_type)
      print('Extension:', person1.image.extension)
      print('Length:', person1.image.length)
      print('Original filename:', person1.image.original_filename)

  with StoreManager(session, delete_orphan=True):
      deleted_filename = join(TEMP_PATH, person1.image.path)
      person1.image = None
      session.commit()

      assert not exists(deleted_filename)

      person1.image = ProfileImage.create_from('https://www.python.org/static/img/python-logo.png')
      session.commit()

      print('Content type:', person1.image.content_type)
      print('Extension:', person1.image.extension)
      print('Length:', person1.image.length)
      print('Original filename:', person1.image.original_filename)

Will produce::

Content type: image/jpeg
Extension: .jpg
Length: 2020
Original filename: https://www.python.org/static/img/[email protected]
8
28 7
Content type: image/jpeg
Extension: .jpg
Length: 2080
Original filename: https://www.python.org/static/img/python-logo.png
Content type: image/jpeg
Extension: .jpg
Length: 2080
Original filename: https://www.python.org/static/img/python-logo.png

Examples

Checkout the examples directory in the root of the repo.

Branching

A new branching model is applied to this repository, which consists of a master branch and release branches.

Contribution

  • Remember the zen of Python(import this) before doing anything.
  • Maximum line width is: 79.

.. _SqlAlchemyMutable: http://docs.sqlalchemy.org/en/latest/orm/extensions/mutable.html .. _SqlAlchemyTypeDecorators: http://docs.sqlalchemy.org/en/latest/core/custom_types.html#typedecorator-recipes .. _SQLAlchemyImageAttach: https://github.com/dahlia/sqlalchemy-imageattach .. _documentation: http://sqlalchemy-media.dobisel.com

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].