Package: icecreamy
Table of ContentsIntroductionQuickstartRunning builtin serverBinding to gunicornEnv parametersWelcome PageIce-cream commandsMigration commandsAuthenticationFilteringRBAC-in-icecreamCachingRelationsFull Text SearchICE CREAMICE-CREAM framework for Bottle designed to simplify building restful API. It is structured such that any part of the core functionality can be customized to suit the needs of your project.Quickstart:#install it from Pypi:
pip install icecreamy

#to make icecream project, run:
make_project 

#make migration
python manage.py  makemigrations init

#to migrate
alembic upgrade head

#runing server
python manage.py runserverand access tohttp://localhost:8888/apiBuiltin server:To run bottle builtin server with commands:python manage.py runserver 
python manage.py runserver 127.0.0.1:8000Binding to Gunicorn:To bind icecream to gunicorn:gunicorn --workers=2  'manage:wsgi_app()'.Env Parameters:Copy and rename .env_example to .env and change the variable as project needs.
Or you can add the parameters manually into the .env file
To generate a .env file these values are required:Variable NameDescriptionhosticecream hostporticecream portdb_nameyour database db_namedb_typesqlite or postgresdb_useryour database usernamedb_passyour database passworddb_hostyour database hostdb_portyour database portproject_secretneeds for jwt authenticationjwt_ttljwt time to livesentry_dsnsentry address (logging tool), it can bemedia_filesstatic media folderalready ice-cream is working with PostgresWelcome Page:Now you need to check that your website is running. Open your browser (Firefox, Chrome, Safari, Internet Explorer or whatever you use) and enter this address:browserhttp://127.0.0.1:8000/apiCongratulations! You've just created your first website and run it using a web server!ice-cream commands:To create superuser:python manage.py createsuperuserTo create new app:python manage.py startapp app_name
then register app in settings.pyMigration Commands:To make migration:python manage.py  makemigrations your_messageTo migrate:alembic upgrade headAuthentication:Unlike some more typical uses of JWTs, this module only generates authentication tokens that will verify the user who is requesting one of your ICECREAM protected API resources. The actual request parameters themselves are not included in the JWT claims which means they are not signed and may be tampered with. To implement user authentication in your application, you need to override the AuthBackend() class in authentication.py in the users folder.
to obtaining token and refresh token it needs to get it from the route allocated in JWTProviderPluginTo using authentication needs to using . Add the following URL pattern:core.route('/users', 'GET', get_users, apply=[jwt_auth_required])Filtering:ICECREAM is using py-mongosql to apply filters query
to get a query from URL need to use this functionquery = get_query_from_url("query")then apply query into MongoFilter like what we did in foo_appfiltered_query = MongoFilter(Room, rooms_query, query).filter()The following are some quick examples of making filtered GET requests from different types of clients. More complete documentation is in the subsequent link. In these examples, each client will filter by instances of the model Room which sorted by name.http://127.0.0.1:8000/rooms/filter?query={"sort":"name-",}This link has more complete versions of these examples.py-mongosqlFile serving:To serving files first  need to create a static folder in the root of the project:Create a folder like :
/statics/media/After that register the address in the .env:media_files = /statics/media/RBAC in ICECREAM:Role-based User Access Controlin ICECREAM an access control model is abstracted into two CSV files. So switching or upgrading the authorization mechanism for a project is just as simple as modifying a CSV file. You can customize your own access control model by combining the available models.
we assume we had 2 roles.we define roles into roles.csv :adminstaffin the next step we will define our policy in model_rules:roleoperationobject_you_want to modifylike this:staffcreatemessageso in the last step we pass the Model to the ACLHandler
and pass the current user to check permission as bellow:aclh = ACLHandler(Resource=Message)
identity = aclh.get_identity(current_user)Caching in ICECREAM:Caching refers to storing the server response in the client itself so that a client need not make a server request for the same resource again and again.ICECREAM using Redis to do caching, Redis has more sophisticated mechanisms as it has been described as a "data structure store", thus making it more powerful and flexible. Redis also has a larger advantage because you can store data in any form. In the ICECREAM When the function runs, it checks if the view key is in the cache. If the key exists, then the app retrieves the data from the cache and returns it. If not, ICECREAM queries the database and then stashes the result in the cache with the view key. The first time this function is run, ICECREAM will query the database and render the template, and then will also make a network call to Redis to store the data in the cache. Each subsequent call to the function will completely bypass the database and business logic and will query the Redis cache.
To cache view functions you will use the cache_for(), decorator.@cache_for(24 * 60, cache_key_func='full_path')
    def get_user(pk, db_session):
        user = get_object_or_404(User, db_session, User.id == pk)
        result = user_serializer.dump(user)
        return HTTPResponse(status=200, body=result)or you can pass the decorator in the routercore.route('/users', 'GET', get_users,apply=[cache_for(24 * 60, cache_key_func='full_path')])Relations in ICECREAM:ICECREAM is using SQLAlchemy ORM to see how the relationship is working
you can check this gist quicklySQLAlchemy basic relationship patternsFull text search in ICECREAM:Full-text search is a more advanced way to search a database. Full-text search quickly finds all instances of a term (word) in a table without having to scan rows and without having to know which column a term is stored in. Full-text search works by using text indexes.
to provide full-text search ICECREAM integrated with SQLAlchemy-Searchable, it provides full-text search capabilities for SQLAlchemy models. Currently, it only supports PostgreSQL.
to start a full-text search first we should follow these steps:as the first step we should define the model in-app :class Article(Base):
    __tablename__ = 'article'
    id = Column(Integer, primary_key=True)
        name = Column(Unicode(255))
        content = Column(sa.UnicodeText)
        search_vector = Column(TSVectorType('name', 'content'))after define models, need to add these lines to ICECREAM settings:searches_index = [
    ('article', 'search_vector', ['name','content'])
]and in the last step, it can be sweet as an icecreamarticle1 = Article(name=u'First article', content=u'This is the first article')
article2 = Article(name=u'Second article', content=u'This is the second article')
session.add(article1)
session.add(article2)
session.commit()

query = session.query(Article)

query = search(query, 'first')

print query.first().name
# First articleOptionally specify sort=True to get results in order of relevance (ts_rank_cd):query = search(query, 'first', sort=True)When making changes to your database schema you have to make sure the associated search triggers and trigger functions get updated also. ICECREAM offers a helper command called index_search for this. to perform this, its calling SQLAlchemy-Searchable  sync_trigger after everyalembic upgrade headto perform search trigger, should run this command  :python manage.py index_search