def import_tags(): if 'file' not in request.files: return bad_request('must provide a file') import_csv = request.files['file'] if import_csv.filename == '': return bad_request('must provide a file') if import_csv and import_csv.filename.rsplit('.', 1)[1].lower() == 'csv': import_filename = secure_filename(import_csv.filename) import_csv.save(os.path.join(Config.UPLOAD_FOLDER, import_filename)) with open(os.path.join(Config.UPLOAD_FOLDER, import_filename), 'r') as open_csv: file_read = csv.DictReader(open_csv, delimiter=',') items_imported = 0 for row in file_read: tag = Tag(name=row['tag']) if not TagCategory.query.filter_by( name=row['tag_category']).first(): tag_category = TagCategory(name=row['tag_category']) db.session.add(tag_category) db.session.commit() if Tag.query.filter_by( name=row['tag'], tag_category_id=TagCategory.query.filter_by( name=row['tag_category']).first().id).first(): continue tag.tag_category_id = TagCategory.query.filter_by( name=row['tag_category']).first().id db.session.add(tag) db.session.commit() items_imported += 1 open_csv.close() os.remove(os.path.join(Config.UPLOAD_FOLDER, import_filename)) return str(items_imported) + ' tags imported'
def import_opportunity_tags(): if 'file' not in request.files: return bad_request('must provide a file') import_csv = request.files['file'] if import_csv.filename == '': return bad_request('must provide a file') if import_csv and import_csv.filename.rsplit('.', 1)[1].lower() == 'csv': import_filename = secure_filename(import_csv.filename) import_csv.save(os.path.join(Config.UPLOAD_FOLDER, import_filename)) with open(os.path.join(Config.UPLOAD_FOLDER, import_filename), 'r') as open_csv: file_read = csv.DictReader(open_csv, delimiter=',') items_imported = 0 for row in file_read: if not Opportunity.query.filter_by( name=row['opportunity']).first() or not \ row['tag'] or not row['tag_category']: continue tag_category_id = TagCategory.query.filter_by( name=row['tag_category']).first().id opportunity = Opportunity.query.filter_by( name=row['opportunity']).first() opportunity.tags.append( Tag.query.filter_by(tag_category_id=tag_category_id, name=row['tag']).first()) db.session.add(opportunity) db.session.commit() index_one_record(opportunity) items_imported += 1 open_csv.close() os.remove(os.path.join(Config.UPLOAD_FOLDER, import_filename)) return str(items_imported) + ' opportunity tags imported'
def update_partner_api(id): partner = Partner.query.get_or_404(id) data = request.get_json() or {} if data['name'] == "": return bad_request('must include a non-empty partner name') if 'name' in data and data['name'] != partner.name and \ Partner.query.filter_by(name=data['name']).first(): return bad_request('please use a different partner name') partner.from_dict(data, new_partner=False) db.session.commit() index_one_record(partner) return jsonify(partner.to_dict())
def update_user_api(id): user = User.query.get_or_404(id) data = request.get_json() or {} if 'username' in data and data['username'] != user.username and \ User.query.filter_by(username=data['username']).first(): return bad_request('please use a different username') if 'roles' in data: for role in data['roles']: if not Role.query.filter_by(name=role).first(): return bad_request('that is not an existing role') user.from_dict(data) db.session.add(user) db.session.commit() return jsonify(user.to_dict())
def import_opportunities(): if 'file' not in request.files: return bad_request('must provide a file') import_csv = request.files['file'] if import_csv.filename == '': return bad_request('must provide a file') if import_csv and import_csv.filename.rsplit('.', 1)[1].lower() == 'csv': import_filename = secure_filename(import_csv.filename) import_csv.save(os.path.join(Config.UPLOAD_FOLDER, import_filename)) with open(os.path.join(Config.UPLOAD_FOLDER, import_filename), 'r') as open_csv: file_read = csv.DictReader(open_csv, delimiter=',') items_imported = 0 for row in file_read: if Opportunity.query.filter_by( name=row['opportunity']).first(): continue opportunity = Opportunity( name=row['opportunity'], location_street=row['location_street1'], location_city=row['location_city'], location_state=row['location_state'], location_zip=row['location_zip'], volunteers_needed=row['volunteers_needed']) if row['start_date']: opportunity.start_date = datetime.strptime( row['start_date'], '%Y%m%d').date() if row['end_date']: opportunity.end_date = datetime.strptime( row['end_date'], '%Y%m%d').date() if row['location_street2']: Opportunity.location_street = row['location_street1'] \ + ', ' + row['location_street2'] if not Partner.query.filter_by(name=row['partner']).first(): partner = Partner(name=row['partner']) db.session.add(partner) db.session.commit() index_one_record(partner) opportunity.partner_id = Partner.query.filter_by( name=row['partner']).first().id opportunity.partner_string = Partner.query.filter_by( name=row['partner']).first().name db.session.add(opportunity) db.session.commit() index_one_record(opportunity) items_imported += 1 open_csv.close() os.remove(os.path.join(Config.UPLOAD_FOLDER, import_filename)) return str(items_imported) + ' Opportunities imported'
def create_partner_api(): data = request.get_json() or {} if 'name' not in data or data['name'] == "": return bad_request('must include a partner name') if Partner.query.filter_by(name=data['name']).first(): return bad_request('this partner already exists') partner = Partner() partner.from_dict(data, new_partner=True) db.session.add(partner) db.session.commit() index_one_record(partner) response = jsonify(partner.to_dict()) response.status_code = 201 response.headers['Location'] = url_for('api.get_partner_api', id=partner.id) return response
def delete_tag_categories_api(id): if not TagCategory.query.filter_by(id=id).first(): return bad_request('this tag category does not exist') tag_category = TagCategory.query.get_or_404(id) db.session.delete(tag_category) db.session.commit() return '', 204
def create_tag_api(): data = request.get_json() or {} if 'name' not in data or 'tag_category' not in data: return bad_request('must include tag and tag category') if Tag.query.filter_by(name=data['name']).first(): return bad_request('this tag already exists') if not TagCategory.query.filter_by(name=data['tag_category']).first(): return bad_request('this tag category does not exist') tag = Tag() tag.from_dict(data, new_tag=True) db.session.add(tag) db.session.commit() response = jsonify(tag.to_dict()) response.status_code = 201 response.headers['Location'] = url_for('api.get_tag_api', id=tag.id) return response
def delete_tag_api(id): if not Tag.query.filter_by(id=id).first(): return bad_request('this tag does not exist') tag = Tag.query.get_or_404(id) db.session.delete(tag) db.session.commit() return '', 204
def delete_user_api(id): if not User.query.filter_by(id=id).first(): return bad_request('this user does not exist') user = User.query.get_or_404(id) db.session.delete(user) db.session.commit() return '', 204
def delete_opportunity_api(id): if not Opportunity.query.filter_by(id=id).first(): return bad_request('this opportunity does not exist') opportunity = Opportunity.query.get_or_404(id) db.session.delete(opportunity) db.session.commit() index_one_record(opportunity, delete=True) return '', 204
def create_user_api(): data = request.get_json() or {} if 'username' not in data or 'password' not in data: return bad_request('must include username and password field') if User.query.filter_by(username=data['username']).first(): return bad_request('this user already exists') if 'roles' in data: for role in data['roles']: if not Role.query.filter_by(name=role).first(): return bad_request('that is not an existing role') user = User() user.from_dict(data) db.session.add(user) db.session.commit() response = jsonify(user.to_dict()) response.status_code = 201 response.headers['Location'] = url_for('api.get_user_api', id=user.id) return response
def update_tag_api(id): tag = Tag.query.get_or_404(id) data = request.get_json() or {} if 'name' in data and data['name'] != tag.name and \ Tag.query.filter_by(name=data['name']).first(): return bad_request('please use a different tag name') tag.from_dict(data, new_tag=False) db.session.commit() return jsonify(tag.to_dict())
def create_tag_category_api(): data = request.get_json() or {} if 'category_name' not in data: return bad_request('must include tag category name field') if TagCategory.query.filter_by(name=data['category_name']).first(): return bad_request('this tag category already exists') if 'tags' in data: for tag in data['tags']: if not Tag.query.filter_by(name=tag).first(): new_tag = Tag(name=tag) db.session.add(new_tag) tag_category = TagCategory() tag_category.from_dict(data, new_tag_category=True) db.session.add(tag_category) db.session.commit() response = jsonify(tag_category.to_dict()) response.status_code = 201 response.headers['Location'] = url_for('api.get_tag_category_api', id=tag_category.id) return response
def delete_partner_api(id): if not Partner.query.filter_by(id=id).first(): return bad_request('this partner does not exist') partner = Partner.query.get_or_404(id) if Opportunity.query.filter_by(partner_id=id): for opp in Opportunity.query.filter_by(partner_id=id): db.session.delete(opp) db.session.commit() db.session.delete(partner) db.session.commit() index_one_record(partner, delete=True) return '', 204
def update_tag_category_api(id): tag_category = TagCategory.query.get_or_404(id) data = request.get_json() or {} if 'name' in data and data['name'] != tag_category.name and \ TagCategory.query.filter_by(name=data['name']).first(): return bad_request('please use a different tag category name') if 'tags' in data: for tag in data['tags']: if not Tag.query.filter_by(name=tag).first(): new_tag = Tag(name=tag) db.session.add(new_tag) tag_category.from_dict(data, new_tag_category=False) db.session.commit() return jsonify(tag_category.to_dict())
def create_opportunity_api(): data = request.get_json() or {} if 'name' not in data or 'partner_name' not in data: return bad_request('must include opportunity and partner name field') if Opportunity.query.filter_by( name=data['name'], partner_id=Partner.query.filter_by( name=data['partner_name']).first().id).first(): return bad_request('this opportunity already exists with this partner') data['partner_id'] = Partner.query.filter_by( name=data['partner_name']).first().id opportunity = Opportunity() opportunity.from_dict(data, new_opportunity=True) opportunity.partner_string = Partner.query.filter_by( id=opportunity.partner_id).first().name db.session.add(opportunity) db.session.commit() index_one_record(opportunity) response = jsonify(opportunity.to_dict()) response.status_code = 201 response.headers['Location'] = url_for('api.get_opportunity_api', id=opportunity.id) return response