#controllers/admin/categories/create.py
import config
from copy import deepcopy
from bottle import redirect,request
from models.categories import create

def call():
    userRole = request.get_cookie('userRole', secret=config.kdict['SECRET_KEY'])

    if(userRole == 'Admin'):
        create.call()

    redirect('/admin/category')

 

#models/categories/create.py
import uuid,config
from bottle import request

def call():
    if(request.forms.getunicode('id')):
        id = request.forms.getunicode('id')
    else:
        id = uuid.uuid4().hex
    category = {
        'id': id,
        'title': request.forms.getunicode('title'),
        'thumb': request.forms.getunicode('thumb'),
        'date': request.forms.getunicode('datetime'),
        'timestamp': config.firestore.SERVER_TIMESTAMP
    }

    category_ref = config.mydb.collection('categories').document(id)
    counter_ref = config.mydb.collection('counters').document('category')

    if(request.forms.getunicode('id')):
        category_ref.update(category)
    else:
        category_ref.set(category)
        counter_ref.update({'total': config.firestore.Increment(1)})
        

 

#controllers/admin/categories/read.py
import config
from copy import deepcopy
from bottle import template
from models.categories import read

def call():
    kdict = deepcopy(config.kdict)
    kdict['pageTitle'] = 'ទំព័រ​ជំពូក'
    kdict['route'] = '/admin/category'
    kdict['type'] = 'category'

    result = read.call(kdict['maxPosts'])
    kdict['items'] = result['categories']
    kdict['counter'] = result['counter']

    return template('base',data=kdict)

 

#models/categories/read.py
import config

def call(amount):
    categories_ref = config.mydb.collection("categories")
    query = categories_ref.order_by(
        "date",direction=config.firestore.Query.DESCENDING).limit(amount)

    results = query.get()
    categories = [category.to_dict() for category in results]

    counter_ref = config.mydb.collection("counters").document('category').get()
    if counter_ref.exists:
        counter = counter_ref.to_dict()
    
    return {'categories':categories, 'counter':counter}

 

Vercel: https://khmerweb-vlog.vercel.app

GitHub: https://github.com/Sokhavuth/vlog