class Post(Base): def start(self): self.app = MyApp() self.auth = self.app.auth # you need to access this to define users self.db = self.app.db([PostModel]) # this is needed to inject auth in template render # only needed to use auth.navbar() self.context.auth = self.auth def list_all(self): self.context.posts = self.db(self.db.Post).select(orderby=~self.db.Post.created_on) def create_new(self): # permission is checked here if self.auth.has_membership("author", self.auth.user_id): self.db.Post.author.default = self.auth.user_id self.context.form = SQLFORM(self.db.Post, formstyle='divs').process(onsuccess=lambda form: redirect(URL('show', args=form.vars.id))) else: self.context.form = "You can't post, only logged in users, members of 'author' group can post" def edit_post(self, post_id): post = self.db.Post[post_id] # permission is checked here if not post or post.author != self.auth.user_id: redirect(URL("post", "index")) self.context.form = SQLFORM(self.db.Post, post.id, formstyle='divs').process(onsuccess=lambda form: redirect(URL('show', args=form.vars.id))) def show(self, post_id): self.context.post = self.db.Post[post_id] if not self.context.post: redirect(URL("post", "index"))
def define_tables(self, migrate=True, fake_migrate=False): """ Called after settings are set to create the required tables for dealing with file uploads from CKEditor. """ from myapp import MyApp app = MyApp() auth = app.auth from datamodel.post import Post as PostModel self.db = app.db([PostModel]) upload_name = self.settings.table_upload_name self.settings.table_upload = self.db.define_table( upload_name, Field('title', length=255), Field('filename', length=255), Field('flength', 'integer'), Field('mime_type', length=128), Field('upload', 'upload'), Field('user_id', 'integer', default=app.session.auth.user.id if app.session.auth else 0), Field("created_on", "datetime", default=app.request.now), *self.settings.extra_fields.get(upload_name, []), **dict(migrate=migrate, fake_migrate=fake_migrate, format='%(title)s')) self.settings.table_upload.upload.requires = [ IS_NOT_EMPTY(), IS_LENGTH(maxsize=self.settings.file_length_max, minsize=self.settings.file_length_min), ]
def define_tables(self, migrate=True, fake_migrate=False): """ Called after settings are set to create the required tables for dealing with file uploads from CKEditor. """ from myapp import MyApp app = MyApp() auth = app.auth from datamodel.post import Post as PostModel self.db = app.db([PostModel]) upload_name = self.settings.table_upload_name self.settings.table_upload = self.db.define_table(upload_name, Field('title', length=255), Field('filename', length=255), Field('flength', 'integer'), Field('mime_type', length=128), Field('upload', 'upload'), Field('user_id', 'integer', default=app.session.auth.user.id if app.session.auth else 0), Field("created_on", "datetime", default=app.request.now), *self.settings.extra_fields.get(upload_name, []), **dict(migrate=migrate, fake_migrate=fake_migrate, format='%(title)s') ) self.settings.table_upload.upload.requires = [ IS_NOT_EMPTY(), IS_LENGTH(maxsize=self.settings.file_length_max, minsize=self.settings.file_length_min), ]
# -*- coding: utf-8 -*- # ########################################################## # ## make sure administrator is on localhost # ########################################################### ############################################################## from myapp import MyApp app = MyApp() auth = app.auth from datamodel.post import Post as PostModel db = app.db([PostModel]) ############################################################## import os import socket import datetime import copy import gluon.contenttype import gluon.fileutils # ## critical --- make a copy of the environment global_env = copy.copy(globals()) global_env['datetime'] = datetime http_host = request.env.http_host.split(':')[0] remote_addr = request.env.remote_addr try: hosts = (http_host, socket.gethostname(), socket.gethostbyname(http_host), '::1', '127.0.0.1', '::ffff:127.0.0.1')
# -*- coding: utf-8 -*- # ########################################################## # ## make sure administrator is on localhost # ########################################################### ############################################################## from myapp import MyApp app = MyApp() auth = app.auth from datamodel.post import Post as PostModel db = app.db([PostModel]) ############################################################## import os import socket import datetime import copy import gluon.contenttype import gluon.fileutils # ## critical --- make a copy of the environment global_env = copy.copy(globals()) global_env['datetime'] = datetime http_host = request.env.http_host.split(':')[0] remote_addr = request.env.remote_addr try: hosts = (http_host, socket.gethostname(), socket.gethostbyname(http_host),