Badgeyay has two main components, the Python-Flask backend server and the EmberJS frontend.
EmberJS frontend uses ember data to save the data from the backend server api into the store of EmberJS frontend. To make the ember data frontend comply with backend api we need the backend server to send responses that comply with the standards of the JSON-API.
What is JSON-API?
As stated by JSONAPI.ORG
"If you've ever argued with your team about the way your JSON responses should be formatted, JSON API can be your anti-bikeshedding tool."
To put it up simply, JSON-API is a way of representing the JSON data that is being generated by the server backend. In this way we represent the JSON data in a particular way that follows the JSON-API convention. An example of data that follows json-api standards is given below:
We proceeded on to adding json-api into the Python-Flask backend. Before we proceed to adding json-api, we first need to install marshmallow_jsonapi
To install marshmallow_jsonapi
$ ~ pip install marshmallow-jsonapi
After installing marshmallow_jsonapi, we proceed onto making our first schema.
A schema is a layer of abstraction that is provided over a database model that can be used to dump data from or into an object. This object can therefore be used to either store in database or to dump it to the EmberJS frontend. Let us create a schema for File.
from marshmallow_jsonapi.flask import Schema
from marshmallow_jsonapi import fields
class FileSchema(Schema):
class Meta:
type_ = 'File'
self_view = 'fileUploader.get_file'
kwargs = {'id': '<id>'}
id = fields.Str(required=True, dump_only=True)
filename = fields.Str(required=True)
filetype = fields.Str(required=True)
user_id = fields.Relationship(
self_url='/api/upload/get_file',
self_url_kwargs={'file_id': '<id>'},
related_url='/user/register',
related_url_kwargs={'id': '<id>'},
include_resource_linkage=True,
type_='User'
)
So we have successfully created a Schema for getting files. This schema has an id, filename and filetype. It also has a relationship with the User.
Let us now create a route for this Schema. The below snippet of code is used to find a given file using this schema.
After adding JSON-API standards to the backend API we can easily integrate it with the EmberJS frontend. Now we can work on adding more schemas as a method of layers of abstraction so that the backend can serve more functionalities and the data can be consumed by the frontend as well.
Badgeyay project is divided into two parts i.e front-end with Ember JS and back-end with REST-API programmed in Python.
Badgeyay has many features related to enhancement in the generation of badges. It gives the choice of uploading data entries i.e by CSV or manually. There are options available for choosing Badge Background and font specifications. But there is an important feature missing which will make the service more user-friendly in terms of creation of badges for different types of events i.e, Badge Size.
Badge Size feature is implemented in Backend. I need to send the data in the backend in the desired format for creation of Badges with different sizes.
In this Blog, I will be discussing how I implemented Badge Size feature in Badgeyay Frontend in my Pull Request.
Let’s get started and understand it step by step.
Step 1:
Create Badge Size component with Ember CLI.
$ ember g component badge-component/badge-size
Step 2:
Write the HTML required in the badge-size component:
Badgeyay project is divided into two parts i.e front-end with Ember JS and back-end with REST-API programmed in Python.
Badgeyay frontend has many features like Login and Sign up features and Login with OAuth and the most important, the badge generation feature is also up and running but the important thing from the User’s perspective is to get notified of all the actions performed in the application so that user can proceed easily further after performing a specific action in the Application..
In this Blog, I will be discussing how I integrated ember-notify in Badgeyay frontend to notify user about the actions performed in my Pull Request.
Ember-notify displays a little notification message down the bottom of our application.
Let’s get started and understand it step by step.
Step 1:
This module is an ember-cli addon, so installation is easy:
npm install ember-notify --save-dev
Step 2:
Inject the notify service in the controller of the template. Here, I will showing how I added it in showing Log In and Logout messages and you can check the whole code in my Pull request for other controllers also.
// controllers/login.js import Ember from 'ember';
import Controller from '@ember/controller';
const { inject } = Ember;
exportdefault Controller.extend({
session : inject.service(),
notify : inject.service('notify'),
..........
this_.transitionToRoute('/');
this_.get('notify').success('Log In Successful');
}).catch(function(err) {
console.log(err.message);
this_.get('notify').error('Log In Failed ! Please try again');
});
............
this_.transitionToRoute('/');
this_.get('notify').success('Log In Successful');
})
.catch(err => {
console.log(err);
});
}).catch(function(err) {
console.log(err.message);
this_.get('notify').error('Log In Failed ! Please try again');
});
..........
// controllers/logout.jsimport Ember from 'ember';
import Controller from '@ember/controller';
const { inject } = Ember;
exportdefault Controller.extend({
session : inject.service(),
notify : inject.service('notify'),
beforeModel() {
returnthis.get('session').fetch().catch(function() {});
},
actions: {
logOut() {
this.get('session').close();
this.transitionToRoute('/');
this.get('notify').warning('Log Out Successful');
}
}
});
I have implemented ember-notify for Logging In and Out feature & in the similar way I have implemented it for other controllers and complete code can be seen in my Pull Request.
Step 3::
Now run the server to see the implemented changes by following command.
$ ember serve
Navigate to localhost and perform login and logout actions to see the changes.
Successful Log In
Successful Log out
Successful CSV Upload
Now, we are done with the integration of ember-notify in Badgeyay frontend to notify user about the actions performed in the Application.
Onboarding screens are designed to introduce users to how the application works and what main functions it has, to help them understand how to use it. It can also be helpful for developers who intend to extend the current project.
When you enter in the SUSI iOS app for the first time, you see the onboarding screen displaying information about SUSI iOS features. SUSI iOS is using Material design so the UI of Onboarding screens are following the Material design.
There are four onboarding screens:
Login (Showing the login features of SUSI iOS) – Login to the app using SUSI.AI account or else signup to create a new account or just skip login.
Chat Interface (Showing the chat screen of SUSI iOS) – Interact with SUSI.AI asking queries. Use microphone button for voice interaction.
SUSI Skill (Showing SUSI Skills features) – Browse and try your favorite SUSI.AI Skill.
Chat Settings (SUSI iOS Chat Settings) – Personalize your chat settings for the better experience.
Onboarding Screens User Interface
There are three important components of every onboarding screen:
Title – Title of the screen (Login, Chat Interface etc).
Image – Showing the visual presentation of SUSI iOS features.
Description – Small descriptions of features.
Onboarding screen user control:
Pagination – Give the ability to the user to go next and previous onboarding screen.
Swiping – Left and Right swipe are implemented to enable the user to go to next and previous onboarding screen.
Skip Button – Enable users to skip the onboarding instructions and go directly to the login screen.
Badgeyay is a badge generator and its main functionality is generating badges. Since the beginning of GSoC 2018 period, Badgeyay is under refactoring and remodeling process. We have introduced many APIs to make sure that Badgeyay works. Now, the badge generator has an endpoint to generate badges for your events/meetups
How to create badges?
Creating badges using the newly formed API is simpler than before. All you need to do is pass some basic details of the image you want, the data you want, the size and the color of font etc to the API and woosh! Within a blink of your eye the badges are generated.
Backend requires some data fields to generate badges
“csv” is the filename of the csv that the user uploads and get back as a result, “image” is the image name that user gets after a successful upload to the respective APIs, “text-color” is the color of the text that the user wants on the badges.
Once the user sends the data to the API, the required route is triggered and the data is checked,If the data is not present an error response is sent back to the user so as to inform them about the misplacement or improper format of data.
import os
from flask import Blueprint, jsonify, request
from flask import current_app as app
# from api.helpers.verifyToken import loginRequired
from api.utils.response import Response
from api.utils.svg_to_png import SVG2PNG
from api.utils.merge_badges import MergeBadges
router = Blueprint('generateBadges', __name__)
@router.route('/generate_badges', methods=['POST'])
def generateBadges():
try:
data = request.get_json()
except Exception as e:
return jsonify(
Response(401).exceptWithMessage(str(e),'Could not find any JSON'))
if not data.get('csv'):
return jsonify(
Response(401).generateMessage('No CSV filename found'))
if not data.get('image'):
return jsonify(Response(401).generateMessage('No Image filename found'))
csv_name = data.get('csv')
image_name = data.get('image')
text_color = data.get('text-color') or '#ffffff'
svg2png = SVG2PNG()
svg2png.do_text_fill('static/badges/8BadgesOnA3.svg', text_color)
merge_badges = MergeBadges(image_name, csv_name)
merge_badges.merge_pdfs()
output = os.path.join(app.config.get('BASE_DIR'), 'static', 'temporary', image_name)
return jsonify(
Response(200).generateMessage(str(output)))
After the data is received, we send it to MergeBadges which internally calls the GenerateBadges class which creates the badges.
Brief explanation of the Badge Generation Process:
- Gather data from the user- Fill the SVG for badges with the text color
- Load the image from uploads directory
- Generate badges for every individual
- Create PDFs for individual Badges
- Merge those PDFs to provide an all-badges pdf to the user
And this is how we generated badges for the user using the Badgeyay Backend API.
How is this effective?
We are making sure that the user chooses the image and csv that he/she has uploaded only,
In this way we maintain a proper workflow, we also manage these badges into the database and hence using the filenames helps a lot.It does not involve sending huge files and a lot of data like we had in the previous API.
Earlier, we used to send the image and the csv altogether that caused a serious mismanagement of the project. In this case we are accepting the CSVs and the Images on different API routes and then using the specific image and csv to make badges. We can now more easily relate to the files associated with each and every badge and henceforth we can easily manage them in the database.
Further Improvements
We will work on adding security to the route so that not anyone can create badges. We also need to integrate database into badges generated service so that we can maintain the badges that the user has generated.
Badgeyay has seen many changes in the recent past during its refactoring. It started off with backend and we have now transition to remodeling backend as well.
The backend transition is working perfectly. We have established sufficient APIs so far to get it working.
Some of the most important APIs that we created are
Image Upload API
File Upload API
Why do we need APIs?
We need APIs so that the frontend written in Ember JS can coordinate with the backend written in Python Flask with the database being PostgreSQL.
Creating the APIs
Creating these APIs is easy and straightforward. The following APIs are written in Python Flask with a backend database support of PostgreSQL.
Image Upload API
The image upload API considers that the frontend is sending the Image as a base64 encoded string and the backend is supposed to accept this string and convert this string into an image and save it onto the server.
We proceed by creating a file named fileUploader.py and code the following API.
First of all, we need to declare the imports
from flask import Blueprint, request, jsonify
from api.utils.response import Response
from api.helpers.verifyToken import loginRequired
from api.helpers.uploads import saveToImage, saveToCSV
Now, let’s create a route for image upload.
router = Blueprint('fileUploader', __name__)
@router.route('/image', methods=['POST'])
@loginRequired
def uploadImage():
try:
image = request.json['data']
except Exception as e:
return jsonify(
Response(400).exceptWithMessage(
str(e),
'No Image is specified'))
extension = request.json['extension']
try:
imageName = saveToImage(imageFile=image, extension=extension)
except Exception as e:
return jsonify(
Response(400).exceptWithMessage(
str(e),
'Image could not be uploaded'))
return jsonify(
Response(200).generateMessage({
'message': 'Image Uploaded Successfully',
'unique_id': imageName}))
We are using the saveToImage function to actually save the image to the backend server.
The function definition of saveToImage function is given below.
@router.route('/file', methods=['POST'])
@loginRequired
def fileUpload():
if 'file' not in request.files:
return jsonify(
Response(401).generateMessage(
'No file is specified'))file = request.files['file']
try:
csvName = saveToCSV(csvFile=file, extension='.csv')
except Exception as e:
return jsonify(
Response(400).exceptWithMessage(
str(e),
'CSV File could not be uploaded'))return jsonify(
Response(200).generateMessage({
'message': 'CSV Uploaded successfully',
'unique_id': csvName}))
What happens to the uploaded files?
The uploaded files gets saved into their respective directories, i.e. static/uploads/csv for CSV files and static/uploads/images for Image uploads.
The developer can view them from their respective folders. The static folder has been added to .gitignore so that it does not gets uploaded to github repository.
Everything has been taken care of with immense accuracy and proper error handling.
Further Improvements
Further improvements in Badgeyay includes adding separate database models, work on adding a beautiful frontend and to add proper routes for completing the backend.
Badgeyay project is divided into two parts i.e front-end of Ember JS and back-end with REST-API programmed in Python.
We already have logging In features implemented with the help of Firebase Authentication. A User can login in the Badgeyay with the help of Google, Facebook and Twitter credentials through a single click. Now, the challenging part is to implement the sign up with Email feature in Frontend and Backend to enable the user to signup and Login with the help of Email and Password
In this blog, I will be discussing how I set up Sign up feature in Badgeyay frontend to send the data in backend besides having Oauth logging features in Badgeyay integrated with Firebase in my Pull Request.
The sign up form is already implemented and I have already mentioned in my previous blog. So we need to send the form data to backend to register user so that user can login using the registered credentials. We need an Adapter, Signup action, controller , Signup Data model and a serializer for doing this task.
Let’s get started and understand the terminologies before implementing the feature.
What is Ember Data ?
It is a data management library for Ember Framework which help to deal with persistent application data.
We will generate Ember data model using Ember CLI in which we will define the data structure we will be requiring to provide to our application for User Signup.
We already have the signup form implemented in frontend. Now we need to provide a action to the form when the user enters the data in form.
If we add the{{action}} helper to any HTML DOM element, when a user clicks the element, the named event will be sent to the template’s corresponding component or controller.
We need to add signUp action in sign-up component and controller.
// Signup Controller import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
exportdefault Controller.extend({
routing : service('-routing'),
actions : {
signUp(email, username, password) {
const _this =this;
let user_ =this.get('store').createRecord('user-signup', {
email,
username,
password
});
user_.save()
.then(record => {
_this.transitionToRoute('/');
})
.catch(err => {
console.log(err);
});
}
}
});
// Sign up Componentimport Component from '@ember/component';
exportdefault Component.extend({
init() {
this._super(...arguments);
},
email :'',
password :'',
isLoading :false,
actions: {
signUp(event) {
event.preventDefault();
let email ='';
let password ='';
let username ='';
email =this.get('email');
password =this.get('password');
username =this.get('username');
this.get('signUp')(email, username, password);
}
},
});
What is an Adapter ?
An adapter determines how the data is persisted to a backend data store. We can configure the backend host, URL format and headers for REST API.
Now as we have specific Data Model for User Signup that we will be using for communicating with its backend so we have to create User-Signup Adapter with the help of Ember-CLI.
Step 1: Generate User Signup Adapter by following together.
$ ember generate adapter user-signup
Step 2: Extend the Adapter according to User-Signup Model.
Serializers format the Data sent to and received from the backend store. By default, Ember Data serializes data using the JSON API format.
Now as we have specific Data Model for User Signup that we will be using for communicating with its backend so we have to create User-Signup Serializer with the help Ember-CLI.
Step 1: Generate the User Signup Adapter by following command:
$ ember generate serializer user-signup
Step 2: Extend the serializer according to User-Signup Model.
We have successfully set up the User Signup in the frontend and data is communicated to backend in JSON API v1 specification with the help of serializers and Adapters.
This is how I set up Sign up feature in Badgeyay frontend to send the data in backend besides having Oauth logging features in Badgeyay integrated with Firebase in my Pull Request.
Badgeyay project is now divided into two parts i.e front-end of Ember JS and back-end with REST-API programmed in Python.
After a discussion, we have finalized to go with Semantic UI framework which uses simple, common language for parts of interface elements, and familiar patterns found in natural languages for describing elements. Semantic allows to build beautiful websites fast, with concise HTML, intuitive javascript and simplified debugging, helping make front-end development a delightful experience. Semantic is responsively designed allowing a web application to scale on multiple devices. Semantic is production ready and partnered with Ember framework which means we can integrate it with Ember frameworks to organize our UI layer alongside our application logic.
In this blog, I will be discussing how I added Log In and Signup Forms and their validations using Semantic UI for badgeyay frontend in my Pull Request.
Let’s get started and understand it step by step.
Step 1:
Generate ember components of Login and Sign up by using the following command :
$ ember generate component forms/login-form
$ ember generate component forms/signup-form
Step 2:
Generate Login and Sign up route by following commands.
$ ember generate route login
$ ember generate route signup
Step 3:
Generate Login and Sign up controller by following commands.
$ ember generate controller login
$ ember generate controller signup
Step 4:
Now we have set up the components, routes, and controllers for adding the forms for login and Sign up. Now let’s start writing HTML in handlebars, adding validations and implementing validations for the form components. In this blog, I will be sharing the code of Login form and actions related to logging In of user. You can check the whole code my Pull Request which I have made for adding these Forms.
Badgeyay project is divided into two parts i.e front-end of Ember JS and back-end with REST-API programmed in Python.
We have integrated PostgreSQL as the object-relational database in Badgeyay and we are using SQLAlchemy SQL Toolkit and Object Relational Mapper tools for working with databases and Python. As we have Flask microframework for Python, so we are having Flask-SQLAlchemy as an extension for Flask that adds support for SQLAlchemy to work with the ORM.
One of the challenging jobs is to manage changes we make to the models and propagate these changes in the database. For this purpose, I have added Added Migrations to Flask SQLAlchemy for handling database changes using the Flask-Migrate extension.
In this blog, I will be discussing how I added Migrations to Flask SQLAlchemy for handling Database changes using the Flask-Migrate extension in my Pull Request.
First, Let’s understand Database Models, Migrations, and Flask Migrate extension. Then we will move onto adding migrations using Flask-Migrate. Let’s get started and understand it step by step.
What are Database Models?
A Database model defines the logical design and structure of a database which includes the relationships and constraints that determine how data can be stored and accessed. Presently, we are having a User and file Models in the project.
What are Migrations?
Database migration is a process, which usually includes assessment, database schema conversion. Migrations enable us to manipulate modifications we make to the models and propagate these adjustments in the database. For example, if later on, we make a change to a field in one of the models, all we will want to do is create and do a migration, and the database will replicate the change.
What is Flask Migrate?
Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations are made available through the Flask command-line interface or through the Flask-Script extension.
Now let’s add support for migration in Badgeyay.
Step 1 :
pip install flask-migrate
Step 2 :
We will need to edit run.py and it will look like this :
importosfromflaskimport Flask
fromflask_migrateimport Migrate // Imported Flask Migrate
fromapi.dbimport db
fromapi.configimport config
......
db.init_app(app)
migrate = Migrate(app, db) // It will allow us to run migrations
......
@app.before_first_request
defcreate_tables():
db.create_all()
if __name__ =='__main__':
app.run()
Step 3 :
Creation of Migration Directory.
export FLASK_APP=run.py
flask db init
This will create Migration Directory in the backend API folder.
We will do our first Migration by the following command.
flask db migrate
Step 5 :
We will apply the migrations by the following command.
flask db upgrade
Now we are all done with setting up Migrations to Flask SQLAlchemy for handling database changes in the badgeyay repository. We can verify the Migration by checking the database tables in the Database.
This is how I have added Migrations to Flask SQLAlchemy for handling Database changes using the Flask-Migrate extension in my Pull Request.
When we build a full scale production application, we make sure that everything is modeled correctly and accordingly to the need of the code. The code must be properly maintained as well as designed in such a way that it is less prone to errors and bugs.
Badgeyay is also targeting to be a full production application, and in order to achieve it we first need to re-factor the code and model it using a strong yet maintainable structure.
What is the current state of Badgeyay?
Currently Badgeyay is divided into two sub folders.
\badgeyay
\frontend
\backend
.
.
It is backed by two folders, viz backend and frontend. The ‘backend’ folder handles the API that the service is currently running. The ‘frontend’ folder houses the Ember based frontend logic of the application.
Improvements to Badgeyay Backend
We have worked on improving Backend for Badgeyay. Instead of traditional methods, i.e. current method, of API development; We employ a far better approach of using Flask Blueprint as a method of refactoring the API.
The new backend API resides inside the following structure.
\badgeyay
\backend
\blueprint
\api
The API folder currently holds the new API being formatted from scratch using
Flask Blueprint
Flask Utilities like jsonify, response etc
The new structure of Badgeyay Backend will follow the following structure
api
\config
\controllers
\helpers
\models
\utils
db.py
run.py
The folders and their use cases are given below
\config
Contain all the configuration files
Configurations about URLs, PostgreSQL etc
\controllers
This will contain the controllers for our API
Controllers will be the house to our routes for APIs
\helpers
Helpers folder will contain the files directly related to API
\models
Models folder contains the Schemas for PostgreSQL
Classes like User etc will be stored in here
\utils
Utils will contain the helper functions or classes
This classes or functions are not directly connected to the APIs
db.py
Main python file for Flask SQLAlchemy
run.py
This is the main entry point.
Running this file will run the entire Flask Blueprint API
How does it help?
It helps in making the backend more solid.
It helps in easy understanding of application with maintained workflow.
Since we will be adding a variety of features during Google Summer of Code 2018 therefore we need to have a well structured API with well defined paths for every file being used inside it.
It will help in easy maintaining for any maintainer on this project.
Development of the API will be faster in this way, since everything is divided into sub parts therefore many people can work on many different possibilities on the same time.
Further Improvements
Since this structure has been setup correctly in Badgeyay now, so we can work on adding separate routes and different functionalities can be added simultaneously.
You must be logged in to post a comment.