Exemple #1
0
class Book():
  def __init__(self):
    self.lib = Lib()
    self.bookdb = Bookdb()

  def get_post_book(self):
    vdict = copy.deepcopy(config.vdict)
    vdict['blog_title'] = 'ចុះផ្សាយ​សៀវភៅ'
    vdict['datetime'] = self.lib.get_timezone()

    if (request.method == "POST") and ('logged-in' in session):
      title = request.form['fbook-title']
      if not title:
        title = 'unknown'
        
      content = request.form['fcontent']
      date = request.form['fbook-date']
      time = request.form['fbook-time']
      author = session['logged-in']

      try:
        date = datetime.datetime.strptime(date, "%d/%m/%Y")
      except ValueError:
        vdict['message'] = 'ទំរង់​កាលបរិច្ឆេទ​មិន​ត្រឹមត្រូវ!'
        return render_template('dashboard/book.html', data=vdict)

      try:
        time = datetime.datetime.strptime(time, "%H:%M:%S")
      except ValueError:
        vdict['message'] = 'ទំរង់​ពេល​វេលា​មិន​ត្រឹមត្រូវ!'
        return render_template('dashboard/book.html', data=vdict)

      id = str(uuid.uuid4().int)
      if 'edit' in session:
        self.bookdb.update(session['edit'], title, content, date, time, author)
        session.pop('edit', None)
      else:
        self.bookdb.insert(id, title, content, date, time, author)

      vdict['books'] = self.bookdb.select(vdict['dashboard_max_post'])
      vdict['thumbs'] = self.lib.get_thumbs(vdict['books'], 2)
      
      return render_template('dashboard/book.html', data=vdict)

    elif 'logged-in' in session:
      if 'edit' in session:
        session.pop('edit', None)
        
      vdict['books'] = self.bookdb.select(vdict['dashboard_max_post'])
      vdict['thumbs'] = self.lib.get_thumbs(vdict['books'], 2)
      return render_template('dashboard/book.html', data=vdict)
    else:
      return redirect('/login/')

  def delete(self, id):
    if 'logged-in' in session:
      self.bookdb.delete(id)
      return redirect('/dashboard/book/')

    return render_template('login.html', data=vdict)

  def edit(self, id):
    vdict = copy.deepcopy(config.vdict)
    vdict['blog_title'] = 'កែតំរូវ​សៀវភៅ'

    if 'logged-in' in session:
      vdict['books'] = self.bookdb.select(vdict['dashboard_max_post'])
      vdict['thumbs'] = self.lib.get_thumbs(vdict['books'], 2)
      vdict['book'] = self.bookdb.select(id=id)
      date = (vdict['book'][3]).strftime('%d/%m/%Y')
      time = (vdict['book'][4]).strftime('%H:%M:%S')
      vdict['datetime'] = (date, time)
      print(vdict['book'])
      return render_template('dashboard/book.html', data=vdict)

    return render_template('login.html', data=vdict)

  def load(self, page):
    if 'logged-in' in session:
      vdict = copy.deepcopy(config.vdict)
      vdict['books'] = self.bookdb.select(vdict['dashboard_max_post'], page=page)
      vdict['thumbs'] = self.lib.get_thumbs(vdict['books'], 2)

      new_list = []
      for book in vdict['books']:
        new_book = list(book)
        new_book[3] = book[3].strftime('%d/%m/%Y') 
        new_book[4] = book[4].strftime('%H:%M:%S') 
        new_list.append(new_book)

      vdict['books'] = new_list
      return vdict
    else:
      return render_template('login.html', data=vdict)
Exemple #2
0
class Post():
    def __init__(self):
        self.lib = Lib()
        self.postdb = Postdb()
        self.category = Categorydb()

    def get_post(self):
        vdict = copy.deepcopy(config.vdict)
        vdict['blog_title'] = 'ចុះ​ផ្សាយមេរៀន'
        vdict['datetime'] = self.lib.get_timezone()
        vdict['categories'] = self.category.select('all')

        if (request.method == "POST") and ('logged-in' in session):
            title = request.form['fpost-title']
            if not title:
                title = 'unknown'

            category = request.form['fcategory']
            content = request.form['fcontent']
            date = request.form['fpost-date']
            time = request.form['fpost-time']
            vidata = request.form['fvidata']
            author = session['logged-in']

            try:
                date = datetime.datetime.strptime(date, "%d/%m/%Y")
            except ValueError:
                vdict['message'] = 'ទំរង់​កាលបរិច្ឆេទ​មិន​ត្រឹមត្រូវ!'
                return render_template('dashboard/dashboard.html', data=vdict)

            try:
                time = datetime.datetime.strptime(time, "%H:%M:%S")
            except ValueError:
                vdict['message'] = 'ទំរង់​ពេល​វេលា​មិន​ត្រឹមត្រូវ!'
                return render_template('dashboard/dashboard.html', data=vdict)

            id = str(uuid.uuid4().int)
            if 'edit' in session:
                self.postdb.update(session['edit'], title, category, content,
                                   date, time, author, vidata)
                session.pop('edit', None)
            else:
                self.postdb.insert(id, title, category, content, date, time,
                                   author, vidata)

            vdict['posts'] = self.postdb.select(vdict['dashboard_max_post'])
            vdict['thumbs'] = self.lib.get_thumbs(vdict['posts'], 3)

            return render_template('dashboard/dashboard.html', data=vdict)

        elif 'logged-in' in session:
            if 'edit' in session:
                session.pop('edit', None)

            vdict['posts'] = self.postdb.select(vdict['dashboard_max_post'])
            vdict['thumbs'] = self.lib.get_thumbs(vdict['posts'], 3)
            return render_template('dashboard/dashboard.html', data=vdict)
        else:
            return redirect('/login/')

    def delete(self, id):
        if 'logged-in' in session:
            self.postdb.delete(id)
            return redirect('/dashboard/')

        return render_template('login.html', data=vdict)

    def edit(self, id):
        vdict = copy.deepcopy(config.vdict)
        vdict['blog_title'] = 'កែតំរូវ​មេរៀន'

        if 'logged-in' in session:
            vdict['posts'] = self.postdb.select(vdict['dashboard_max_post'])
            vdict['thumbs'] = self.lib.get_thumbs(vdict['posts'], 3)
            vdict['post'] = self.postdb.select(id=id)
            date = (vdict['post'][4]).strftime('%d/%m/%Y')
            time = (vdict['post'][5]).strftime('%H:%M:%S')
            vdict['datetime'] = (date, time)
            vdict['categories'] = self.category.select('all')

            return render_template('/dashboard/dashboard.html', data=vdict)

        return render_template('login.html', data=vdict)

    def load(self, page):
        if 'logged-in' in session:
            vdict = copy.deepcopy(config.vdict)
            vdict['posts'] = self.postdb.select(vdict['dashboard_max_post'],
                                                page=page)
            vdict['thumbs'] = self.lib.get_thumbs(vdict['posts'], 3)

            new_list = []
            for post in vdict['posts']:
                new_post = list(post)
                new_post[4] = post[4].strftime('%d/%m/%Y')
                new_post[5] = post[5].strftime('%H:%M:%S')
                new_list.append(new_post)

            vdict['posts'] = new_list
            return vdict
        else:
            return render_template('login.html', data=vdict)