Deployed…a few days earlier than planned.

Turns out deploying the site was much easier than I had expected. For hosting I chose Digital Ocean based on the multitude of recommendations online plus I was able to get a couple of months free by using a referral link.

So far it is great! They have a ton of useful and informative articles to walk you through most anything you are going to do. First, I signed up on the $5 plan and got a droplet set up with Ubuntu 16.04.3. I used a few different articles to get the site off the ground. Starting with this one to get the server set up: https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04

Basically this one walks you through creating a non-root user with sudo privileges, setting up SSH keys etc… then I went on to actually deploy my app…using this article:

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-16-04

This one walks through the process of installing everything and creating a basic flask app, at this step I actually cloned my repository instead…and then modified the rest of the tutorial based on my specific application.

Also had to install MongoDB to get the app to work correctly: https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-ubuntu-16-04

I then add my Domain and set the nameservers with my registrar…added the @, WWW A records to point toward my domain…

Then I set up LetEncrypt for SSL, https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

All in all it only took about an hour…and it’s online. I did a basic video which I need to improve some of my setup but…my goal was to get this done by 9/6 and it looks like I made it with time to spare. https://www.flaskcasts.com

I am totally new to all of this so if anyone has any suggestions on how I can improve…I am all ears.

Pagination

So the concept escaped me for a couple of days. I actually had considered abandoning my MongoDB trials in favor of a SQL based system. What I discovered was that I am more persistent in finding a solution to my problem than I originally thought. I stumbled upon this Gist: https://gist.github.com/wonderb0lt/10645080

This is a very simple and basic Pagination class written for MongoEngine. Flask already has an extension for MongoEngine with this already built in. After looking at the code I realized all this is doing is passing an iterable to the Pagination class and then building links in the template. So I decided I would type it out line by line so I could get a better grasp on what was happening in the code.

So now I have Flask-PyMongo communicating with my database and was able to build the models in such a way that makes more sense to me at this stage of my learning.

For instance, my Post model looks like this:

class Post(object):


    def __init__(self, title, content, author, created=None):
        self.title = title
        self.content = content
        self.author = author
        self.created = datetime.datetime.utcnow().strftime('%A %x @ %H:%M:%S') \
            if created is None else created

    def __repr__(self):
        return "<Post {}>".format(self.title)

    def json(self):
        return {
            "title": self.title,
            "content": self.content,
            "author": self.author,
            "created": self.created
        }

    def save(self):
        mongo.db.posts.update({'title': self.title}, self.json(), upsert=True)

MongoEngine allows reference fields so you can actually store the author object in the post document…which is kind of cool becuase you could do something like this from the template post.author.full_name. So I was a little stuck on trying to find a way to do this using PyMongo until I read this post: https://docs.mongodb.com/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/

It explains a more efficient way of doing this. In my case I am referencing the user by the author variable, so I have set my User class up so that _id is a unique username and will reference the user by the username.

See the User class:

class User(object):

    def __init__(self, _id, email, password, fullname):
        self._id = _id # username, unique
        self.email = email
        self.password = password
        self.fullname = fullname

    def __repr__(self):
        return "<User {}>".format(self.fullname)

    def json(self):
        return {
            "_id": self._id,
            "email": self.email,
            "password": self.password,
            "fullname": self.fullname
        }

    @staticmethod
    def get_user(user_id):
        return mongo.db.users.find_one({"_id": user_id})

    def save(self):
        mongo.db.users.update({"_id": self._id}, self.json(), upsert=True)

So using the get_user() method I can get the fullname…

LEARNING A LOT! and having a lot of fun!

Also found this library…for generating fake data…VERY useful http://mimesis.readthedocs.io/en/latest/