daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest May 23rd, 2018 172 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.shortcuts import render
  2. from sqlalchemy import Column, Integer, String
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy.ext.declarative import DeclarativeMeta
  5. from sqlalchemy.ext.serializer import loads, dumps
  6.  
  7. # Create your views here.
  8. from django.http import HttpResponse, JsonResponse
  9. from django.views.decorators.csrf import csrf_exempt
  10. from sqlalchemy.ext.automap import automap_base
  11. from sqlalchemy.orm import Session
  12. from sqlalchemy import create_engine
  13. import json
  14. from rest_framework.parsers import JSONParser
  15. Base = declarative_base()
  16.  
  17.  
  18. def getuser():
  19.     Base = automap_base()
  20.     engine = create_engine("mysql://develop:Programista18@localhost:3306/WebCompilatorDb")
  21.     Base.prepare(engine, reflect=True)
  22.     session = Session(engine)
  23.  
  24.     # result = session.query(User_model.User, user.id == 1,  user)
  25.     result = session.query(User).filter_by(firstName="qwerty").first()
  26.     session.commit()
  27.     return json.dumps(result, cls=AlchemyEncoder)
  28.  
  29.  
  30. def save_user(request):
  31.     Base = automap_base()
  32.     engine = create_engine("mysql://develop:Programista18@localhost:3306/WebCompilatorDb")
  33.     Base.prepare(engine, reflect=True)
  34.     session = Session(engine)
  35.     user = User()
  36.     userjson = Payload(request)
  37.     user.password = userjson.password
  38.     user.firstName = userjson.firstName
  39.     user.lastName = userjson.lastName
  40.     user.email = userjson.email
  41.     user.salt = userjson.salt
  42.     # result = session.query(User_model.User, user.id == 1,  user)
  43.     result = session.add(user)
  44.     session.commit()
  45.     return json.dump(user.firstName)
  46.  
  47.  
  48. def login(request):
  49.     Base = automap_base()
  50.     engine = create_engine("mysql://develop:Programista18@localhost:3306/WebCompilatorDb")
  51.     Base.prepare(engine, reflect=True)
  52.     session = Session(engine)
  53.     # result = session.query(User_model.User, user.id == 1,  user)
  54.     result = session.query(User).one()
  55.     session.commit()
  56.     return json.dumps(result, cls=AlchemyEncoder)
  57.  
  58.  
  59. @csrf_exempt
  60. def user_list(request):
  61.     if request.method == 'GET':
  62.         return JsonResponse(getuser(), safe=False)
  63.     elif request.method == 'POST':
  64.         return JsonResponse(save_user(request), safe=False)
  65.  
  66.  
  67. @csrf_exempt
  68. def login(request):
  69.     if request.method == 'GET':
  70.         return JsonResponse(getuser(), safe=False)
  71.     elif request.method == 'POST':
  72.         return JsonResponse(login(str(request.data)), safe=False)
  73.  
  74.  
  75. class Payload(object):
  76.     def __init__(self, j):
  77.         self.__dict__ = json.loads(j)
  78.  
  79.  
  80. class AlchemyEncoder(json.JSONEncoder):
  81.     def default(self, obj):
  82.         if isinstance(obj.__class__, DeclarativeMeta):
  83.             # an SQLAlchemy class
  84.             fields = {}
  85.             for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
  86.                 data = obj.__getattribute__(field)
  87.                 try:
  88.                     json.dumps(data) # this will fail on non-encodable values, like other classes
  89.                     fields[field] = data
  90.                 except TypeError:
  91.                     fields[field] = None
  92.             # a json-encodable dict
  93.             return fields
  94.  
  95.         return json.JSONEncoder.default(self, obj)
  96.  
  97.  
  98. class User(Base):
  99.     __tablename__ = 'User'
  100.     id = Column(Integer, primary_key=True)
  101.     email = Column(String)
  102.     firstName = Column(String)
  103.     lastName = Column(String)
  104.     salt = Column(String)
  105.     password = Column(String)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top