def test_locate(self): import functools from sqlalchemy import Column, Integer from sqlalchemy_media import File, StoreManager from sqlalchemy_media.tests.helpers.types import Json StoreManager.register('ftp', functools.partial(FTPStore, hostname=MockFTP(), root_path=self.root_path, base_url=self.base_url), default=True) class Person(self.Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) file = Column(File.as_mutable(Json)) session = self.create_all_and_get_session() person1 = Person() self.assertIsNone(person1.file) sample_content = b'Simple text.' with StoreManager(session): person1 = Person() person1.file = File.create_from(io.BytesIO(sample_content), content_type='text/plain', extension='.txt') self.assertIsInstance(person1.file, File) self.assertEqual( person1.file.locate(), '%s%s?_ts=%s' % (self.base_url, person1.file.path, person1.file.timestamp))
def initialize_models(self, session=None): StoreManager.register('fs', functools.partial( FileSystemStore, settings.media_storage.file_system_dir, base_url=settings.media_storage.base_url), default=True) super().initialize_models(session=session)
def initialize_orm(cls, engine=None): StoreManager.register('fs', functools.partial( FileSystemStore, settings.storage.local_directory, base_url=settings.storage.base_url, ), default=True) super().initialize_orm(cls, engine)
def mockup(): with StoreManager(DBSession): images = PlaceImageList([ PlaceImage.create_from(f'{IMAGE_PATH}/1.jpeg'), PlaceImage.create_from(f'{IMAGE_PATH}/2.jpeg'), ]) categories = [ Category(name='جنگل', ), Category(name='رودخونه', ), Category(name='کوه', ), Category(name='مذهبی', ), ] for i in range(1000): place = Place( name=f'foo {i}', description=description, address=address, latitude=random() * 20 + 30, longitude=random() * 20 + 30, images=images, category=choice(categories), ) DBSession.add(place) DBSession.commit()
def index(): page = MasterPageView('Index') page += '<form method="POST" action="/" enctype="multipart/form-data">' page += '<input type="text" name="name" value="Your Name here"/>' page += '<input type="file" name="avatar" />' page += '<input type="submit" />' page += '</form>' page += '<hr />' with StoreManager(db.session()): if request.method == 'POST': new_person = Person(name=request.form['name'], avatar=request.files['avatar']) db.session.add(new_person) db.session.commit() page += '<ul>' for p in db.session.query(Person): page += '<li>' page += '<img src="%s" alt="%s">' % (p.avatar.locate(), p.name) page += '<h2>%s</h2>' % p.name page += '<h2>ID: %s</h2>' % p.id page += '</li>' page += '</ul>' return str(page)
def mockup(cls): session = cls.create_session(expire_on_commit=True) with StoreManager(session): with open(IMAGE_PATH, 'rb') as f: cls.message1 = Message( body='This is message 1', mimetype='image/png', attachment=io.BytesIO(f.read()), ) cls.message2 = Message(body='This is message 2', mimetype='text/plain') user = Member(title='user', first_name='user_first_name', last_name='user_last_name', email='*****@*****.**', access_token='access token', reference_id=1) session.add(user) user1 = Member(title='user1', first_name='user1_first_name', last_name='user1_last_name', email='*****@*****.**', access_token='access token1', reference_id=2, messages=[cls.message1, cls.message2]) cls.room = Room(title='room', messages=[cls.message1, cls.message2], members=[user1]) session.add(cls.room) cls.message2.soft_delete() session.commit()
def mockup(cls): cls.session = cls.create_session(expire_on_commit=True) with StoreManager(cls.session): cls.message1 = Message(body='This is message 1', mimetype='text/plain') cls.message2 = Message(body='This is message 2', mimetype='text/plain') cls.message3 = Message(body='This is message 3', mimetype='image/png', attachment=image_path) user = Member(email='*****@*****.**', title='user', access_token='access token', reference_id=1, messages=[cls.message1, cls.message3]) user1 = Member(email='*****@*****.**', title='user1', access_token='access token1', reference_id=2, messages=[cls.message2]) user2 = Member(email='*****@*****.**', title='user2', access_token='access token2', reference_id=3) room = Room(title='room', type='room', messages=[cls.message1, cls.message2, cls.message3], members=[user, user1]) cls.session.add(user2) cls.session.add(room) cls.session.commit()
def mockup(cls): session = cls.create_session() with StoreManager(session): with open(IMAGE_PATH, 'rb') as f: cls.message1 = Message( body='This is message 1', mimetype='image/png', attachment=io.BytesIO(f.read()), ) cls.message2 = Message(body='This is message 2', mimetype='text/plain') cls.message3 = Message(body='This is message 3', mimetype='text/plain') user1 = Member(email='*****@*****.**', title='user1', first_name='user1_first_name', last_name='user1_last_name', access_token='access token1', reference_id=2, messages=[cls.message1]) user2 = Member(email='*****@*****.**', title='user2', first_name='user2_first_name', last_name='user2_last_name', access_token='access token2', reference_id=3, messages=[cls.message3, cls.message2]) room1 = Room(title='room1', members=[user1, user2], messages=[cls.message1, cls.message3]) room2 = Room(title='room2', members=[user2], messages=[cls.message2]) session.add(user1) session.commit()
def test_model_constructor(self): class Person(self.Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) cv = Column(File.as_mutable(Json), nullable=True) session = self.create_all_and_get_session() person1 = Person(cv=File()) self.assertIsInstance(person1.cv, File) with StoreManager(session): person1.cv.attach(BytesIO(b'Simple text')) session.add(person1) session.commit()
def main(): if not path.exists(TEMP_PATH): mkdir(TEMP_PATH) StoreManager.register('fs', functools.partial(FileSystemStore, TEMP_PATH, 'http://localhost:8080/avatars'), default=True) conf = { '/': { 'tools.sessions.on': True, 'tools.staticdir.root': WORKING_DIR }, '/avatars': { 'tools.staticdir.on': True, 'tools.staticdir.dir': TEMP_PATH } } cherrypy.quickstart(Root(), config=conf)
def setUp(self): self.temp_path = join(self.this_dir, 'temp', self.__class__.__name__, self._testMethodName) self.sys_temp_path = join('/tmp/sa-media-tests', self.__class__.__name__, self._testMethodName) self.base_url = 'http://static1.example.orm' # Remove previous files, if any! to make a clean temp directory: if exists(self.temp_path): # pragma: no cover shutil.rmtree(self.temp_path) makedirs(self.temp_path) StoreManager.register('fs', functools.partial(FileSystemStore, self.temp_path, self.base_url), default=True) StoreManager.register( 'temp_fs', functools.partial(FileSystemStore, self.sys_temp_path, self.base_url)) super().setUp()
def test_content_type_validator(self): class PDFFile(File): __pre_processors__ = [ MagicAnalyzer(), ContentTypeValidator(['application/pdf', 'image/jpeg']) ] class Person(self.Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) cv = Column(PDFFile.as_mutable(Json), nullable=True) session = self.create_all_and_get_session() person1 = Person(cv=PDFFile()) with StoreManager(session): self.assertIsNotNone(person1.cv.attach(self.cat_jpeg)) self.assertRaises(ContentTypeValidationError, person1.cv.attach, BytesIO(b'Simple text'))
def test_file_size_limit(self): class LimitedFile(File): __min_length__ = 20 __max_length__ = 30 class Person(self.Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) cv = Column(LimitedFile.as_mutable(Json), nullable=True) session = self.create_all_and_get_session() person1 = Person() person1.cv = LimitedFile() with StoreManager(session): self.assertRaises(MaximumLengthIsReachedError, person1.cv.attach, BytesIO(b'more than 30 chars!............'))
def submit(self, name=None, avatar=None): session = DBSession() with StoreManager(session): new_person = Person(name=name, avatar=avatar.file if avatar else avatar) session.add(new_person) session.commit() page = MasterPageView('View', body='<ul>') for p in session.query(Person): page += '<li>' page += '<img src="%s" alt="%s">' % (p.avatar.locate(), p.name) page += '<h2>%s</h2>' % p.name page += '<h2>ID: %s</h2>' % p.id page += '</li>' page += '</ul>' return page
def mockup(cls): session = cls.create_session() with StoreManager(session): images = PlaceImageList([ PlaceImage.create_from(f'{IMAGE_PATH}/1.jpeg'), PlaceImage.create_from(f'{IMAGE_PATH}/2.jpeg'), ]) jungle_category = Category(name='Jungle', ) place1 = Place( name='foo', description='bar', address='a/b/c/d', latitude=0, longitude=0, images=images, category=jungle_category, ) session.add(place1) place2 = Place( name='foo', description='bar', address='a/b/c/d', latitude=1, longitude=1, category=jungle_category, ) session.add(place2) place3 = Place( name='foo', description='bar', address='a/b/c/d', latitude=3, longitude=3, category=jungle_category, ) session.add(place3) session.commit()
def test_attribute_type_coercion(self): class MyAttachmentType(File): __auto_coercion__ = True pass class Person(self.Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) cv = Column(MyAttachmentType.as_mutable(Json), nullable=True) session = self.create_all_and_get_session() with StoreManager(session): person1 = Person() person1.cv = BytesIO(b'Simple text') self.assertIsInstance(person1.cv, MyAttachmentType) person2 = Person(cv=BytesIO(b'Simple text')) self.assertIsInstance(person2.cv, MyAttachmentType) session.add(person2) session.commit()
def mockup(cls): session = cls.create_session() with StoreManager(session): with open(IMAGE_PATH, 'rb') as f: cls.message1 = Message( body='This is message 1', mimetype='text/plain', attachment=io.BytesIO(f.read()), ) message2 = Message( body='This is message 2', mimetype='text/plain', ) cls.message3 = Message( body='This is message 3', mimetype='text/plain', ) cls.user1 = Member( email='*****@*****.**', title='user1', access_token='access token1', reference_id=2, messages=[cls.message1, message2, cls.message3] ) cls.user2 = Member( email='*****@*****.**', title='user2', access_token='access token2', reference_id=3 ) session.add(cls.user2) room = Room( title='room', type='room', members=[cls.user1], messages=[cls.message1, message2, cls.message3] ) session.add(room) cls.message3.soft_delete() session.commit()
def test_overwrite(self): class Person(self.Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) cv = Column(File.as_mutable(Json), nullable=True) session = self.create_all_and_get_session() person1 = Person(cv=File()) self.assertIsInstance(person1.cv, File) with StoreManager(session): person1.cv.attach(BytesIO(b'Simple text')) cv_filename = join(self.temp_path, person1.cv.path) self.assertTrue(exists(cv_filename)) session.add(person1) session.commit() # Now overwriting the file person1 = session.query(Person).filter( Person.id == person1.id).one() person1.cv.attach(BytesIO(b'Another simple text'), overwrite=True) self.assertTrue(exists(cv_filename)) session.commit() self.assertTrue(exists(cv_filename))
def test_file(self): class Person(self.Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) cv = Column(File.as_mutable(Json), nullable=True) session = self.create_all_and_get_session() # person1 = Person(name='person1') person1 = Person() self.assertIsNone(person1.cv) sample_content = b'Simple text.' with StoreManager(session): # First file before commit person1.cv = File.create_from(BytesIO(sample_content), content_type='text/plain', extension='.txt') self.assertIsInstance(person1.cv, File) self.assertEqual( person1.cv.locate(), '%s/%s?_ts=%s' % (self.base_url, person1.cv.path, person1.cv.timestamp)) self.assertDictEqual( person1.cv, { 'content_type': 'text/plain', 'key': person1.cv.key, 'extension': '.txt', 'length': len(sample_content), 'reproducible': False, 'timestamp': person1.cv.timestamp }) first_filename = join(self.temp_path, person1.cv.path) self.assertTrue(exists(first_filename)) self.assertEqual(person1.cv.length, len(sample_content)) # Second file before commit person1.cv.attach(BytesIO(sample_content), content_type='text/plain', extension='.txt') second_filename = join(self.temp_path, person1.cv.path) self.assertTrue(exists(second_filename)) # Adding object to session, the new life-cycle of the person1 just began. session.add(person1) session.commit() self.assertFalse(exists(first_filename)) self.assertTrue(exists(second_filename)) # Loading again sample_content = b'Lorem ipsum dolor sit amet' person1 = session.query(Person).filter( Person.id == person1.id).one() person1.cv.attach(BytesIO(sample_content), content_type='text/plain', extension='.txt') self.assertIsInstance(person1.cv, File) self.assertDictEqual( person1.cv, { 'content_type': 'text/plain', 'key': person1.cv.key, 'extension': '.txt', 'length': len(sample_content), 'reproducible': False, 'timestamp': person1.cv.timestamp }) third_filename = join(self.temp_path, person1.cv.path) self.assertTrue(exists(second_filename)) self.assertTrue(exists(third_filename)) # Committing the session, so the store must done the scheduled jobs session.commit() self.assertFalse(exists(second_filename)) self.assertTrue(exists(third_filename)) # Rollback person1.cv.attach(BytesIO(sample_content), content_type='text/plain', extension='.txt') forth_filename = join(self.temp_path, person1.cv.path) self.assertTrue(exists(forth_filename)) session.rollback() self.assertTrue(exists(third_filename)) self.assertFalse(exists(forth_filename)) # Delete file after object deletion person1 = session.query(Person).filter( Person.id == person1.id).one() session.delete(person1) session.commit() self.assertFalse(exists(third_filename)) # Delete file on set to null person1 = Person() self.assertIsNone(person1.cv) person1.cv = File() person1.cv.attach(BytesIO(sample_content), content_type='text/plain', extension='.txt') fifth_filename = join(self.temp_path, person1.cv.path) person1.cv = None session.add(person1) self.assertTrue(exists(fifth_filename)) session.commit() # Because delete_orphan is not set. self.assertTrue(exists(fifth_filename)) # storing a file on separate store: person1.cv = File.create_from(BytesIO(sample_content), store_id='temp_fs') fifth_filename = join(self.sys_temp_path, person1.cv.path) session.commit() self.assertTrue(exists(fifth_filename))
from sqlalchemy_media.exceptions import ContentTypeValidationError import pytest from jaguar.models.envelop import Message, FileAttachment from jaguar.models.membership import Member from jaguar.models.target import Room this_dir = abspath(join(dirname(__file__))) text_path = join(this_dir, 'stuff', 'text_file.txt') tex_path = join(this_dir, 'stuff', 'sample_tex_file.tex') image_path = join(this_dir, 'stuff', '150x150.png') temp_path = join(this_dir, 'temp') base_url = 'http://static1.example.orm' StoreManager.register('fs', functools.partial(FileSystemStore, temp_path, base_url), default=True) def test_message_model(db): session = db() if exists(temp_path): shutil.rmtree(temp_path) message1 = Message( mimetype='message1', body='This is message 1', ) message2 = Message( mimetype='message2', body='This is message 2',
from flask_sqlalchemy import SQLAlchemy from sqlalchemy import TypeDecorator, Unicode from sqlalchemy_media import Image, ImageValidator, ImageProcessor, ImageAnalyzer, StoreManager, \ FileSystemStore from sqlalchemy_media.constants import MB, KB WORKING_DIR = path.abspath(getcwd()) TEMP_PATH = path.join(WORKING_DIR, 'static', 'avatars') app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///demo.db' db = SQLAlchemy(app) StoreManager.register('fs', functools.partial( FileSystemStore, TEMP_PATH, 'http://localhost:5000/static/avatars'), default=True) class MasterPageView(object): header = '<!DOCTYPE html><head><meta charset="utf-8"><title>%s</title></head><body>' footer = '</body>' def __init__(self, title='demo', body=''): self.title = title self.body = body def __str__(self): return (self.header % self.title) + self.body + self.footer
from sqlalchemy import Column, Integer, create_engine, Unicode, TypeDecorator from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy_media import Image, StoreManager, FileSystemStore, ImageProcessor # Step 1 TEMP_PATH = '/tmp/sqlalchemy-media' Base = declarative_base() engine = create_engine('sqlite:///:memory:', echo=False) session_factory = sessionmaker(bind=engine) # Step 2 StoreManager.register('fs', functools.partial(FileSystemStore, TEMP_PATH, 'http://static.example.org/'), default=True) # Sqlite is not supporting JSON type, so emulating it: 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)
def mockup(cls): session = cls.create_session() with StoreManager(session): with open(IMAGE_PATH, 'rb') as f: cls.user1 = Member( email='*****@*****.**', title='user', first_name='user_first_name', last_name='user_last_name', access_token='access token', reference_id=1, ) session.add(cls.user1) cls.user2 = Member( email='*****@*****.**', title='user2', first_name='user2_first_name', last_name='user2_last_name', access_token='access token2', reference_id=2, ) session.add(cls.user2) room = Room( title='room', type='room', members=[cls.user1, cls.user2] ) session.add(room) session.flush() auditlog_message1 = Message( body='{"log": 1}', mimetype='application/x-auditlog', target_id=room.id, sender_id=cls.user2.id, ) session.add(auditlog_message1) auditlog_message2 = Message( body='{"log": 1}', mimetype='application/x-auditlog', target_id=room.id, sender_id=cls.user2.id, ) session.add(auditlog_message2) auditlog_message3 = Message( body='{"log": 1}', mimetype='application/x-auditlog', target_id=room.id, sender_id=cls.user2.id, ) session.add(auditlog_message3) cls.message1 = Message( body='This is message 1', mimetype='text/plain', target_id=room.id, sender_id=cls.user1.id, ) session.add(cls.message1) cls.message2 = Message( body='This is message 2', mimetype='text/plain', target_id=room.id, sender_id=cls.user1.id ) session.add(cls.message2) cls.message3 = Message( body='This is message 3', mimetype='text/plain', target_id=room.id, sender_id=cls.user2.id, attachment=io.BytesIO(f.read()), ) session.add(cls.message3) session.flush() cls.message4 = Message( body='This is message 4', mimetype='text/plain', target_id=room.id, sender_id=cls.user2.id, ) session.add(cls.message3) member_message_seen1 = MemberMessageSeen( member_id=cls.user1.id, message_id=cls.message1.id ) session.add(member_message_seen1) member_message_seen2 = MemberMessageSeen( member_id=cls.user1.id, message_id=cls.message2.id ) session.add(member_message_seen2) session.commit() cls.auditlog_message1_id = auditlog_message1.id cls.auditlog_message2_id = auditlog_message2.id cls.auditlog_message3_id = auditlog_message3.id
def mockup(cls): session = cls.create_session() with StoreManager(session): cls.message1 = Message( body='This is message 1', mimetype='text/plain', ) cls.message2 = Message( body='This is message 2', mimetype='text/plain', ) cls.message3 = Message( body='This is message 3', mimetype='text/plain', ) cls.message4 = Message( body='This is message 4', mimetype='text/plain', ) cls.message5 = Message(body='This is message 5', mimetype='image/png', attachment=image_path) cls.message6 = Message( body='This is message 6', mimetype='text/plain', ) user1 = Member(email='*****@*****.**', title='user', first_name='user_first_name', last_name='user_last_name', access_token='access token1', reference_id=2, messages=[ cls.message1, cls.message2, cls.message3, cls.message5 ]) user2 = Member(email='*****@*****.**', title='user2', first_name='user2_first_name', last_name='user2_last_name', access_token='access token2', reference_id=3, messages=[cls.message4, cls.message6]) session.add(user2) user3 = Member( email='*****@*****.**', title='user3', first_name='user3_first_name', last_name='user3_last_name', access_token='access token3', reference_id=4, ) room1 = Room(title='room1', type='room', members=[user1, user3], messages=[ cls.message1, cls.message3, cls.message4, cls.message5, cls.message6 ]) session.add(room1) room2 = Room( title='room2', type='room', members=[user1, user3], messages=[cls.message2], ) session.add(room2) session.commit()