Commit 0df6b588 authored by ishan masdekar's avatar ishan masdekar
Browse files

added enumdict error handler in organisation api

parent 8752f7c7
...@@ -31,7 +31,7 @@ Contributor: ...@@ -31,7 +31,7 @@ Contributor:
from pyramid.view import view_defaults, view_config from pyramid.view import view_defaults, view_config
from gkcore import eng from gkcore import eng, enumdict
from gkcore.models import gkdb from gkcore.models import gkdb
from sqlalchemy.sql import select, distinct from sqlalchemy.sql import select, distinct
import json import json
...@@ -49,21 +49,27 @@ class api_organisation(object): ...@@ -49,21 +49,27 @@ class api_organisation(object):
@view_config(request_method='GET', renderer ='json') @view_config(request_method='GET', renderer ='json')
def getOrgs(self): def getOrgs(self):
result = con.execute(select([gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype]).distinct()) try:
orgs = [] result = con.execute(select([gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype]).distinct())
for row in result: orgs = []
orgs.append({"orgname":row["orgname"], "orgtype":row["orgtype"]}) for row in result:
print orgs orgs.append({"orgname":row["orgname"], "orgtype":row["orgtype"]})
return orgs print orgs
return {"gkstatus":enumdict["Success"], "gkdata":orgs}
except:
return {"gkstatus":enumdict["ConnectionFailed"]}
@view_config(route_name='orgyears', request_method='GET', renderer ='json') @view_config(route_name='orgyears', request_method='GET', renderer ='json')
def getYears(self): def getYears(self):
result = con.execute(select([gkdb.organisation.c.yearstart, gkdb.organisation.c.yearend,gkdb.organisation.c.orgcode]).where(and_(gkdb.organisation.c.orgname==self.request.matchdict["orgname"], gkdb.organisation.c.orgtype == self.request.matchdict["orgtype"]))) try:
years = [] result = con.execute(select([gkdb.organisation.c.yearstart, gkdb.organisation.c.yearend,gkdb.organisation.c.orgcode]).where(and_(gkdb.organisation.c.orgname==self.request.matchdict["orgname"], gkdb.organisation.c.orgtype == self.request.matchdict["orgtype"])))
for row in result: years = []
years.append({"yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcode":row["orgcode"]}) for row in result:
print years years.append({"yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcode":row["orgcode"]})
return years print years
return {"gkstatus":enumdict["Success"],"gkdata":years}
except:
return {"gkstatus":enumdict["ConnectionFailed"]}
@view_config(request_method='POST',renderer='json') @view_config(request_method='POST',renderer='json')
def postOrg(self): def postOrg(self):
...@@ -159,24 +165,29 @@ class api_organisation(object): ...@@ -159,24 +165,29 @@ class api_organisation(object):
record = result.fetchone() record = result.fetchone()
token = jwt.encode({"orgcode":userdata["orgcode"],"userid":record["userid"]},gkcore.secret,algorithm='HS256') token = jwt.encode({"orgcode":userdata["orgcode"],"userid":record["userid"]},gkcore.secret,algorithm='HS256')
return {"status":"ok","token":token } return {"gkstatus":enumdict["Success"],"token":token }
else: else:
return {"status":"invalid"} return {"gkstatus":enumdict["ConnectionFailed"]}
else: else:
return False return {"gkstatus":enumdict["ConnectionFailed"]}
except: except:
result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==orgcode["orgcode"])) result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==orgcode["orgcode"]))
return {"gkstatus":enumdict["ConnectionFailed"]}
else: else:
return False return {"gkstatus":enumdict["ConnectionFailed"]}
except: except:
return False return {"gkstatus":enumdict["ConnectionFailed"]}
@view_config(route_name='organisation', request_method='GET',renderer='json') @view_config(route_name='organisation', request_method='GET',renderer='json')
def getOrg(self): def getOrg(self):
result = con.execute(select([gkdb.organisation]).where(gkdb.organisation.c.orgcode==self.request.matchdict["orgcode"])) try:
row = result.fetchone() result = con.execute(select([gkdb.organisation]).where(gkdb.organisation.c.orgcode==self.request.matchdict["orgcode"]))
orgDetails={"orgname":row["orgname"], "orgtype":row["orgtype"], "yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcity":row["orgcity"], "orgaddr":row["orgaddr"], "orgpincode":row["orgpincode"], "orgstate":row["orgstate"], "orgcountry":row["orgcountry"], "orgtelno":row["orgtelno"], "orgfax":row["orgfax"], "orgwebsite":row["orgwebsite"], "orgemail":row["orgemail"], "orgpan":row["orgpan"], "orgmvat":row["orgmvat"], "orgstax":row["orgstax"], "orgregno":row["orgregno"], "orgregdate":row["orgregdate"], "orgfcrano":row["orgfcrano"], "orgfcradate":row["orgfcradate"], "roflag":row["roflag"], "booksclosedflag":row["booksclosedflag"] } row = result.fetchone()
return orgDetails orgDetails={"orgname":row["orgname"], "orgtype":row["orgtype"], "yearstart":str(row["yearstart"]), "yearend":str(row["yearend"]),"orgcity":row["orgcity"], "orgaddr":row["orgaddr"], "orgpincode":row["orgpincode"], "orgstate":row["orgstate"], "orgcountry":row["orgcountry"], "orgtelno":row["orgtelno"], "orgfax":row["orgfax"], "orgwebsite":row["orgwebsite"], "orgemail":row["orgemail"], "orgpan":row["orgpan"], "orgmvat":row["orgmvat"], "orgstax":row["orgstax"], "orgregno":row["orgregno"], "orgregdate":row["orgregdate"], "orgfcrano":row["orgfcrano"], "orgfcradate":row["orgfcradate"], "roflag":row["roflag"], "booksclosedflag":row["booksclosedflag"] }
return {"gkstatus":enumdict["Success"],"gkdata":orgDetails}
except:
return {"gkstatus":enumdict["ConnectionFailed"]}
@view_config(request_method='PUT', renderer='json') @view_config(request_method='PUT', renderer='json')
def putOrg(self): def putOrg(self):
token = self.request.headers['gktoken'] token = self.request.headers['gktoken']
...@@ -184,7 +195,7 @@ class api_organisation(object): ...@@ -184,7 +195,7 @@ class api_organisation(object):
if authDetails["auth"]==False: if authDetails["auth"]==False:
return {"gkstatus":enumdict["UnauthorisedAccess"]} return {"gkstatus":enumdict["UnauthorisedAccess"]}
else: else:
try: try:
user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] )) user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
userRole = user.fetchone() userRole = user.fetchone()
dataset = self.request.json_body dataset = self.request.json_body
...@@ -194,8 +205,9 @@ class api_organisation(object): ...@@ -194,8 +205,9 @@ class api_organisation(object):
return {"gkstatus":enumdict["Success"]} return {"gkstatus":enumdict["Success"]}
else: else:
{"gkstatus": enumdict["BadPrivilege"]} {"gkstatus": enumdict["BadPrivilege"]}
except: except:
return {"gkstatus": enumdict["ConnectionFailed"]} return {"gkstatus": enumdict["ConnectionFailed"]}
@view_config(request_method='DELETE', renderer='json') @view_config(request_method='DELETE', renderer='json')
def deleteOrg(self): def deleteOrg(self):
token = self.request.headers['gktoken'] token = self.request.headers['gktoken']
...@@ -203,7 +215,7 @@ class api_organisation(object): ...@@ -203,7 +215,7 @@ class api_organisation(object):
if authDetails["auth"]==False: if authDetails["auth"]==False:
return {"gkstatus":enumdict["UnauthorisedAccess"]} return {"gkstatus":enumdict["UnauthorisedAccess"]}
else: else:
try: try:
user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] )) user=con.execute(select([gkdb.users.c.userrole]).where(gkdb.users.c.userid == authDetails["userid"] ))
userRole = user.fetchone() userRole = user.fetchone()
if userRole[0]==-1: if userRole[0]==-1:
...@@ -212,12 +224,15 @@ class api_organisation(object): ...@@ -212,12 +224,15 @@ class api_organisation(object):
return {"gkstatus":enumdict["Success"]} return {"gkstatus":enumdict["Success"]}
else: else:
{"gkstatus": enumdict["BadPrivilege"]} {"gkstatus": enumdict["BadPrivilege"]}
except: except:
return {"gkstatus": enumdict["ConnectionFailed"]} return {"gkstatus": enumdict["ConnectionFailed"]}
@view_config(route_name='orgid', request_method='GET',renderer='json') @view_config(route_name='orgid', request_method='GET',renderer='json')
def getOrgCode(self): def getOrgCode(self):
result = con.execute(select([gkdb.organisation.c.orgcode]).where(and_(gkdb.organisation.c.orgname==self.request.matchdict["orgname"], gkdb.organisation.c.orgtype==self.request.matchdict["orgtype"], gkdb.organisation.c.yearstart==self.request.matchdict["yearstart"], gkdb.organisation.c.yearend==self.request.matchdict["yearend"]))) try:
row = result.fetchone() result = con.execute(select([gkdb.organisation.c.orgcode]).where(and_(gkdb.organisation.c.orgname==self.request.matchdict["orgname"], gkdb.organisation.c.orgtype==self.request.matchdict["orgtype"], gkdb.organisation.c.yearstart==self.request.matchdict["yearstart"], gkdb.organisation.c.yearend==self.request.matchdict["yearend"])))
orgcode={"orgcode":row["orgcode"]} row = result.fetchone()
return orgcode orgcode={"orgcode":row["orgcode"]}
return {"gkstatus":enumdict["Success"],"gkdata":orgcode}
except:
return {"gkstatus":enumdict["ConnectionFailed"]}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment