Skip to content

Commit

Permalink
Imgee V2 (#59)
Browse files Browse the repository at this point in the history
* removed celery as dependency

* fixed - was missing folder name when downloading file

* handling multiple resize request for same file

* removed print statements

* if there is an error while processing a file, raise it

* fixed comment

* removed all tests

* removed more unnecessary things

* removed unnecessary imports and minor cleanup

* renamed async.py

* added label tests

* separating label logic

* fixed label tests

* more cleanup, moved some views out of index.py

* cleaned up label saving codes a little

* added tests for upload and delete

* updated status code for delete test

* mixed couple of migrations and showing error

* some more cleanups

* using get_image_url to create image url

* not showing delete label button if not logged in

* importing only the needed forms

* added tests for resizing files and testing with multiple file types

* comparing redirect location with generated thumbnail url

* added redis to travis

* fixed fixtures file to use new init_app behavior

* Function name fix, using uuid1mc, time format fix, TaskRegistry fix (#60)

* function name fix, using uuid1mc, time format fix

* deleting keys from registry when deleting a file

* restructured TaskRegistry to use Redis Keys with TTL set to 60 seconds
instead of assigning them to a set

* re-adding nose and coverage to travis file, removed it by mistake

* using runtests.py file to run tests in travis

* reordered functions and better string formating

* made the redis key expiry time an argument

* assigning error handler the right way

* not resizing if the gif image is animated

* added pillow as dependency

* using redis pipeline to delete multiple keys

* missed one place to use redis pipeline

* deleted runtests.py, not needed anymore

* fixed typo

* fixed some import issues

* moved upload directory creation to manage.py

* added some comments

* moved importing app inside the only function that uses it

* Update runtests.sh

* added manage.py init to runtests

* removed mkdir from travis config

* fixed upload path issue

* moar fixes to registry, cleanup, moved back to uuid4

* fixed typo

* setting test environment variable in travis file

* setting env in existing block, otherwise it gets overwritten

* moved the init command out of if block

* rearranged travis config, cleanup of upload directory path

* lot of minor fixes

* using dotted relative import syntax everywhere, added tests for registry(redis)

* cleaning up queries and imports

* cleanup of os.path and more comments on thumbnail

* fixed typo

* added buildspec.yml file for testing AWS CodeBuild

* setting env varialbe manually for aws codebuild

* removed buildspec.yml file

* showing loading spinner if file is still processing
  • Loading branch information
Bibhas authored Jul 17, 2017
1 parent fd8efa0 commit 153ea3e
Show file tree
Hide file tree
Showing 38 changed files with 817 additions and 902 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ baseframe-packed.css
baseframe-packed.js
error.log
imgee/static/uploads
imgee/static/test_uploads
*.bak
instance/production.env.sh
imgee/static/gen
26 changes: 16 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
env:
global:
env:
global:
- secure: |-
G5Dn+zkbN/BeNoopxtM2idC2Hy1ebJxRxprD7XEAmH6VO26ANgWYLI1dMrdH
uQc9F317STawQ/Um6KnqjErOKkC2BUYTOTj8AzoPVFz6NcK/Ca4d9Vfbtf5u
Expand All @@ -8,17 +8,23 @@ env:
SQ9lTuSD5jg3NSM8yMfMU58ppAYBgd+6VQP01wUPFrV/JSsbfgnVjMTm/Nbk
EGeHYvQUiyAg7zM4KdNJr6txj+jBE8MAeh7EwYNHoh9B7Vx//GxmXFnWyjXV
9cJkFroDW1Zfs2SZjLtzMQC8YXE30jmMxg+XHCQewKzRa4u1320=
# this is already being set in runtests.sh,
# but need to set it for `./manage.py init` too.
# could set it before running manage.py, but chose to put it here
- FLASK_ENV=TESTING
language: python
python:
python:
- 2.7
before_script:
- mkdir imgee/static/test_uploads
script:
- nosetests --with-coverage
install:
- sudo apt-get -qq install zlib1g-dev libfreetype6-dev liblcms1-dev libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev libtiff4-dev librsvg2-dev ghostscript imagemagick pandoc
services:
- redis-server
install:
- sudo apt-get -qq install zlib1g-dev libfreetype6-dev liblcms1-dev libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev libtiff4-dev librsvg2-dev ghostscript imagemagick pandoc inkscape
- pip install -r requirements.txt
- pip install nose coverage BeautifulSoup4 Pillow
- pip install -r test_requirements.txt
before_script:
- ./manage.py init # creates the test upload directory
script:
- ./runtests.sh
notifications:
email: false
slack:
Expand Down
27 changes: 8 additions & 19 deletions imgee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

# The imports in this file are order-sensitive

import os
from celery import Celery
import os.path

from werkzeug.utils import secure_filename

from flask import Flask, redirect, url_for
from flask_lastuser import Lastuser
Expand All @@ -16,41 +17,29 @@
version = Version(__version__)
app = Flask(__name__, instance_relative_config=True)
lastuser = Lastuser()
celery = Celery()

assets['imgee.css'][version] = 'css/app.css'

from . import models, views
from .models import db
from .api import api
from .async import TaskRegistry
from .tasks import TaskRegistry

registry = TaskRegistry()

def mkdir_p(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname)


# Configure the app

# Configure the application
coaster.app.init_app(app)
migrate = Migrate(app, db)
baseframe.init_app(app, requires=['baseframe', 'picturefill', 'imgee'])
lastuser.init_app(app)
lastuser.init_usermanager(UserManager(db, models.User))
registry.init_app(app)


@app.errorhandler(403)
def error403(error):
return redirect(url_for('login'))

if app.config.get('MEDIA_DOMAIN') and (
app.config['MEDIA_DOMAIN'].startswith('http:') or
app.config['MEDIA_DOMAIN'].startswith('https:')):
if app.config.get('MEDIA_DOMAIN', '').lower().startswith(('http://', 'https://')):
app.config['MEDIA_DOMAIN'] = app.config['MEDIA_DOMAIN'].split(':', 1)[1]
mkdir_p(app.config['UPLOADED_FILES_DEST'])

celery.conf.add_defaults(app.config)
registry.set_connection()
app.register_blueprint(api, url_prefix='/api/1')
app.upload_folder = os.path.join(app.static_folder, secure_filename(app.config.get('UPLOADED_FILES_DIR')))
60 changes: 0 additions & 60 deletions imgee/api.py

This file was deleted.

107 changes: 0 additions & 107 deletions imgee/async.py

This file was deleted.

16 changes: 8 additions & 8 deletions imgee/forms.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# -*- coding: utf-8 -*-

import os.path
from coaster.utils import make_name
from flask_wtf import Form
from baseframe.forms import Form
from wtforms.validators import Required, ValidationError, Length
from wtforms import (FileField, TextField, HiddenField,
SelectMultipleField, SelectField)
from wtforms import (FileField, TextField, HiddenField, SelectField)

from imgee import app
from imgee.models import Label
from imgee.utils import get_file_type, is_file_allowed
from .models import Label
from .utils import is_file_allowed


def valid_file(form, field):
if not is_file_allowed(field.data.stream):
if not is_file_allowed(field.data.stream, field.data.mimetype, field.data.filename):
raise ValidationError("Sorry, unknown image format. Please try uploading another file.")


Expand All @@ -28,8 +26,9 @@ class DeleteImageForm(Form):
class PurgeCacheForm(Form):
pass


def reserved_words():
"""get all words which can't be used as labels"""
"""Get all words which can't be used as labels"""
words = []
for rule in app.url_map.iter_rules():
r = rule.rule
Expand Down Expand Up @@ -67,6 +66,7 @@ class EditTitleForm(Form):
file_name = HiddenField('file_name')
file_title = TextField('title', validators=[Required(), Length(max=250)])


class UpdateTitle(Form):
title = TextField('Title', validators=[Required(), Length(max=250)])

Expand Down
2 changes: 1 addition & 1 deletion imgee/models/profile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from coaster.sqlalchemy import BaseNameMixin
from flask_lastuser.sqlalchemy import ProfileMixin
from imgee.models import db
from . import db


class Profile(ProfileMixin, BaseNameMixin, db.Model):
Expand Down
42 changes: 35 additions & 7 deletions imgee/models/stored_file.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
from flask import url_for

from coaster.sqlalchemy import BaseNameMixin, BaseScopedNameMixin
import imgee
from imgee.models import db
from imgee.utils import newid, guess_extension
from .. import app
from . import db
from ..utils import newid, guess_extension


image_labels = db.Table('image_labels',
Expand Down Expand Up @@ -58,7 +59,34 @@ def __repr__(self):
def extn(self):
return guess_extension(self.mimetype, self.orig_extn) or ''

def is_queued_for_deletion(self):
if imgee.app.config.get('CELERY_ALWAYS_EAGER'):
return False
return imgee.registry.is_queued_for_deletion(self.name+self.extn)
@property
def filename(self):
return self.name + self.extn

def dict_data(self):
return dict(
title=self.title,
uploaded=self.created_at.isoformat() + 'Z',
filesize=app.jinja_env.filters['filesizeformat'](self.size),
imgsize=u'%s×%s px' % (self.width, self.height),
url=url_for('view_image', profile=self.profile.name, image=self.name),
thumb_url=url_for('get_image', image=self.name, size=app.config.get('THUMBNAIL_SIZE'))
)

def add_labels(self, labels):
status = {
'+': [],
'-': [],
'': []
}

new_labels = set(labels)
old_labels = set(self.labels)
if new_labels != old_labels:
self.labels = labels

status['+'] = new_labels - old_labels # added labels
status['-'] = old_labels - new_labels # removed labels
status[''] = old_labels.intersection(new_labels) # unchanged labels

return status
4 changes: 2 additions & 2 deletions imgee/models/thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-

from coaster.sqlalchemy import BaseMixin
from imgee.models import db
from imgee.utils import newid
from . import db
from ..utils import newid


class Thumbnail(BaseMixin, db.Model):
Expand Down
Loading

0 comments on commit 153ea3e

Please sign in to comment.