Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #99 from mostafa/feature/grest-cmd
Browse files Browse the repository at this point in the history
feature/grest-cmd
  • Loading branch information
mostafa committed Feb 2, 2019
2 parents afcfd5c + e11510f commit 7dd6940
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 15 deletions.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -55,6 +55,13 @@ You can find an example app in [examples](https://github.com/mostafa/grest/tree/
+ `app.py` is a simple grest app that contains only one route `(/persons)`.
+ `extended_app.py` is the extended version of the above app and contains another route `(/pets)`, its relationship with `Person` model and a custom method (route) to handle `RelationshipFrom` properties. The `RelationshipTo` is automatically constructed using secondary model and secondary selection field of the `PersonsView`.

## grest Command
The package ships a very simple command that can help you create a boilerplate Flask application. Simply run the following command:

```bash
grest <project_name>
```

## Usage
In order to build an API, you should make a simple Flask app (or you may already have one).

Expand Down
44 changes: 44 additions & 0 deletions bin/grest
@@ -0,0 +1,44 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
# This file is part of grest.
#
# grest is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# grest is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with grest. If not, see <http://www.gnu.org/licenses/>.
#

import sys
from cookiecutter.main import cookiecutter

try:
from pathlib import Path
except ImportError:
from pathlib2 import Path


if __name__ == "__main__":
try:
if len(sys.argv) < 2:
raise Exception("Usage: grest PROJECT_NAME")

project_path = Path(sys.argv[1]).resolve()
if project_path.exists():
raise Exception(
"[ERROR] Directory exists. Consider using a different project name.")

template = Path(__file__).resolve().parent.joinpath("template")
cookiecutter(str(template), overwrite_if_exists=False)
except Exception as e:
print(str(e))
14 changes: 14 additions & 0 deletions bin/template/cookiecutter.json
@@ -0,0 +1,14 @@
{
"app_name": "api",
"grest_version": "1.2.0",
"DEBUG": false,
"IP_ADDRESS": "localhost",
"PORT": "5000",
"NEO4J_USERNAME": "NEO4J",
"NEO4J_PASSWORD": "NEO4J",
"NEO4J_HOSTNAME": "localhost",
"NEO4J_BOLT_PORT": 7687,
"SECRET_KEY": "",
"X_AUTH_TOKEN": "X-Auth-Token",
"ENABLE_DELETE_ALL": false
}
17 changes: 17 additions & 0 deletions bin/template/{{cookiecutter.app_name}}/README.md
@@ -0,0 +1,17 @@
# gREST Boilerplate

## Install dependencies

For installing dependencies, use the following command:

```bash
pip install -r requirements.txt
```

## Running application

For running the application, use the following command:

```bash
python main.py
```
Empty file.
17 changes: 17 additions & 0 deletions bin/template/{{cookiecutter.app_name}}/config.py
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-

import os

DEBUG = os.getenv("DEBUG", {{cookiecutter.DEBUG}})

IP_ADDRESS = os.getenv("IP_ADDRESS", "{{cookiecutter.IP_ADDRESS}}")
PORT = os.getenv("PORT", {{cookiecutter.PORT}})

VERSION = os.getenv("VERSION", "0.0.1")
SECRET_KEY = os.getenv("SECRET_KEY", "{{cookiecutter.SECRET_KEY}}")
DB_URL = os.getenv(
"DB_URL", "bolt://{{cookiecutter.NEO4J_USERNAME}}:{{cookiecutter.NEO4J_PASSWORD}}@{{cookiecutter.NEO4J_HOSTNAME}}:{{cookiecutter.NEO4J_BOLT_PORT}}")

X_AUTH_TOKEN = "{{cookiecutter.X_AUTH_TOKEN}}"
ENABLE_DELETE_ALL = os.getenv(
"ENABLE_DELETE_ALL", "{{cookiecutter.ENABLE_DELETE_ALL}}")
21 changes: 21 additions & 0 deletions bin/template/{{cookiecutter.app_name}}/main.py
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-

import neomodel
from flask import Flask

import config
from users import UsersView


if __name__ == "__main__":
app = Flask(__name__)

neomodel.config.DATABASE_URL = config.DB_URL
neomodel.config.AUTO_INSTALL_LABELS = True

UsersView.register(app, route_base="/users", trailing_slash=False)

app.run(debug=config.DEBUG,
host=config.IP_ADDRESS,
port=config.PORT,
threaded=True)
1 change: 1 addition & 0 deletions bin/template/{{cookiecutter.app_name}}/requirements.txt
@@ -0,0 +1 @@
pygrest=={{cookiecutter.grest_version}}
19 changes: 19 additions & 0 deletions bin/template/{{cookiecutter.app_name}}/schema.py
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-

from neomodel import StringProperty, StructuredNode, UniqueIdProperty
from webargs import fields
from grest import models


class User(StructuredNode, models.Node):
"""
Person model
"""
__validation_rules__ = {
"username": fields.Str(),
"password": fields.Str()
}

uid = UniqueIdProperty()
username = StringProperty()
password = StringProperty()
9 changes: 9 additions & 0 deletions bin/template/{{cookiecutter.app_name}}/users.py
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-

from grest import GRest
from schema import User


class UsersView(GRest):
__models__ = {"primary": User}
__selection_field__ = {"primary": "uid"}
2 changes: 1 addition & 1 deletion examples/app.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 1 addition & 1 deletion examples/extended_app.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
4 changes: 2 additions & 2 deletions grest/__init__.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand All @@ -20,4 +20,4 @@

from .grest import GRest

__version__ = '1.1.0'
__version__ = '1.2.0'
2 changes: 1 addition & 1 deletion grest/auth.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 1 addition & 1 deletion grest/exceptions.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 1 addition & 1 deletion grest/global_config.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 1 addition & 1 deletion grest/grest.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 1 addition & 1 deletion grest/models.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 1 addition & 1 deletion grest/utils.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 1 addition & 1 deletion grest/validation.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Expand Up @@ -14,3 +14,5 @@ webargs==5.1.1
Werkzeug==0.14.1
PyYAML==4.2b4
simplejson==3.16.0
pathlib2==2.3.3
cookiecutter==1.6.0
3 changes: 2 additions & 1 deletion setup.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down Expand Up @@ -66,4 +66,5 @@
],
setup_requires=['pytest-runner'],
tests_require=['pytest', 'pytest-flask'],
scripts=['bin/grest'],
zip_safe=False)
2 changes: 1 addition & 1 deletion tests/test_app.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 1 addition & 1 deletion tests/test_extended_app.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down
2 changes: 1 addition & 1 deletion tests/test_validation_rules_property.py
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017-2019 Mostafa Moradian <mostafamoradian0@gmail.com>
#
Expand Down

0 comments on commit 7dd6940

Please sign in to comment.