#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = 'Jiayi Li'

import time, hashlib

from flask import current_app

from app import db

class User(object):
    
    def __init__(self, **kw):
        self.name = kw.get('name')
        self.email = kw.get('email')
        self.password = kw.get('password')
        self.created = kw.get('created', int(time.time()))
        self.admin = kw.get('admin', False)
        self.image = kw.get('image', '/static/img/default.png')
        self.register()
    
    def register(self):
        sha1_password = self.password + self.email + 'the-Salt' # secondary encryption
        self.password = hashlib.sha1(sha1_password.encode('utf-8')).hexdigest()
        db.users.insert_one(self.__dict__)


class Blog(object):
    
    def __init__(self, **kw):
        self.user_id = kw.get('user_id')
        self.user_name = kw.get('user_name')
        self.user_image = kw.get('user_image')
        self.title = kw.get('title')
        self.tag = kw.get('tag', [])
        self.content = kw.get('content')
        self.summary = '%s%s' % (kw.get('content', '')[:140], '...')
        self.created = kw.get('created', int(time.time()))
        self.lastModified = False
        db.blogs.insert_one(self.__dict__) # save to mongodb


class Comment(object):
    
    def __init__(self, **kw):
        pass
@@ -392,31 +392,14 @@
 in =
- kw.get('admin',
  False
-)
 %0A   
@@ -721,16 +721,34 @@
 _dict__)
+ # save to mongodb
 %0A%0A%0Aclass
@@ -1194,9 +1194,10 @@
 last
-M
+_m
 odif
@@ -1337,12 +1337,369 @@
         
-pass
+self.blog_id = kw.get('blog_id')%0A        self.user_id = kw.get('user_id')%0A        self.user_name = kw.get('user_name')%0A        self.user_image = kw.get('user_image')%0A        self.content = kw.get('content')%0A        self.created = kw.get('created', int(time.time()))%0A        self.subcomment = False%0A        db.comments.insert_one(self.__dict__) # save to mongodb

add Comment model