entire v3 datamodel orm NEXT: test orm
This commit is contained in:
@@ -1,7 +1,42 @@
|
|||||||
import orm
|
import requests
|
||||||
|
from oauthlib.oauth2 import BackendApplicationClient
|
||||||
|
from requests_oauthlib import OAuth2Session, OAuth2
|
||||||
|
import jsonapi_requests
|
||||||
|
from orm.log import *
|
||||||
|
from orm.asset import *
|
||||||
|
from orm.quantity import *
|
||||||
|
from orm.taxonomy import *
|
||||||
|
|
||||||
def getHarvests():
|
# constants:
|
||||||
# result = orm.Harvest.from_id("750511d3-5f7b-4e70-b541-d2f70696734f")
|
root_url = 'https://farmos.wagframe.duckdns.org/'
|
||||||
# print(result.name)
|
api_root = root_url + 'api/'
|
||||||
result = orm.Harvest.get_list()
|
token_url = root_url + 'oauth/token/'
|
||||||
return result
|
client_id = 'neofarm'
|
||||||
|
client_secret = 'Wk&%g#d8lYFUDrU7k7k%5ZS$V@hHauBq'
|
||||||
|
|
||||||
|
api_path_separator = '/'
|
||||||
|
api_type_spatrator = '--'
|
||||||
|
|
||||||
|
def init_api():
|
||||||
|
client = BackendApplicationClient(client_id=client_id)
|
||||||
|
oauth_session = OAuth2Session(client=client)
|
||||||
|
token = oauth_session.fetch_token(
|
||||||
|
token_url = token_url,
|
||||||
|
client_id = client_id,
|
||||||
|
client_secret = client_secret
|
||||||
|
)
|
||||||
|
oauth = OAuth2(client_id=client_id, client=client, token=token)
|
||||||
|
|
||||||
|
api = jsonapi_requests.orm.OrmApi.config({
|
||||||
|
'API_ROOT': api_root,
|
||||||
|
'AUTH': oauth,
|
||||||
|
'TIMEOUT': 1,
|
||||||
|
})
|
||||||
|
jsonapi_requests.configuration.default = api
|
||||||
|
return api
|
||||||
|
|
||||||
|
api = init_api()
|
||||||
|
Log.Meta.set_api(api)
|
||||||
|
Asset.Meta.set_api(api)
|
||||||
|
Quantity.Meta.set_api(api)
|
||||||
|
Taxonomy.Meta.set_api(api)
|
||||||
161
neofarm/orm.py
161
neofarm/orm.py
@@ -1,161 +0,0 @@
|
|||||||
import requests
|
|
||||||
from oauthlib.oauth2 import BackendApplicationClient
|
|
||||||
from requests_oauthlib import OAuth2Session, OAuth2
|
|
||||||
import jsonapi_requests
|
|
||||||
import lib
|
|
||||||
|
|
||||||
# constants:
|
|
||||||
root_url = 'https://farmos.wagframe.duckdns.org/'
|
|
||||||
api_root = root_url + 'api/'
|
|
||||||
token_url = root_url + 'oauth/token/'
|
|
||||||
client_id = 'neofarm'
|
|
||||||
client_secret = 'Wk&%g#d8lYFUDrU7k7k%5ZS$V@hHauBq'
|
|
||||||
|
|
||||||
api_path_separator = '/'
|
|
||||||
api_type_spatrator = '--'
|
|
||||||
|
|
||||||
def init_api():
|
|
||||||
client = BackendApplicationClient(client_id=client_id)
|
|
||||||
oauth_session = OAuth2Session(client=client)
|
|
||||||
token = oauth_session.fetch_token(
|
|
||||||
token_url = token_url,
|
|
||||||
client_id = client_id,
|
|
||||||
client_secret = client_secret
|
|
||||||
)
|
|
||||||
oauth = OAuth2(client_id=client_id, client=client, token=token)
|
|
||||||
|
|
||||||
api = jsonapi_requests.orm.OrmApi.config({
|
|
||||||
'API_ROOT': api_root,
|
|
||||||
'AUTH': oauth,
|
|
||||||
'TIMEOUT': 1,
|
|
||||||
})
|
|
||||||
jsonapi_requests.configuration.default = api
|
|
||||||
return api
|
|
||||||
|
|
||||||
api = init_api()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Logs
|
|
||||||
# Assets
|
|
||||||
# Quantities
|
|
||||||
# Taxonomy Terms
|
|
||||||
|
|
||||||
class Log(jsonapi_requests.orm.ApiModel):
|
|
||||||
class Meta:
|
|
||||||
type = 'log'
|
|
||||||
api = api
|
|
||||||
@classmethod
|
|
||||||
def get_type_and_path(cls, name):
|
|
||||||
return (f"{cls.type}{api_type_spatrator}{name}", f"{cls.path}{api_path_separator}{name}")
|
|
||||||
|
|
||||||
name = jsonapi_requests.orm.AttributeField('name')
|
|
||||||
timestamp = jsonapi_requests.orm.AttributeField('timestamp')
|
|
||||||
isMovement = jsonapi_requests.orm.AttributeField('isMovement')
|
|
||||||
assets = jsonapi_requests.orm.RelationField('asset')
|
|
||||||
location = jsonapi_requests.orm.RelationField('location')
|
|
||||||
equipment = jsonapi_requests.orm.RelationField('equipment')
|
|
||||||
quantities = jsonapi_requests.orm.RelationField('quantity')
|
|
||||||
|
|
||||||
def getPlant(self):
|
|
||||||
for asset in self.assets:
|
|
||||||
if asset.type == 'asset--plant':
|
|
||||||
return asset
|
|
||||||
|
|
||||||
class Activity(Log):
|
|
||||||
class Meta(Log.Meta):
|
|
||||||
name = 'activity'
|
|
||||||
type, path = Log.Meta.get_type_and_path(name)
|
|
||||||
class Maintenance(Log):
|
|
||||||
class Meta(Log.Meta):
|
|
||||||
name = 'maintenance'
|
|
||||||
type, path = Log.Meta.get_type_and_path(name)
|
|
||||||
class Purchase(Log):
|
|
||||||
class Meta(Log.Meta):
|
|
||||||
name = 'purchase'
|
|
||||||
type, path = Log.Meta.get_type_and_path(name)
|
|
||||||
class Seeding(Log):
|
|
||||||
class Meta(Log.Meta):
|
|
||||||
name = 'seeding'
|
|
||||||
type, path = Log.Meta.get_type_and_path(name)
|
|
||||||
class Input(Log):
|
|
||||||
class Meta(Log.Meta):
|
|
||||||
name = 'input'
|
|
||||||
type, path = Log.Meta.get_type_and_path(name)
|
|
||||||
class Medical(Log):
|
|
||||||
class Meta(Log.Meta):
|
|
||||||
name = 'medical'
|
|
||||||
type, path = Log.Meta.get_type_and_path(name)
|
|
||||||
class Harvest(Log):
|
|
||||||
class Meta(Log.Meta):
|
|
||||||
name = 'harvest'
|
|
||||||
type, path = Log.Meta.get_type_and_path(name)
|
|
||||||
class Sale(Log):
|
|
||||||
class Meta(Log.Meta):
|
|
||||||
name = 'sale'
|
|
||||||
type, path = Log.Meta.get_type_and_path(name)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Asset(jsonapi_requests.orm.ApiModel):
|
|
||||||
class Meta:
|
|
||||||
type = 'asset'
|
|
||||||
api = api
|
|
||||||
@classmethod
|
|
||||||
def get_type_and_path(cls, name):
|
|
||||||
return (f"{cls.type}{api_type_spatrator}{name}", f"{cls.path}{api_path_separator}{name}")
|
|
||||||
|
|
||||||
name = jsonapi_requests.orm.AttributeField('name')
|
|
||||||
timestamp = jsonapi_requests.orm.AttributeField('timestamp')
|
|
||||||
|
|
||||||
class Land(Asset):
|
|
||||||
class Meta(Asset.Meta):
|
|
||||||
name = 'land'
|
|
||||||
type, path = Asset.Meta.get_type_and_path(name)
|
|
||||||
geometry = jsonapi_requests.orm.AttributeField('intrinsic_geometry')
|
|
||||||
class Plant(Asset):
|
|
||||||
class Meta(Asset.Meta):
|
|
||||||
name = 'plant'
|
|
||||||
type, path = Asset.Meta.get_type_and_path(name)
|
|
||||||
crop = jsonapi_requests.orm.RelationField('plant_type')
|
|
||||||
season = jsonapi_requests.orm.RelationField('season')
|
|
||||||
class Equipment(Asset):
|
|
||||||
class Meta(Asset.Meta):
|
|
||||||
name = 'equipment'
|
|
||||||
type, path = Asset.Meta.get_type_and_path(name)
|
|
||||||
class Material(Asset):
|
|
||||||
class Meta(Asset.Meta):
|
|
||||||
name = 'material'
|
|
||||||
type, path = Asset.Meta.get_type_and_path(name)
|
|
||||||
crop = jsonapi_requests.orm.RelationField('material_type')
|
|
||||||
class Seed(Asset):
|
|
||||||
class Meta(Asset.Meta):
|
|
||||||
name = 'seed'
|
|
||||||
type, path = Asset.Meta.get_type_and_path(name)
|
|
||||||
crop = jsonapi_requests.orm.RelationField('plant_type')
|
|
||||||
season = jsonapi_requests.orm.RelationField('season')
|
|
||||||
class Product(Asset):
|
|
||||||
class Meta(Asset.Meta):
|
|
||||||
name = 'product'
|
|
||||||
type, path = Asset.Meta.get_type_and_path(name)
|
|
||||||
type = jsonapi_requests.orm.RelationField('product_type')
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Quantity(jsonapi_requests.orm.ApiModel):
|
|
||||||
class Meta:
|
|
||||||
type = 'quantity'
|
|
||||||
api = api
|
|
||||||
@classmethod
|
|
||||||
def get_type_and_path(cls, name):
|
|
||||||
return (f"{cls.type}{api_type_spatrator}{name}", f"{cls.path}{api_path_separator}{name}")
|
|
||||||
|
|
||||||
measure = jsonapi_requests.orm.AttributeField('measure')
|
|
||||||
class Standard(Quantity):
|
|
||||||
class Meta(Asset.Meta):
|
|
||||||
name = 'standard'
|
|
||||||
type, path = Asset.Meta.get_type_and_path(name)
|
|
||||||
type = jsonapi_requests.orm.RelationField('product_type')
|
|
||||||
45
neofarm/orm/asset.py
Normal file
45
neofarm/orm/asset.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import jsonapi_requests
|
||||||
|
|
||||||
|
class Asset(jsonapi_requests.orm.ApiModel):
|
||||||
|
class Meta:
|
||||||
|
type = 'asset'
|
||||||
|
api = api
|
||||||
|
@classmethod
|
||||||
|
def get_type_and_path(cls, name):
|
||||||
|
return (f"{cls.type}{api_type_spatrator}{name}", f"{cls.path}{api_path_separator}{name}")
|
||||||
|
|
||||||
|
name = jsonapi_requests.orm.AttributeField('name')
|
||||||
|
timestamp = jsonapi_requests.orm.AttributeField('timestamp')
|
||||||
|
|
||||||
|
class Land(Asset):
|
||||||
|
class Meta(Asset.Meta):
|
||||||
|
name = 'land'
|
||||||
|
type, path = Asset.Meta.get_type_and_path(name)
|
||||||
|
geometry = jsonapi_requests.orm.AttributeField('intrinsic_geometry')
|
||||||
|
class Plant(Asset):
|
||||||
|
class Meta(Asset.Meta):
|
||||||
|
name = 'plant'
|
||||||
|
type, path = Asset.Meta.get_type_and_path(name)
|
||||||
|
crop = jsonapi_requests.orm.RelationField('plant_type')
|
||||||
|
season = jsonapi_requests.orm.RelationField('season')
|
||||||
|
class Equipment(Asset):
|
||||||
|
class Meta(Asset.Meta):
|
||||||
|
name = 'equipment'
|
||||||
|
type, path = Asset.Meta.get_type_and_path(name)
|
||||||
|
class Material(Asset):
|
||||||
|
class Meta(Asset.Meta):
|
||||||
|
name = 'material'
|
||||||
|
type, path = Asset.Meta.get_type_and_path(name)
|
||||||
|
crop = jsonapi_requests.orm.RelationField('material_type')
|
||||||
|
class Seed(Asset):
|
||||||
|
class Meta(Asset.Meta):
|
||||||
|
name = 'seed'
|
||||||
|
type, path = Asset.Meta.get_type_and_path(name)
|
||||||
|
crop = jsonapi_requests.orm.RelationField('plant_type')
|
||||||
|
season = jsonapi_requests.orm.RelationField('season')
|
||||||
|
class Product(Asset):
|
||||||
|
class Meta(Asset.Meta):
|
||||||
|
name = 'product'
|
||||||
|
type, path = Asset.Meta.get_type_and_path(name)
|
||||||
|
type = jsonapi_requests.orm.RelationField('product_type')
|
||||||
|
|
||||||
58
neofarm/orm/log.py
Normal file
58
neofarm/orm/log.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import jsonapi_requests
|
||||||
|
|
||||||
|
class Log(jsonapi_requests.orm.ApiModel):
|
||||||
|
class Meta:
|
||||||
|
type = 'log'
|
||||||
|
@classmethod
|
||||||
|
def get_type_and_path(cls, name):
|
||||||
|
return (f"{cls.type}{api_type_spatrator}{name}", f"{cls.path}{api_path_separator}{name}")
|
||||||
|
@classmethod
|
||||||
|
def set_api(cls, api):
|
||||||
|
cls.api = api
|
||||||
|
|
||||||
|
name = jsonapi_requests.orm.AttributeField('name')
|
||||||
|
timestamp = jsonapi_requests.orm.AttributeField('timestamp')
|
||||||
|
isMovement = jsonapi_requests.orm.AttributeField('isMovement')
|
||||||
|
assets = jsonapi_requests.orm.RelationField('asset')
|
||||||
|
location = jsonapi_requests.orm.RelationField('location')
|
||||||
|
equipment = jsonapi_requests.orm.RelationField('equipment')
|
||||||
|
quantities = jsonapi_requests.orm.RelationField('quantity')
|
||||||
|
|
||||||
|
def getPlant(self):
|
||||||
|
for asset in self.assets:
|
||||||
|
if asset.type == 'asset--plant':
|
||||||
|
return asset
|
||||||
|
|
||||||
|
class Activity(Log):
|
||||||
|
class Meta(Log.Meta):
|
||||||
|
name = 'activity'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class Maintenance(Log):
|
||||||
|
class Meta(Log.Meta):
|
||||||
|
name = 'maintenance'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class Purchase(Log):
|
||||||
|
class Meta(Log.Meta):
|
||||||
|
name = 'purchase'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class Seeding(Log):
|
||||||
|
class Meta(Log.Meta):
|
||||||
|
name = 'seeding'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class Input(Log):
|
||||||
|
class Meta(Log.Meta):
|
||||||
|
name = 'input'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class Medical(Log):
|
||||||
|
class Meta(Log.Meta):
|
||||||
|
name = 'medical'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class Harvest(Log):
|
||||||
|
class Meta(Log.Meta):
|
||||||
|
name = 'harvest'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class Sale(Log):
|
||||||
|
class Meta(Log.Meta):
|
||||||
|
name = 'sale'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
|
||||||
31
neofarm/orm/quantity.py
Normal file
31
neofarm/orm/quantity.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import jsonapi_requests
|
||||||
|
|
||||||
|
class Quantity(jsonapi_requests.orm.ApiModel):
|
||||||
|
class Meta:
|
||||||
|
type = 'quantity'
|
||||||
|
api = api
|
||||||
|
@classmethod
|
||||||
|
def get_type_and_path(cls, name):
|
||||||
|
return (f"{cls.type}{api_type_spatrator}{name}", f"{cls.path}{api_path_separator}{name}")
|
||||||
|
|
||||||
|
measure = jsonapi_requests.orm.AttributeField('measure')
|
||||||
|
measure = jsonapi_requests.orm.AttributeField('value.decimal')
|
||||||
|
units = jsonapi_requests.orm.RelationFieldField('units')
|
||||||
|
inventory_adjustment = jsonapi_requests.orm.AttributeField('inventory_adjustment')
|
||||||
|
inventory_asset = jsonapi_requests.orm.RelationFieldField('inventory_asset')
|
||||||
|
|
||||||
|
class Standard(Quantity):
|
||||||
|
class Meta(Quantity.Meta):
|
||||||
|
name = 'standard'
|
||||||
|
type, path = Asset.Meta.get_type_and_path(name)
|
||||||
|
class Price(Quantity):
|
||||||
|
class Meta(Quantity.Meta):
|
||||||
|
name = 'price'
|
||||||
|
type, path = Asset.Meta.get_type_and_path(name)
|
||||||
|
unit_price = jsonapi_requests.orm.AttributeField('unit_price.decimal')
|
||||||
|
total_price = jsonapi_requests.orm.AttributeField('total_price.decimal')
|
||||||
|
class Material(Quantity):
|
||||||
|
class Meta(Quantity.Meta):
|
||||||
|
name = 'material'
|
||||||
|
type, path = Asset.Meta.get_type_and_path(name)
|
||||||
|
unit_price = jsonapi_requests.orm.RelationField('material_type')
|
||||||
38
neofarm/orm/taxonomy.py
Normal file
38
neofarm/orm/taxonomy.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import jsonapi_requests
|
||||||
|
|
||||||
|
class Taxonomy(jsonapi_requests.orm.ApiModel):
|
||||||
|
class Meta:
|
||||||
|
type = 'taxonomy_term'
|
||||||
|
@classmethod
|
||||||
|
def get_type_and_path(cls, name):
|
||||||
|
return (f"{cls.type}{api_type_spatrator}{name}", f"{cls.path}{api_path_separator}{name}")
|
||||||
|
@classmethod
|
||||||
|
def set_api(cls, api):
|
||||||
|
cls.api = api
|
||||||
|
|
||||||
|
name = jsonapi_requests.orm.AttributeField('name')
|
||||||
|
|
||||||
|
class Crop(Taxonomy):
|
||||||
|
class Meta(Taxonomy.Meta):
|
||||||
|
name = 'crop_family'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class LogCategory(Taxonomy):
|
||||||
|
class Meta(Taxonomy.Meta):
|
||||||
|
name = 'log_category'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class MaterialType(Taxonomy):
|
||||||
|
class Meta(Taxonomy.Meta):
|
||||||
|
name = 'material_type'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class PlantType(Taxonomy):
|
||||||
|
class Meta(Taxonomy.Meta):
|
||||||
|
name = 'plant_type'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class ProductType(Taxonomy):
|
||||||
|
class Meta(Taxonomy.Meta):
|
||||||
|
name = 'product_type'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
|
class Season(Taxonomy):
|
||||||
|
class Meta(Taxonomy.Meta):
|
||||||
|
name = 'season'
|
||||||
|
type, path = Log.Meta.get_type_and_path(name)
|
||||||
Reference in New Issue
Block a user