A Badge generator like Badgeyay must be able to generate, store and export the user data as and when needed. This blog post is about adding the exporting functionality to badgeyay backend..
Why do we need such an API?
Exporting data is required for a user. A user may want to know the details he/she has uploaded to the system or server. In our case we are dealing with the fact of exporting the CSV data from backend of Badgeyay.
Adding the functionality to backend
Let us see how we implemented this functionality into the backend of the project.
Step 1 : Adding the necessary imports
We first need to import the required dependencies for the route to work
import os |
Step 2 : Adding a route
This step involves adding a separate route that provides us with the exported data from backend.
@router.route(‘/csv/data’, methods=[‘GET’]) if file is None: export_obj = { with open(os.path.join(app.config.get(‘BASE_DIR’), ‘static’, ‘uploads’, ‘csv’, export_obj[‘filename’]), “r”) as f: export_obj[‘file_data’] = base64.b64encode(export_obj[‘file_data’].encode()) return jsonify(ExportFileSchema().dump(export_obj).data) |
Step 2 : Adding a relevant Schema
After creating a route we need to add a relevant schema that will help us to deliver the badges generated by the user to the Ember JS frontend so that it can be consumed as JSON API objects and shown to the user.
class ExportFileSchema(Schema): id = fields.Str(required=True, dump_only=True) |
This is the ExportFileSchema that produces the output results of the GET request on the route. This helps us get the data onto the frontend.
Further Improvements
We are working on making badgeyay more comprehensive yet simple. This API endpoint needs to get registered onto the frontend. This can be a further improvement to the project and can be iterated over the next days.
Resources
- The Pull Request for the same : https://github.com/fossasia/badgeyay/pull/1138
- The Issue for the same : https://github.com/fossasia/badgeyay/issues/1137
- Read about adding routes Blueprint : http://flask.pocoo.org/docs/1.0/blueprints/
- Read about Schemas : https://github.com/marshmallow-code/marshmallow-jsonapi