Using sqlalchemy declarative in the external worker - heroku

I have a web app written in python using Flask and sqlalchemy. This application runs on Heroku, and as a worker I use the ironworker extension, I have all my models defined in the models module like this:
from app import db
class Player(db.Model):
__tablename__ = 'players'
id = db.Column(db.Integer, primary_key=True)
....
type = db.Column(db.String(50))
def __init__(self, ....):
....
__mapper_args__ = {
'polymorphic_identity':'player',
'polymorphic_on':type
}
class Outfielder(Player):
__tablename__ = 'outfielders'
id = db.Column(db.Integer, db.ForeignKey('players.id'), primary_key=True)
__mapper_args__ = {
'polymorphic_identity':'outfielder'
}
class Goalkeeper(Player):
__tablename__ = 'goalkeepers'
id = db.Column(db.Integer, db.ForeignKey('players.id'), primary_key=True)
__mapper_args__ = {
'polymorphic_identity':'goalkeeper'
}
(Note how this imports db from app)
I can create the instances of these models in my application by instantiating the models, like so:
db = SQLAlchemy(app)
gk = models.Goalkeeper('John', 'Snow', 1, 1, 3456, 67, 55, 187)
of = models.Outfielder('Gary', 'Lineker', 1, 1, 3999, 77, 78, 176)
db.session.add(gk)
db.session.add(of)
db.session.commit()
Now, what I would like to do is to be able to create the instances of these models on my external worker. The only thing that prevents me from doing so is the dependency on my app module. I don't feel like this dependency is justified and would like to find a better design approach to be able to simply export my models module to the worker and init sqlalchemy instance there and create instances of my models and save them to the database exactly the same way I can do it in the application itself.
Note that I can already access my database from the worker like this:
db = create_engine(db_url)
metadata = MetaData(db)
print "connection to the database established"
players = Table('players', metadata, autoload=True)
s = players.select()
I'm just looking for a better way of reflecting my model structure on the worker. Specifically, I'm thinking of the scenario when my models change and I'll have to maintain the code on both ends separately. If I manage to reuse the models and use declarative on both ends, then there will be no maintenance on the worker side what so ever.

The db.Model is a SQLAlchemy declarative_base(), so you can use your models like even with plain SQLAlchemy.
This example works (YourModel is a Flask-SQLAlchemy model):
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import settings
from models import YourModel
engine = create_engine(settings.SQLALCHEMY_DATABASE_URI)
Session = sessionmaker(bind=engine)
session = Session()
# do something with your session
session.query(YourModel).first()
You have to decouple db from app, so you can import it without triggering app creation, using db.init_app.
Of course you should be careful here. If you load your settings dynamically, maybe you'll load it from the wrong place with import settings instead of current_app.config, but looks like you already have your worker adressed, so it is not an issue for you.
Another option is create another Flask app instead of your huge web app.
From Flask-SQLAlchemy docs (http://pythonhosted.org/Flask-SQLAlchemy/api.html#flask.ext.sqlalchemy.SQLAlchemy):
You can still use sqlalchemy and sqlalchemy.orm directly, but note
that Flask-SQLAlchemy customizations are available only through an
instance of this SQLAlchemy class. Query classes default to BaseQuery
for db.Query, db.Model.query_class, and the default query_class for
db.relationship and db.backref. If you use these interfaces through
sqlalchemy and sqlalchemy.orm directly, the default query class will
be that of sqlalchemy.
EDIT: One additional thing to remember when mixing sqlalchemy session with flask-sqlalchemy models is that the flask-sqlalchemy session object is actually a subclass, with added _model_changes dictionary. As per this answer, you won't be able to commit the session if you don't add the _model_changes field:
def create_session(config):
engine = create_engine(config['DATABASE_URI'])
Session = sessionmaker(bind=engine)
session = Session()
session._model_changes = {}
return session

Related

How to serialize a Django model using Graphene-Django outside the context of a graphQL query

I'm using Graphene-Django to build a GraphQL API, and I've defined object types as explained in the docs. By way of example,
import graphene
from graphene_django import DjangoObjectType
from .models import Question
class QuestionType(DjangoObjectType):
class Meta:
model = Question
fields = ("id", "question_text")
class Query(graphene.ObjectType):
questions = graphene.List(QuestionType)
question_by_id = graphene.Field(QuestionType, id=graphene.String())
def resolve_questions(root, info, **kwargs):
# Querying a list
return Question.objects.all()
def resolve_question_by_id(root, info, id):
# Querying a single question
return Question.objects.get(pk=id)
This works fine for fetching the content of a Question model in a GraphQL query, but I'd also like to serialize my Question objects to a JSON representation in log files and tests. I can write a separate utility function, but it seems redundant when I already have an existing GraphQL schema that defines which objects fields I'm interested in and how to serialize them.
Basically, I'm looking for a Graphene analogue to using a DRF serializer
serializer = CommentSerializer(comment)
serializer.data
# {'email': 'leila#example.com', 'content': 'foo bar', 'created': '2016-01-27T15:17:10.375877'}
Ideally, I'm looking for something like
from my_app.schema import QuestionType
from my_app.models import Question
question_object = Quest.objects.get(pk=1)
data = QuestionType.to_dict(question_object)
I've looked at the Graphene docs, and poked around the source on Github but it all seems very obfuscated and I can't find an obvious way to do what I want.
Can anyone offer a concise example of using a Graphene ObjectType to serialize a single model instance outside the entire GraphQL query context?

Serialize available choices and mark selected

I am new to REST and django-rest-framework. I want to get list of available ManyToMany choices along with some way to know which ones are currently selected.
I have model like this:
class PGroup(models.Model):
.
permissions = models.ManyToManyField(
Permission, related_name="group_permissions", help_text=_('Select permissions for this group.')
)
Serializers.
class PermissionSerializer(serializers.ModelSerializer):
class Meta:
model = Permission
fields = ['pk', 'name',]
class PGroupSerializer(serializers.ModelSerializer):
permissions = PermissionSerializer(many=True)
class Meta:
model = PGroup
fields = [....'permissions']
Looking at Browseable API, with this setup I get 'permissions: []'(empty list) for generics.createAPIView and get the associated 'permissions[....]'(non-empty list) for generics.RetrieveUpdateAPIView.
I want a list of available permissions on both API views and also want to know which permissions are already selected for Update API view.
Can anyone please help.
Thanks
There are 2 ways to get the list of choices.
Using the SerializerMethodField,
from rest_framework import serializers
from .models import Permission
class PGroupSerializer(serializers.ModelSerializer):
permissions = PermissionSerializer(many=True)
all_available_permissions = serializers.SerializerMethodField()
def get_all_available_permissions(self, obj):
return Permission.objects.all()
class Meta:
model = PGroup
fields = ['permissions', "all_available_permissions"]
or using source, we can define a custom method on the model and point the serializer to use it using the source argument.
### models.py
class PGroup(models.Model):
.
permissions = models.ManyToManyField(
Permission, related_name="group_permissions", help_text=_('Select permissions for this group.')
)
def all_permissions(self):
return Permission.objects.all()
### serializers.py
class PGroupSerializer(serializers.ModelSerializer):
permissions = PermissionSerializer(many=True)
all_available_permissions = PermissionSerializer(many=True, read_only=True, source="all_permissions")
class Meta:
model = PGroup
fields = ['permissions', "all_available_permissions"]
2nd option is much better, IMO.
Note: you may not always want to send a full list of choices as that could get really slow overtime when u have hundreds or thousands of objects.

Why we need serializers while we can return Response as Json without any serialzer in django rest framework?

I'm just confuse that while we can return json data as Response without using serialzers then why we use serializers?
Here is a simple user profile view which i am rendering without using serialzer:
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
#api_view(['GET'])
#permission_classes([IsAuthenticated])
def view_profile(request):
response_data = {}
if request.method == 'GET':
user = request.user
try:
get_user = User.objects.get(email=user.email)
response_data['username'] = get_user.username
response_data['email'] = get_user.email
except Exception as e:
print(e)
response_data['response'] = str(e)
return Response(response_data)
What is the main purpose of using serialzers?
You don't need serializers for the sake of having serializers.
However, they help in several ways.
They convert complex structures -datetimes, Model instances- into more simple ones -strings, dicts, lists, integers and floats.
They also provide a structure to build schemas (OpenAPI for example) or allow the BrowsableAPI to interact with the web service through HTML forms deduced from serializers.

How to write a generic CRUD service on django using rest framework?

I am new to django and faced to several problems trying to write a simple service.
What am I trying to do?
I intend to write a generic crud service for my models using rest-framework library.
I don't want to write serializers and views to all my models and trying to optimize code and learn some useful stuff.
My model
Let's imagine I have an abstract BaseBusinessObject
class BaseBusinessObject(models.Model):
CreatedAt = models.DateField()
UpdatedAt = models.DateField()
class Meta:
abstract = True
I also have a plenty of concrete classes, which are inherited from base one:
class Product(BaseBusinessObject):
Description: models.TextField()
Type: models.CharField()
....
class Company(BaseBusinessObject):
Title: models.CharField()
....
class Person(BaseBusinessObject):
Name: models.CharField()
and so on
What I want
I already figured out, that with rest-framework I can create serializers and views, then register router for url .../Product, .../Company, .../Person. But what if I have 1000 classes? This is boring
A. How can I dynamically specified url's for child objects? I don't want to hardcode methods, I am looking for solution...something like this:
.../api/Entities/ClassName
B. How can I then use my dynamically created urls in django-rest-framework?
router.register('persons', PersonViewSet)
How can write it in more generic way?
router.register('<ClassName>', <GenericViewSet>)
C. Using DRF I can create my viewset for each concrete class in my model:
class PersonViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
But as I said I have a lot of classes. How can I write it in more generic way?
I tried to create a view set for abstract class, but there are some trouble when querying an abstract object.
Is it possible to create such service for an abstract class and then all its child simply(or not simply) inherit CRUD methods?
Maybe I should try to write a factory for serializers and viewsets?
What possible solutions could I implement for solving my problem?
After 2 days of walking around I finally find my solution. May be someone else will face the some problem, so I trying to explain what I had already done.
First, I create a "base" application inside my django project and add it to settings.py. Then I create an abstract class in models.py:
class BaseCrudEntity(models.Model):
pass
class Meta:
abstract = True
I want to write a generic service for CRUD operations for all "business" classes.
The problem is that I don't want to write serializers and views for them - I want to create them "on fly", dynamically. I decided to use django rest framework as well, because I am not intended to create a bycicle again.
I decided to inherit all my "business" classes from that abstract one and write a service for all possible "families"
So I have to create a fabric which is responsible for VeiwSet creation.
Here is my view.py:
class BaseCrudViewSetFabric():
#classmethod
def CreateViewSet(self, _context):
classname = _context.__name__ + 'ViewSet'
return type(classname, (viewsets.ModelViewSet,), {
'queryset':_context.objects.all(),
'serializer_class':BaseCrudSerializerFabric.CreateSrializer(_context)
})
pass
here _context - variable which describes concrete class of my model.
as you can see this function creates a concrete ViewSet based on my context. Inside it a Serializers fabric is called.
Here the code of my serializers.py:
class BaseCrudSerializerFabric():
#classmethod
def CreateSrializer(self, _context):
classname = _context.__name__
_Meta = type('Meta', (), {'model':_context,'fields':'__all__'})
_crudserializer = type(
classname,
(serializers.ModelSerializer,),
{'Meta': _Meta}
)
return _crudserializer
Moreover, I have to write a service for dynamically routing - I don't wanna hardcode my urls.
Here the example ursl.py from core project:
from base.urls import router
url(r'^api/v1/', include(router.urls))
and from base/urls.py:
from rest_framework.routers import DefaultRouter, SimpleRouter
from base.models.BaseCrudEntity import BaseCrudEntity
from base.views.basecrud_view import BaseCrudViewSetFabric
class CustomRouter(SimpleRouter):
def RoutRegister(self):
childs = getChilds(BaseCrudEntity)
#print(childs)
for ch in childs:
if (ch._meta.abstract == False):
#print(ch.__name__)
prefix = ch.__name__
self.register(prefix, BaseCrudViewSetFabric.CreateViewSet(ch))
return(self)
pass
router = CustomRouter()
router.RoutRegister()
Finally I simply create some concrete models:
from django.db import models
from base.models.BaseCrudEntity import BaseCrudEntity
class Person(BaseCrudEntity):
Name = models.CharField(max_length = 255)
Surname = models.CharField(max_length = 255)
Patronymic = models.CharField(max_length = 255, null = True)
DateOfBirth = models.DateField(null = True)
#slug = models.SlugField(default = 'hui', editable = False)
def __str__(self):
return "{} {} {}".format (self.Surname, self.Name, self.Patronymic)
and thats all.
When application starts it register a route for http://127.0.0.1:8000/api/v1/Person and creates serializers and viewsets so all CRUD operations provided by Django- Rest Framework will be provided as well.
I would suggest using query parameters instead of path parameters, then you just register one URL and process the various elements of the request and route it correctly server-side. When do I use path params vs. query params in a RESTful API?

How to use a custom id with Graphene and Relay?

I've implemented graphql and I'm migrating to relay. I already have a uuid for every table and that is named 'id'. And my application I found this github thread that talks about possibly changing the spec but it feels like a rabbit hole.
Is there a simple way that I can use my own custom id with relay?
If you've already implemented a default relay endpoint then you should have some
TableNameNode classes that have a Meta nested class, and a seperate Query
class.
class ExampleTableNameNode(DjangoObjectType):
class Meta:
model = ExampleTableName
interface = (relay.Node,)
class Query(object):
example_table_name = relay.Node.Field(ExampleTableNameNode)
all_example_table_names = DjangoFilterConnectionField(ExampleTableNameNode)
def resolve_example_table_name(self, info, **kwargs):
pass
def resolve_all_example_table_names(self, info, **kwargs):
pass
The interface = (relay.Node,) is what defines:
How the ids are being generated
How they are used to fetch data
If we create a relay.Node subclass that redefines these two features then we can use our custom ids.
class CustomNode(relay.Node):
class Meta:
name = 'Node'
#staticmethod
def to_global_id(type, id):
#returns a non-encoded ID
return id
#staticmethod
def get_node_from_global_id(info, global_id, only_type=None):
model = getattr(Query,info.field_name).field_type._meta.model
return model.objects.get(id=global_id)
Here we implemented two functions, to_global_id, and get_node_from_global_id.
The line model = ... is a bit of magic to go from the graphql query table name
to the actual model. If that doesn't work you'll just need to make a dictionary
to go from something like example_table_name to the actual ExampleTableName
django model.
Once you do that you'll have to replace the two references to relay.Node with
CustomNode like so.
class ExampleTableNameNode(DjangoObjectType):
class Meta:
model = ExampleTableName
interface = (CustomNode,)
class Query(object):
example_table_name = CustomNode.Field(ExampleTableNameNode)
all_example_table_names = DjangoFilterConnectionField(ExampleTableNameNode)
def resolve_example_table_name(self, info, **kwargs):
pass
def resolve_all_example_table_names(self, info, **kwargs):
pass
The answer is in the graphene docs. I read them when I was implementing
graphene and relay but there is so much to learn at once that it's easy to read
through custom node section and not remember later that you need to do a custom
node solution.

Resources