api_organisation.py 4.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32



"""
Copyright (C) 2014 2015 2016 Digital Freedom Foundation


  This file is part of GNUKhata:A modular,robust and Free Accounting System.

  GNUKhata 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.and old.stockflag = 's'

  GNUKhata 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 GNUKhata (COPYING); if not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA  02110-1301  USA59 Temple Place, Suite 330,


Contributor: 
"Krishnakant Mane" <kk@gmail.com>
"Ishan Masdekar " <imasdekar@dff.org.in>
"Navin Karkera" <navin@dff.org.in>
"""


33
from pyramid.view import view_defaults,  view_config
34
35
36
37
38
from gkcore import eng
from gkcore.models import gkdb
from sqlalchemy.sql import select
import json 
from sqlalchemy.engine.base import Connection
39
from sqlalchemy import and_
40
41
42
con = Connection
con = eng.connect()

43
@view_defaults(route_name='organisations')
44
45
46
class api_organisation(object):
	def __init__(self,request):
		self.request = request
47
		
48
49
	@view_config(request_method='GET', renderer ='json')
	def getOrgs(self):
50
		result = con.execute(select([gkdb.organisation.c.orgcode, gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype,gkdb.organisation.c.yearstart,gkdb.organisation.c.yearend]))
51
52
		orgs = []
		for row in result:
53
			orgs.append({"orgcode":row["orgcode"], "orgname":row["orgname"], "orgtype":row["orgtype"]})
54
55
		print orgs
		return orgs
56
57
58
59
60
61
62
63
64
	
	@view_config(route_name='orgyears', request_method='GET', renderer ='json')
	def getYears(self):
		result = con.execute(select([gkdb.organisation.c.yearstart, gkdb.organisation.c.yearend]).where(gkdb.organisation.c.orgname==self.request.matchdict["orgname"] and gkdb.organisation.c.orgtype == self.request.matchdict["orgtype"]))
		years = []
		for row in result:
			years.append({"yearstart":str(row["yearstart"]), "yearend":str(row["yearend"])})
		print years
		return years
65
		
66
67
	@view_config(request_method='POST',renderer='json')
	def postOrg(self):
68
69
70
		
		try:
			dataset = self.request.json_body
71
			print dataset
72
			result = con.execute(gkdb.organisation.insert(),[dataset])
73
74
			print result.rowcount
			return result.rowcount
75
76
77
		except:
			return False
		
78
79
	@view_config(route_name='organisation', request_method='GET',renderer='json')
	def getOrg(self):
80
		result = con.execute(select([gkdb.organisation]).where(gkdb.organisation.c.orgcode==self.request.matchdict["orgcode"]))
81
		row = result.fetchone()
82
		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"]	}
83
		return orgDetails 
84
85
	@view_config(request_method='PUT', renderer='json')
	def putOrg(self):
86
		#auth check
87
88
89
90
91
92
93
		try:
			dataset = self.request.json_body
			result = con.execute(gkdb.organisation.update().where(gkdb.organisation.c.orgcode==dataset["orgcode"]).values(dataset))
			print result.rowcount
			return result.rowcount
		except:
			return False
94
95
	@view_config(request_method='DELETE', renderer='json')
	def deleteOrg(self):
96
		#auth check
97
98
99
100
101
		dataset = self.request.json_body
		result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==dataset["orgcode"]))
		print result.rowcount
		return result.rowcount