Badgeyay is not just an anonymous badge generator that creates badges according to your needs, but it now has an admin section that allows the admin of the website to control and look over the statistics of the website.
Why do we need such an API?
For an admin, one of the most common functionality is to gather the details of the users or the files being served onto or over the server. Not just that, but the admin must also be aware about the traffic or files on the server in a particular duration of time. So we need an API that can coordinate all the stuff that requires dated queries from the backend database.
Adding the functionality to backend
Let us see how we implemented this functionality into the backend of the project.
Step 1 : Adding a route
This step involves adding a separate route that provides us with the output of the dated badges queries from backend.
@router.route(‘/get_badges_dated’, methods=[‘POST’]) |
This route allows us to get badges produced by any user during a certain duration as a JSON API data object. This object is fed to the frontend to render the badges as cards.
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 DatedBadgeSchema(Schema): id = fields.Str(required=True, dump_only=True) |
class AllBadges(Schema): id = fields.Str(required=True, dump_only=True) |
This is the DatedBadge schema that produces the output results of the POST request on the route. And there is the AllBadges schema that produces the output results of the POST request on the route.
Further Improvements
We are working on adding multiple routes and adding modifications to database models and schemas so that the functionality of Badgeyay can be extended to a large extent. This will help us in making this badge generator even better.
Resources
- The Pull Request for the same : https://github.com/fossasia/badgeyay/pull/1040
- The Issue for the same : https://github.com/fossasia/badgeyay/issues/1039
- Read about adding routes Blueprint : http://flask.pocoo.org/docs/1.0/blueprints/
- Read about Schemas : https://github.com/marshmallow-code/marshmallow-jsonapi