Exemplo n.º 1
0
def restaurant(restaurant_id=None):
    app.logger.debug('Args: {}'.format(request.args))
    restaurant_name = to_string(request.args.get('name', ''))
    category = to_string(request.args.get('category', ''))
    if request.method == 'POST':
        if not restaurant_name or not category:
            return json_response(
                {'response': 'Either name or category is missing in params'},
                400)
        result = Restaurant.create(restaurant_name, category)
        return json_response({'response': 'Created', 'id': result.id})
    elif request.method == 'PUT':
        # restaurant_id = int(request.args.get('id', ''))
        app.logger.debug('Restaurant_id: {}'.format(restaurant_id))
        if not restaurant_name and not category:
            json_response(
                {'response': 'Either name or category is missing in params'},
                400)
        result = Restaurant.update(restaurant_id, restaurant_name, category)
        app.logger.debug('result: {}'.format(result))
        return json_response({'response': 'Updated', 'id': result.id})
    else:
        return json_response({'response': 'BAD Request'}, 400)
Exemplo n.º 2
0
def process_file(filehandler, delimiter=','):
    """Process CSV file to insert data into DB"""

    reader = csv.reader(filehandler)
    headers = next(reader)
    headers = tuple(header.lower().replace(' ', '_') for header in headers)
    for num, line in enumerate(reader):
        record = {header: value for header, value in zip(headers, line)}
        # Cast record to correct type
        record['camis'] = int(record['camis'])
        try:
            record['score'] = int(record['score'])
        except ValueError:
            record['score'] = None
        try:
            record['inspection_date'] = datetime.datetime.strptime(
                record['inspection_date'], '%m/%d/%Y')
        except ValueError:
            record['inspection_date'] = None
        try:
            record['grade_date'] = datetime.datetime.strptime(
                record['grade_date'], '%m/%d/%Y')
        except ValueError:
            record['grade_date'] = None
        try:
            record['record_date'] = datetime.datetime.strptime(
                record['record_date'], '%m/%d/%Y')
        except ValueError:
            record['record_date'] = None
        record['cuisine_description'] = [
            x.strip().lower() for x in record['cuisine_description'].split(',')
        ]

        if record['violation_code']:
            violation = Violation.objects(
                code=record['violation_code']).first()
            if violation is None:
                violation = Violation(
                    code=record['violation_code'],
                    description=record['violation_description'],
                    critical_flag=record['critical_flag'])
                violation.save()

        inspection = Inspection.objects(
            restaurnt_id=record['camis'],
            inspection_date=record['inspection_date']).first()
        if inspection is None:
            inspection = Inspection(restaurnt_id=record['camis'],
                                    inspection_date=record['inspection_date'],
                                    action=record['action'],
                                    score=record['score'],
                                    grade=record['grade'],
                                    grade_date=record['grade_date'],
                                    record_date=record['record_date'],
                                    type=record['inspection_type'])
            inspection.save()
        inspection.update(add_to_set__violations=[violation])

        restaurant = Restaurant.objects(restaurnt_id=record['camis']).first()
        if restaurant is None:
            restaurant = Restaurant(restaurnt_id=record['camis'],
                                    name=record['dba'],
                                    boro=record['boro'],
                                    building=record['building'],
                                    street=record['street'],
                                    zipcode=record['zipcode'],
                                    phone=record['phone'],
                                    cuisine=record['cuisine_description'],
                                    grade=record['grade'],
                                    grade_date=record['grade_date'],
                                    record_date=record['record_date'])
            restaurant.save()

        elif (restaurant.grade_date is None or record['grade_date']
              and record['grade_date'] > restaurant.grade_date):
            restaurant.grade = record['grade']
            restaurant.grade_date = record['grade_date']
            restaurant.save()

        restaurant.update(add_to_set__inspections=[inspection])