api_organisation.py 3.34 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
34
35
36
37
38
from cornice.resource import resource, view 
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
@resource(collection_path='/organisation',path='/organisation/{orgcode}')
44
45
46
class api_organisation(object):
	def __init__(self,request):
		self.request = request
Krishnakant Mane's avatar
Krishnakant Mane committed
47
	@view(renderer='json')
48
	def collection_get(self):
49
		result = con.execute(select([gkdb.organisation.c.orgcode, gkdb.organisation.c.orgname, gkdb.organisation.c.orgtype,gkdb.organisation.c.yearstart,gkdb.organisation.c.yearend]))
50
51
		orgs = []
		for row in result:
Krishnakant Mane's avatar
Krishnakant Mane committed
52
			orgs.append({"orgcode":row["orgcode"], "orgname":row["orgname"], "orgtype":row["orgtype"], "yearstart":str(row["yearstart"]), "yearend":str(row["yearend"])})
53
54
55
56
57
		print orgs
		return orgs
		
	@view(renderer='json', accept='text/json')
	def collection_post(self):
58
59
60
		
		try:
			dataset = self.request.json_body
61
			print dataset
62
			result = con.execute(gkdb.organisation.insert(),[dataset])
63
64
			print result.rowcount
			return result.rowcount
65
66
67
		except:
			return False
		
68
	
69
	def get(self):
70
		result = con.execute(select([gkdb.organisation]).where(gkdb.organisation.c.orgcode==self.request.matchdict["orgcode"]))
71
		row = result.fetchone()
72
		orgDetails={"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"]	}
73
		return orgDetails 
74
75
	
	def collection_put(self):
76
		#auth check
77
78
79
80
81
82
83
		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
84
85

	def collection_delete(self):
86
		#auth check
87
88
89
90
91
		dataset = self.request.json_body
		result = con.execute(gkdb.organisation.delete().where(gkdb.organisation.c.orgcode==dataset["orgcode"]))
		print result.rowcount
		return result.rowcount