neofarm half done NEXT: finish and extract orm into separate files
This commit is contained in:
161
neofarm/orm.py
Normal file
161
neofarm/orm.py
Normal file
@@ -0,0 +1,161 @@
|
||||
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')
|
||||
Reference in New Issue
Block a user