Skip to main content

Code base to jump start flask-classy system

This post is just to announce my sharing of a source code base which can be used to jump start development using the python micro framework flask and it's excellent extension flask-classy. Do note that this code base uses the Ace Admin bootstrap theme. Please purchase the license for your use from wrapbootstrap or remove it from your source code referencing it.

To begin, you need to clone the repository using mercurial:
hg clone https://bitbucket.org/abdza/flask-classy-base-system examplesystem

Then you need to create the virtualenv for your system:
virtualenv --python=/usr/bin/python2 examplesystem

Do note that my example here is running on arch linux which uses python3 as the default python version. I suggest the above command to use python2 in your virtualenv because it makes things easier to migrate to systems using python2 (eg ubuntu, centos and others).

The you need to go into the directory and activate the virtualenv:
cd examplesystem
source bin/activate

Then you need to install all the required python modules into your virtualenv:
pip install -r requirements.txt

Once all that is done you need to configure your database access by editing the file app/config.py
Update the SQLALCHEMY_DATABASE_URI line with your own database setting:
SQLALCHEMY_DATABASE_URI = 'postgresql://<username>:<password>@localhost/<database>'

So for this example I changed it to:
SQLALCHEMY_DATABASE_URI = 'postgresql://exampleuser:examplepass@localhost/exampledb

You'd probably want to update the SECRET_KEY, APP_NAME and COMPANY_NAME too.

Then you need to create the appropriate postgres database, role, grant and allow the user to login.
sudo -u postgres psql
postgres=# create database exampledb;
postgres=# create role exampleuser with password 'examplepass';
postgres=# grant all on database exampledb to exampleuser;
postgres=# alter user exampleuser login;
Then you can test the user and database connection:
postgres=# \c - exampleuser
postgres=> \c exampledb
If there is no error, then you can continue to the next step after you quit postgres:
exampledb=> \q
You then need to init the database by running:
./run.py db init
./run.py db migrate
./run.py db upgrade
That should get your database updated with all your models (which currently is only User)
You can try running it now:
./run.py
Then in your web browser go to the address below and register your new user:
http://localhost:5000/user/register

After all of that, you should probably edit your .hg/hgrc file and point the default path to your own repository.

Once everything is set, you can begin your development. Add a new model by adding a file in the app/models directory. For example lets add the file app/models/Post.py:
from app import db

class Post(db.Model):
 id = db.Column(db.Integer,primary_key=True)
 name = db.Column(db.String)
 content = db.Column(db.Text)

Then you need to tell your app about the existence of this model by adding the following line to app/models/__init__.py:
from Post import Post
Once you have added your model files, you can generate the view associated with that model:
./run.py genview

You can then see the generated view files at app/views/PostView.py

Before you can view the view in your web browser, make sure your database get updated first with your newly added model:
./run.py db migrate
./run.py db upgrade
Then you can view the view page by pointing your web browser to:
http://localhost:5000/post

You can then start editing the view for your model and your processes. For example we can add the Post field in your form by changing the ItemForm class in your app/views/PostView.py:
class ItemForm(Form):
 name = StringField('Name',validators=[DataRequired()])
 post = TextAreaField('Post')
That's all for the introduction to this code base. Hope it is useful for you to jump start your development using flask and flask-classy.

Comments

Popular posts from this blog

haproxy on Centos 7 with SELinux

Setting up haproxy and varnish with reference to the configuration from these sites: http://sharadchhetri.com/2014/12/20/how-to-install-varnish-4-version-on-centos-7-rhel-7/ https://www.upcloud.com/support/haproxy-load-balancer-centos/ And finally can access plone on the server. But when I check the haproxy status, only client1 can be accessed. All other clients were down with general socket error, permission denied. Googling a bit I finally found out it was due to SELinux. Refer to this page: http://stackoverflow.com/questions/26420729/haproxy-health-check-permission-denied With the solution from that page, I installed policycoreutils-python and tried to run the command given: semanage port --add --type http_port_t --proto tcp 8081 But that command failed with the error that the port was already defined. When I looked it up using: semanage port -l | grep 8081 It belonged to some other type (transproxy_port_t). When I tried to delete the type using: semanage port -d -t transpro…

Installing Centos 7 on HP G7 Server

When I first initially tried it, the installer cannot detect any local hard disk to install to. After much googling found out that it was because a certain driver for the Smart Array Controller is no longer available on Centos 7. To allow for use of the new driver on old hardware, you need to enable the option. Once the DVD has booted and on the first menu screen, highlight the first option and then press tab which would bring you to a command line where you can add additional parameters to boot. Add the following parameters: hpsa.hpsa_allow_any=1 hpsa.hpsa_simple_mode=1 And then press enter to boot. You should now be able to install as you wish. After installation, on the first boot, make sure at the menu, press 'e' to edit the parameters and add them again. Then once booted, go to /boot/grub2/grub.cfg to permanently add the parameters to the menu. Refer to http://serverfault.com/questions/611182/centos-7-x64-and-hp-proliant-dl360-g5-scsi-controller-compatibility and it&#…

Me and my sleep apnea

I have been suffering from sleep apnea for quite a few years already. It started to show when I was still with OSCC some 6 years back when I would sometimes "accidentally" sleep at my cubicle. Early on I just dismissed it as because I was growing fat (we ate a lot at OSCC.. :P and I barely went anywhere except my cubicle) and also because of babies. Oh babies.. those cute little things that keeps you awake at night so that they can be so cute and charming when your friends and relatives come over the next day. So it was dismissed. Later on I got more and more used to sleeping at work, started associating it with my lack of motivation and sometimes feeling of downright depression. And I kept growing fatter and fatter, which I assumed was the reason why I was getting more and more tired.

But then around 2 years ago a friend of mine suggested maybe I've got sleep apnea. And a few other people suggested I go to a sleep clinic and all that. Put it off for quite a while. Until…