Example #1
0
def validate_get_system_events(request_details):

    valid = False

    if 'eventType' in request_details:
        event_type = request_details['eventType']
        if isinstance(event_type, list):
            for e in event_type:
                validate_event_type(e)
        else:
            validate_event_type(event_type)
        valid = True

    if 'entityType' in request_details:
        validate_entity_type(['entityType'])
        if 'entityId' not in request_details:
            raise AgroError(400, "Enitity Ids are required")
        valid = True

    if 'timestamp' in request_details:
        validate_timestamp(request_details['timestamp'])

    if not valid and ['orderId', 'ticketId', 'farmerId'
                      ] not in request_details:
        raise AgroError(400, "Invalid Request")

    return True
Example #2
0
def validate_timestamp(timestamp):
    if not isinstance(timestamp, list):
        timestamp_list = [timestamp]
    else:
        timestamp_list = timestamp

    for t in timestamp_list:
        try:
            datet = datetime.datetime.fromtimestamp(float(t) / 1000.)
            now = datetime.datetime.now()
            if datet > now or datet < reference_ts:
                raise AgroError(400, "Invalid Timestamp")
        except:
            raise AgroError(400, "Invalid Timestamp")
def insert_system_event(event_json):
    """

    :param event_json:
        entityType
        entityId
        eventType
        eventName

        other keys: farmerId, orderId, productUUID, ticketUUID,
            --> for search indexing --> sparse indexes
    :return: True or Error
    """
    logger.info("Insert System Event :: {}".format(locals().copy()))
    cursor = get_system_event_collection_cursor()

    try:
        result = cursor.insert_one(event_json)
    except:
        result = None

    if result:
        return True
    else:
        raise AgroError(422, "Record action for system event failed")
Example #4
0
def validate_system_event_request(request_details):

    if 'event' in request_details.keys():
        validate_system_event(request_details['event'])
    elif 'events' in request_details.keys():
        validate_system_event_list(request_details['events'])
    else:
        raise AgroError(400, "Bad Request. Need event/events in request")
Example #5
0
def validate_system_event_list(event_list):
    if not isinstance(event_list, list):
        raise AgroError(400, "Events is not a list")

    for event in event_list:
        validate_system_event(event)

    return True
def post_system_event(request_details):

    validate_system_event_request(request_details)

    if 'event' in request_details:
        insertion = insert_system_event(request_details['event'])
    else:
        raise AgroError(501, "Not implemented for your request")

    return {}, 201
Example #7
0
    def post(self):
        app.logger.info("POST {}".format(self.__class__.__name__))

        try:
            request_details = request.json
        except:
            raise AgroError(400, "Bad Request")

        response, code = post_system_event(request_details)

        return ok_response(response, code=code)
def get_template(params):
    try:
        template_type = params['type'].lower()
        template_name = params['name'].upper()
        template_language = params.get('language','en')
    except:
        raise AgroError(400, "Bad Request Parameters")

    template_dict = get_template_string(template_type, template_name, template_language)

    if not template_dict:
        raise AgroError(404, "Matching Template Not Found")

    try:
        template_subject = template_dict.get('subject')
        template = template_dict['body']
        fields = template_dict.get('fields', [])
    except:
        raise AgroError(422, "Error in template object")

    return dict(name=template_name, type=template_type, body=template, subject=template_subject,fields=fields)
Example #9
0
def validate_event_name(event_name):
    if event_name not in event_name_set:
        raise AgroError(400, "Event Name is invalid")

    return True
Example #10
0
def validate_event_type(event_type):
    if event_type not in event_type_set:
        raise AgroError(400, "Event type is invalid")

    return True
Example #11
0
def validate_entity_type(entity_type):
    if entity_type not in entity_type_set:
        raise AgroError(400, "Entity type is invalid")

    return True