Skip to content

Instantly share code, notes, and snippets.

@Cirhuzalain
Last active June 11, 2018 12:00
Show Gist options
  • Save Cirhuzalain/c44d2a6ad522e4d84f4fe545bd9e883b to your computer and use it in GitHub Desktop.
Save Cirhuzalain/c44d2a6ad522e4d84f4fe545bd9e883b to your computer and use it in GitHub Desktop.

Revisions

  1. Cirhuzalain revised this gist Mar 7, 2018. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions model_vendor_farmer.py
    Original file line number Diff line number Diff line change
    @@ -78,11 +78,11 @@
    'ar_iwp',
    'ar_ss',
    'harvest_id',
    'farmer_id'
    'vendor_id'
    )]),
    ('vendor_land_ids', [(
    'id',
    'farmer_id',
    'vendor_id',
    'plot_size',
    'lat',
    'lng',
    @@ -101,7 +101,7 @@
    'farmer_expected_min_ppp',
    'minimum_flow_price',
    'harvest_id',
    'farmer_id'
    'vendor_id'
    )]),
    ('finance_data_ids', [(
    'id',
    @@ -116,7 +116,7 @@
    'loan_purpose_o',
    'mobile_money_account',
    'harvest_id',
    'farmer_id'
    'vendor_id'
    )]),
    ('baseline_ids', [(
    'id',
    @@ -128,7 +128,7 @@
    'total_qty_middlemen',
    'price_sold_middlemen',
    'harvest_id',
    'farmer_id'
    'vendor_id'
    )])
    )
    # ]
    @@ -189,11 +189,11 @@
    'ar_iwp',
    'ar_ss',
    'harvest_id',
    'farmer_id'
    'vendor_id'
    )]),
    ('vendor_land_ids', [(
    'id',
    'farmer_id',
    'vendor_id',
    'plot_size',
    'lat',
    'lng',
    @@ -212,7 +212,7 @@
    'farmer_expected_min_ppp',
    'minimum_flow_price',
    'harvest_id',
    'farmer_id'
    'vendor_id'
    )]),
    ('finance_data_ids', [(
    'id',
    @@ -227,7 +227,7 @@
    'loan_purpose_o',
    'mobile_money_account',
    'harvest_id',
    'farmer_id'
    'vendor_id'
    )]),
    ('baseline_ids', [(
    'id',
    @@ -239,7 +239,7 @@
    'total_qty_middlemen',
    'price_sold_middlemen',
    'harvest_id',
    'farmer_id'
    'vendor_id'
    )])
    )

  2. Cirhuzalain created this gist Mar 7, 2018.
    361 changes: 361 additions & 0 deletions model_vendor_farmer.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,361 @@
    # -*- coding: utf-8 -*-
    from main import *

    _logger = logging.getLogger(__name__)


    # List of REST resources in current file:
    # (url prefix) (method) (action)
    # /api/vendor.farmer GET - Read all (with optional filters, offset, limit, order)
    # /api/vendor.farmer/<id> GET - Read one
    # /api/vendor.farmer POST - Create one
    # /api/vendor.farmer/<id> PUT - Update one
    # /api/vendor.farmer/<id> DELETE - Delete one
    # /api/vendor.farmer/<id>/<method> PUT - Call method (with optional parameters)


    # List of IN/OUT data (json data and HTTP-headers) for each REST resource:

    # /api/vendor.farmer GET - Read all (with optional filters, offset, limit, order)
    # IN data:
    # HEADERS:
    # 'access_token'
    # JSON:
    # (optional filters (Odoo domain), offset, limit, order)
    # { # editable
    # "filters": "[('some_field_1', '=', some_value_1), ('some_field_2', '!=', some_value_2), ...]",
    # "offset": XXX,
    # "limit": XXX,
    # "order": "list_of_fields" # default 'name asc'
    # }
    # OUT data:
    OUT__vendor_farmer__read_all__SUCCESS_CODE = 200 # editable
    # JSON:
    # {
    # "count": XXX, # number of returned records
    # "results": [
    OUT__vendor_farmer__read_all__JSON = ( # editable
    'id',
    'name',
    'phone',
    'email',
    'address',
    'gender',
    'head_of_household',
    'num_household_members',
    'spouse_firstname',
    'spouse_lastname',
    'cellphone_alt',
    'cell_carrier',
    'membership_id',
    'ar_tractors',
    'ar_harverster',
    'ar_dryer',
    'ar_thresher',
    'ar_safestorage',
    'ar_other',
    'mws_dam',
    'mws_well',
    'mws_borehole',
    'mws_rs',
    'mws_pb',
    'mws_irrigation',
    'mws_none',
    'mws_other',
    'user_id',
    'storage_details',
    'other_details',
    'total_land_plot_size',
    'categ_total_land_plot_size',
    ('access_info_ids', [(
    'id',
    'ar_aes',
    'ar_cri',
    'ar_seeds',
    'ar_of',
    'ar_if',
    'ar_labour',
    'ar_iwp',
    'ar_ss',
    'harvest_id',
    'farmer_id'
    )]),
    ('vendor_land_ids', [(
    'id',
    'farmer_id',
    'plot_size',
    'lat',
    'lng',
    'harvest_id'
    )]),
    ('forecast_vendor_ids', [(
    'id',
    'total_arable_land_plots',
    'expected_production_in_mt',
    'forecasted_yield_mt',
    'forecasted_harvest_sale_value',
    'total_coop_land_size',
    'farmer_percentage_land',
    'current_ppp_commitment',
    'farmer_contribution_ppp',
    'farmer_expected_min_ppp',
    'minimum_flow_price',
    'harvest_id',
    'farmer_id'
    )]),
    ('finance_data_ids', [(
    'id',
    'outstanding_loan',
    'total_loan_amount',
    'total_outstanding',
    'interest_rate',
    'duration',
    'loan_provider',
    'loan_purpose_i',
    'loan_purpose_a',
    'loan_purpose_o',
    'mobile_money_account',
    'harvest_id',
    'farmer_id'
    )]),
    ('baseline_ids', [(
    'id',
    'seasona_harvest',
    'lost_harvest_total',
    'sold_harvest_total',
    'total_qty_coops',
    'price_sold_coops',
    'total_qty_middlemen',
    'price_sold_middlemen',
    'harvest_id',
    'farmer_id'
    )])
    )
    # ]
    # }

    # /api/vendor.farmer/<id> GET - Read one
    # IN data:
    # HEADERS:
    # 'access_token'
    # JSON:
    # (optional parameter 'search_field' for search object not by 'id' field)
    # {"search_field": "some_field_name"} # editable
    # OUT data:
    OUT__vendor_farmer__read_one__SUCCESS_CODE = 200 # editable
    OUT__vendor_farmer__read_one__JSON = ( # editable
    # (The order of fields of different types maybe arbitrary)
    # simple fields (non relational):
    'id',
    'name',
    'phone',
    'email',
    'address',
    'gender',
    'head_of_household',
    'num_household_members',
    'spouse_firstname',
    'spouse_lastname',
    'cellphone_alt',
    'cell_carrier',
    'membership_id',
    'ar_tractors',
    'ar_harverster',
    'ar_dryer',
    'ar_thresher',
    'ar_safestorage',
    'ar_other',
    'mws_dam',
    'mws_well',
    'mws_borehole',
    'mws_rs',
    'mws_pb',
    'mws_irrigation',
    'mws_none',
    'mws_other',
    'user_id',
    'total_land_plot_size',
    'storage_details',
    'other_details',
    'categ_total_land_plot_size',
    ('access_info_ids', [(
    'id',
    'ar_aes',
    'ar_cri',
    'ar_seeds',
    'ar_of',
    'ar_if',
    'ar_labour',
    'ar_iwp',
    'ar_ss',
    'harvest_id',
    'farmer_id'
    )]),
    ('vendor_land_ids', [(
    'id',
    'farmer_id',
    'plot_size',
    'lat',
    'lng',
    'harvest_id'
    )]),
    ('forecast_vendor_ids', [(
    'id',
    'total_arable_land_plots',
    'expected_production_in_mt',
    'forecasted_yield_mt',
    'forecasted_harvest_sale_value',
    'total_coop_land_size',
    'farmer_percentage_land',
    'current_ppp_commitment',
    'farmer_contribution_ppp',
    'farmer_expected_min_ppp',
    'minimum_flow_price',
    'harvest_id',
    'farmer_id'
    )]),
    ('finance_data_ids', [(
    'id',
    'outstanding_loan',
    'total_loan_amount',
    'total_outstanding',
    'interest_rate',
    'duration',
    'loan_provider',
    'loan_purpose_i',
    'loan_purpose_a',
    'loan_purpose_o',
    'mobile_money_account',
    'harvest_id',
    'farmer_id'
    )]),
    ('baseline_ids', [(
    'id',
    'seasona_harvest',
    'lost_harvest_total',
    'sold_harvest_total',
    'total_qty_coops',
    'price_sold_coops',
    'total_qty_middlemen',
    'price_sold_middlemen',
    'harvest_id',
    'farmer_id'
    )])
    )

    # /api/vendor.farmer POST - Create one
    # IN data:
    # HEADERS:
    # 'access_token'
    # DEFAULTS:
    # (optional default values of fields)
    DEFAULTS__vendor_farmer__create_one__JSON = { # editable
    #"some_field_1": some_value_1,
    #"some_field_2": some_value_2,
    #...
    }
    # JSON:
    # (fields and its values of created object;
    # don't forget about model's mandatory fields!)
    # ... # editable
    # OUT data:
    OUT__vendor_farmer__create_one__SUCCESS_CODE = 200 # editable
    OUT__vendor_farmer__create_one__JSON = ( # editable
    'id',
    )

    # /api/vendor.farmer/<id> PUT - Update one
    # IN data:
    # HEADERS:
    # 'access_token'
    # JSON:
    # (fields and new values of updated object) # editable
    # ...
    # OUT data:
    OUT__vendor_farmer__update_one__SUCCESS_CODE = 200 # editable

    # /api/vendor.farmer/<id> DELETE - Delete one
    # IN data:
    # HEADERS:
    # 'access_token'
    # OUT data:
    OUT__vendor_farmer__delete_one__SUCCESS_CODE = 200 # editable

    # /api/vendor.farmer/<id>/<method> PUT - Call method (with optional parameters)
    # IN data:
    # HEADERS:
    # 'access_token'
    # JSON:
    # (named parameters of method) # editable
    # ...
    # OUT data:
    OUT__vendor_farmer__call_method__SUCCESS_CODE = 200 # editable


    # HTTP controller of REST resources:

    class ControllerREST(http.Controller):

    # Read all (with optional filters, offset, limit, order):
    @http.route('/api/vendor.farmer', methods=['GET'], type='http', auth='none')
    @check_permissions
    def api__vendor_farmer__GET(self):
    return wrap__resource__read_all(
    modelname='vendor.farmer',
    default_domain=[],
    success_code=OUT__vendor_farmer__read_all__SUCCESS_CODE,
    OUT_fields=OUT__vendor_farmer__read_all__JSON
    )

    # Read one:
    @http.route('/api/vendor.farmer/<id>', methods=['GET'], type='http', auth='none')
    @check_permissions
    def api__vendor_farmer__id_GET(self, id):
    return wrap__resource__read_one(
    modelname='vendor.farmer',
    id=id,
    success_code=OUT__vendor_farmer__read_one__SUCCESS_CODE,
    OUT_fields=OUT__vendor_farmer__read_one__JSON
    )

    # Create one:
    @http.route('/api/vendor.farmer', methods=['POST'], type='http', auth='none', csrf=False)
    @check_permissions
    def api__vendor_farmer__POST(self):
    return wrap__resource__create_one(
    modelname='vendor.farmer',
    default_vals=DEFAULTS__vendor_farmer__create_one__JSON,
    success_code=OUT__vendor_farmer__create_one__SUCCESS_CODE,
    OUT_fields=OUT__vendor_farmer__create_one__JSON
    )

    # Update one:
    @http.route('/api/vendor.farmer/<id>', methods=['PUT'], type='http', auth='none', csrf=False)
    @check_permissions
    def api__vendor_farmer__id_PUT(self, id):
    return wrap__resource__update_one(
    modelname='vendor.farmer',
    id=id,
    success_code=OUT__vendor_farmer__update_one__SUCCESS_CODE
    )

    # Delete one:
    @http.route('/api/vendor.farmer/<id>', methods=['DELETE'], type='http', auth='none', csrf=False)
    @check_permissions
    def api__vendor_farmer__id_DELETE(self, id):
    return wrap__resource__delete_one(
    modelname='vendor.farmer',
    id=id,
    success_code=OUT__vendor_farmer__delete_one__SUCCESS_CODE
    )

    # Call method (with optional parameters):
    @http.route('/api/vendor.farmer/<id>/<method>', methods=['PUT'], type='http', auth='none', csrf=False)
    @check_permissions
    def api__vendor_farmer__id__method_PUT(self, id, method):
    return wrap__resource__call_method(
    modelname='vendor.farmer',
    id=id,
    method=method,
    success_code=OUT__vendor_farmer__call_method__SUCCESS_CODE
    )