Exemplo n.º 1
0
class CategoryThing(object):
    def __init__(self):
        self.category_repo = Backend(Category.__name__)
        self.post_repo = Backend('Post')

    def get_by_cid(self, category_id):
        return self.category_repo.find(category_id)

    def dropdown(self):
        return self.category_repo.dropdown()

    def page(self, page=1, perpage=10):
        total = self.category_repo.count()
        pages = self.category_repo.paginate(page, perpage)
        pagination = Paginator(pages, total, page, perpage, '/admin/category')
        return pagination

    def add_category(self, title, slug, description):
        category = Category(title, slug, description)
        cid = self.category_repo.create(category)
        category.cid = cid
        return category

    def update_category(self, category_id, title, slug, description):
        slug = slug or title
        category = Category(title, slug, description, category_id)
        self.category_repo.save(category)
        return category

    def delete(self, category_id):
        if category_id == 1:
            return
        category = self.category_repo.find(category_id)
        if category and self.category_repo.delete(category_id):
            self.post_repo.reset_post_category(category_id)
Exemplo n.º 2
0
class ExtendThing(object):

    def __init__(self):
        self.extend_repo = Backend('Extend')
        self.meta_repo = Backend('Meta')

    def get_by_eid(self, extend_id):
        return self.extend_repo.find(extend_id)

    def field_page(self, page=1, perpage=10, url= '/admin/extend/field'):
        total = self.extend_repo.count()
        pages = self.extend_repo.paginate(page, perpage, url)
        pagination = Paginator(
            pages, total, page, perpage,)
        return pagination

    def get_fields_by_type(self, type='post', node_id=None):
        extends = self.extend_repo.find_by_type(type)
        if node_id is None:
            load_meta = lambda extend: Meta(0, type, extend)
        else:
            load_meta = lambda extend: self.meta_repo.find(
                type, node_id, extend.eid) or Meta(0, type, extend)

        return [Field(extend, load_meta(extend)) for extend in extends]

    def count(self, key, type):
        return self.extend_repo.count(key=key, type=type)

    def create_extend(self, type, key, label, field, attributes):
        extend = Extend(type, key, label, field, attributes)
        self.extend_repo.create(extend)
        return extend

    def update_extend(self, type, key, label, field, attributes, extend_id):
        extend = self.get_by_eid(extend_id)
        if not extend:
            return None
        extend.attributes = attributes
        extend.label = label
        self.extend_repo.save(extend)
        return extend

    def delete_extend(self, extend_id):
        field = self.extend_repo.find(extend_id)
        if not field:
            return None
        return self.extend_repo.delete(field)

    def prcoess_field(self, handler, node, type='post'):
        FieldMananger(handler, node, type).process()
Exemplo n.º 3
0
class PageThing(object):

    def __init__(self):
        self.page_repo = Backend(Page.__name__)

    get = lambda self, pid: self.page_repo.find(pid)

    def get_by_redirect(self, redirect):
        return self.page_repo.find_by_redirect(redirect)

    def get_by_slug(self, slug):
        return self.page_repo.find_by_slug(slug)

    def dropdown(self, show_in_menu=True):
        return self.page_repo.dropdown(show_in_menu)

    def page(self, status, page=1, perpage=10):
        total = self.page_repo.count(status)
        pages = self.page_repo.paginate(page, perpage, status)
        if status:
            url = '/admin/page/status/' + status
        else:
            url = '/admin/page'
        pagination = Paginator(pages, total, page, perpage, url)
        return pagination

    def delete(self, page_id):
        page = self.page_repo.find(page_id)
        if not page:
            return None
        return self.page_repo.delete(page.pid)

    def add_page(self, parent, name, title, slug, content, status, redirect, show_in_menu):
        redirect = redirect.strip()
        show_in_menu = 1 if show_in_menu else 0
        page = Page(parent, name, title, slug, content, status, redirect, show_in_menu)
        pid = self.page_repo.create(page)
        page.pid = pid
        return page

    def is_exist_slug(self, slug):
        return self.page_repo.count_slug(slug) == 1

    def update_page(self, parent, name, title, slug, content, status, redirect, show_in_menu, pid):
        show_in_menu = 1 if show_in_menu else 0
        redirect = redirect.strip()
        page = Page(parent, name, title, slug, content, status, redirect, show_in_menu, pid)
        self.page_repo.save(page)
        return page
Exemplo n.º 4
0
class CategoryThing(object):

    def __init__(self):
        self.category_repo = Backend(Category.__name__)
        self.post_repo = Backend('Post')

    def get_by_cid(self, category_id):
        return self.category_repo.find(category_id)

    def dropdown(self):
        return self.category_repo.dropdown()

    def page(self, page=1, perpage=10):
        total = self.category_repo.count()
        pages = self.category_repo.paginate(page, perpage)
        pagination = Paginator(pages, total, page, perpage, '/admin/category')
        return pagination

    def add_category(self, title, slug, description):
        category = Category(title, slug, description)
        cid = self.category_repo.create(category)
        category.cid = cid
        return category

    def update_category(self, category_id, title, slug, description):
        slug = slug or title
        category = Category(title, slug, description, category_id)
        self.category_repo.save(category)
        return category

    def delete(self, category_id):
        if category_id == 1:
            return
        category = self.category_repo.find(category_id)
        if category and self.category_repo.delete(category_id):
            self.post_repo.reset_post_category(category_id)
Exemplo n.º 5
0
class UserThing(object):

    def __init__(self):
        self.repo = Backend(User.__name__)

    get = lambda self, uid: self.repo.find(uid)

    def auth(self, username, password):
        user = self.repo.find_by_username(username)
        if not user:
            return {'status': 404, 'msg': 'not found'}

        if user and not user.inactive() and user.check(password):
            return {'status': 200, 'msg': 'auth success', 'user': user}
        return {'status': 403, 'msg': 'username or password is invaild'}

    def page(self, page, perpage=5):
        total = self.repo.count()
        users = self.repo.take(page, perpage)
        page = Paginator(users, total, page, perpage, '/admin/user')
        return page


    def get_user_page(self, user):
        return Paginator([user], 1, 1, 5, '/admin/user')

    def user_count(self):
        return self.repo.count()

    def check_email(self, email):
        return email_validator(email)

    def add_user(self, username, email, real_name, password, bio, status='', role='user'):
        username, real_name = username.strip(), real_name.strip()
        errors = []
        if not re.match(r'^[A-Za-z0-9_]{4,16}$', username):
            errors.append(text('user.username_missing'))

        if not re.match(r'^[A-Za-z0-9_]{4,16}$', password):
            errors.append(text('user.password_invalid'))

        if not self.check_email(email):
            errors.append(text('user.email_missing'))

        if errors:
            return {'status': 'error', 'errors': errors}

        if status not in Account.STATUSES:
            status = 'inactive'

        if role not in Account.ROLES:
            role = 'user'

        if self.repo.find_by_username(username):
            errors.append(text('user.username_used'))

        if errors:
            return {'status': 'error', 'errors': errors}

        user = User(username, email, real_name, password, None, bio, status, role)
        user.uid = self.repo.create(user)
        return {'status': 'ok', 'msg': 'saved', 'user': user}

    def update_user(self, me, uid, email, real_name, password, newpass1, newpass2, bio, status, role='user'):
        real_name, newpass1, newpass2, bio = real_name.strip(), newpass1.strip(), newpass2.strip(), bio.strip()
        errors = []

        if not self.check_email(email):
            errors.append(text('user.email_missing'))

        if errors:
            return {'status': 'error', 'errors': errors}

        user = self.repo.find(uid)
        if not user:
            return {'status': 'error', 'errors': 'User not Found'}

        if me.uid == user.uid:
            if re.match(r'[A-Za-z0-9@#$%^&+=]{4,16}', newpass1):
                if password and newpass1 and newpass1 == newpass2 and user.check(password):
                    user.secure_pass = newpass1
            elif newpass1:
                errors.append(text('users.password_missing'))

            if self.check_email(email):
                user_ = self.repo.find_by_email(email)
                if user_ and user_.uid != user.uid:
                    errors.append(text('user.email_used'))
                else:
                    user.email = email

        if errors:
            return {'status': 'error', 'errors': errors}

        account = Account.fromUser(user)
        if me.is_root() or me.uid == uid:
            if me.is_root() and not account.is_root():
                if role in (Account.ADMIN, Account.USER, Account.EDITOR):
                    user.role = role
                if user.status != status and status in Account.STATUSES:
                    user.status = status

            if user.real_name != real_name:
                user.real_name = real_name

            if user.bio != bio:
                user.bio = bio

        self.repo.save(user)
        return {'status': 'ok', 'msg': 'updated', 'user': user}


    def delete(self, me, user_id):
        user = self.repo.find(user_id)

        if not user:
            return
        account = Account.fromUser(user)
        if account.is_root():
            return 
        if me.is_root():
            return self.repo.delete(user)
Exemplo n.º 6
0
class UserThing(object):
    def __init__(self):
        self.repo = Backend(User.__name__)

    get = lambda self, uid: self.repo.find(uid)

    def auth(self, username, password):
        user = self.repo.find_by_username(username)
        if not user:
            return {'status': 404, 'msg': 'not found'}

        if user and not user.inactive() and user.check(password):
            return {'status': 200, 'msg': 'auth success', 'user': user}
        return {'status': 403, 'msg': 'username or password is invaild'}

    def page(self, page, perpage=5):
        total = self.repo.count()
        users = self.repo.take(page, perpage)
        page = Paginator(users, total, page, perpage, '/admin/user')
        return page

    def get_user_page(self, user):
        return Paginator([user], 1, 1, 5, '/admin/user')

    def user_count(self):
        return self.repo.count()

    def check_email(self, email):
        return email_validator(email)

    def add_user(self,
                 username,
                 email,
                 real_name,
                 password,
                 bio,
                 status='',
                 role='user'):
        username, real_name = username.strip(), real_name.strip()
        errors = []
        if not re.match(r'^[A-Za-z0-9_]{4,16}$', username):
            errors.append(text('user.username_missing'))

        if not re.match(r'^[A-Za-z0-9_]{4,16}$', password):
            errors.append(text('user.password_invalid'))

        if not self.check_email(email):
            errors.append(text('user.email_missing'))

        if errors:
            return {'status': 'error', 'errors': errors}

        if status not in Account.STATUSES:
            status = 'inactive'

        if role not in Account.ROLES:
            role = 'user'

        if self.repo.find_by_username(username):
            errors.append(text('user.username_used'))

        if errors:
            return {'status': 'error', 'errors': errors}

        user = User(username, email, real_name, password, None, bio, status,
                    role)
        user.uid = self.repo.create(user)
        return {'status': 'ok', 'msg': 'saved', 'user': user}

    def update_user(self,
                    me,
                    uid,
                    email,
                    real_name,
                    password,
                    newpass1,
                    newpass2,
                    bio,
                    status,
                    role='user'):
        real_name, newpass1, newpass2, bio = real_name.strip(), newpass1.strip(
        ), newpass2.strip(), bio.strip()
        errors = []

        if not self.check_email(email):
            errors.append(text('user.email_missing'))

        if errors:
            return {'status': 'error', 'errors': errors}

        user = self.repo.find(uid)
        if not user:
            return {'status': 'error', 'errors': 'User not Found'}

        if me.uid == user.uid:
            if re.match(r'[A-Za-z0-9@#$%^&+=]{4,16}', newpass1):
                if password and newpass1 and newpass1 == newpass2 and user.check(
                        password):
                    user.secure_pass = newpass1
            elif newpass1:
                errors.append(text('users.password_missing'))

            if self.check_email(email):
                user_ = self.repo.find_by_email(email)
                if user_ and user_.uid != user.uid:
                    errors.append(text('user.email_used'))
                else:
                    user.email = email

        if errors:
            return {'status': 'error', 'errors': errors}

        account = Account.fromUser(user)
        if me.is_root() or me.uid == uid:
            if me.is_root() and not account.is_root():
                if role in (Account.ADMIN, Account.USER, Account.EDITOR):
                    user.role = role
                if user.status != status and status in Account.STATUSES:
                    user.status = status

            if user.real_name != real_name:
                user.real_name = real_name

            if user.bio != bio:
                user.bio = bio

        self.repo.save(user)
        return {'status': 'ok', 'msg': 'updated', 'user': user}

    def delete(self, me, user_id):
        user = self.repo.find(user_id)

        if not user:
            return
        account = Account.fromUser(user)
        if account.is_root():
            return
        if me.is_root():
            return self.repo.delete(user)