Пример #1
0
def create_company():
    try:
        data = request.json
        data['user_id'] = current_user.id
        company = CurriculumCompany(**data)
        db.session.add(company)
        db.session.commit()
        resp = Response(json.dumps({'id': company.id}),
                        status=CREATED,
           content_type=JSON_HEADER)
        watchdog(request.url, 'Create company', json.dumps(data))
    except Exception as e:
        db.session.rollback()
        combined = json.dumps(data) + " " + e.message
        watchdog(request.url, 'Create company', combined)
        resp = Response(json.dumps({'error': e.message}),
                            status=ERROR,
               content_type=JSON_HEADER)
    finally:
        return resp
Пример #2
0
def update_company(company_id):

    permission = EditCurriculumCompanyPermission(company_id)

    if permission.can():
        try:
            data = request.json
            company = CurriculumCompany.query.get(company_id)
            if 'user_id' in data.keys():
                data.pop('user_id')
            company.populate(**data)
            db.session.add(company)
            db.session.commit()
            watchdog(request.url, 'Update company', json.dumps(request.json))
            resp = Response(status=OK, content_type=JSON_HEADER)
        except Exception as e:
            db.session.rollback()
            resp = Response(json.dumps({'msg': e.message}),
                                status=ERROR,
                   content_type=JSON_HEADER)
            combined = json.dumps(request.json) + " " + e.message
            watchdog(request.url, 'Update company', combined)
        finally:
            return resp
    else:
        resp = Response(json.dumps({'msg': 'That is not yours'}),
                                status=ERROR,
                   content_type=JSON_HEADER)
        combined = json.dumps(request.json) + " by user %d" % current_user.id
        watchdog(request.url, 'Update company fail', combined)
        return resp
Пример #3
0
def delete_company(company_id):
    permission = EditCurriculumCompanyPermission(company_id)

    if permission.can():
        try:
            company = CurriculumCompany.query.get(company_id)
            db.session.delete(company)
            db.session.commit()
            resp = Response(status=OK, content_type=JSON_HEADER)
        except Exception as e:
            db.session.rollback()
            resp = Response(json.dumps({'msg': e.message}),
                                status=ERROR,
                   content_type=JSON_HEADER)
            watchdog(request.url, 'Delete company fail', e.message)
        finally:
            return resp
    else:
        resp = Response(json.dumps({'msg': 'That is not yours'}),
                                status=ERROR,
                   content_type=JSON_HEADER)
        combined = "Error by user %d" % current_user.id 
        watchdog(request.url, 'Delete company fail', combined)
        return resp