Example #1
0
    def site_search(self, search_terms_enc, site):
        from core.models import Page, Site

        # ct = 0

        if site is not None:
            site_to_search = Site.load(site).pages.select(Page.id).tuples()

        try:
            search_results = (Page_Search.select(Page_Search.id).where(
                Page_Search.id << site_to_search,
                Page_Search.title.contains(search_terms_enc)
                | Page_Search.text.contains(search_terms_enc)).order_by(
                    Page_Search.id.desc()).tuples())
            search_results.count(
            )  # This statement is used to trap FTS4 errors
        except Exception:
            # pass
            # if ct == 0:
            search_results = (Page.select(Page.id).where(
                Page.blog.site == site,
                Page.title.contains(search_terms_enc)
                | Page.text.contains(search_terms_enc)).order_by(
                    Page.id.desc()).tuples())

        return search_results
Example #2
0
def blog_create(site_id):

    user = auth.is_logged_in(request)
    site = Site.load(site_id)
    permission = auth.is_site_admin(user, site)

    new_blog = Blog(
        name="",
        description="",
        url="",
        path="")

    tags = template_tags(site_id=site.id,
        user=user)

    tags.blog = new_blog
    from core.libs import pytz

    themes = Theme.select()

    return template('ui/ui_blog_settings',
        section_title="Create new blog",
        # search_context=(search_context['sites'], None),
        menu=generate_menu('site_create_blog', site),
        nav_default='all',
        timezones=pytz.all_timezones,
        themes=themes,
        ** tags.__dict__
        )
Example #3
0
def site_user(user_id, site_id):
    # Obtains user edit in site context.
    user = auth.is_logged_in(request)
    site = Site.load(site_id)
    permission = auth.is_site_admin(user, site)
    user_to_edit = User.find(user_id)

    return edit_user(user_to_edit, editing_user=user, context=site_context, site=site)
Example #4
0
def site_user(user_id, site_id):
    # Obtains user edit in site context.
    user = auth.is_logged_in(request)
    site = Site.load(site_id)
    permission = auth.is_site_admin(user, site)
    user_to_edit = User.find(user_id)

    return edit_user(user_to_edit,
                     editing_user=user,
                     context=site_context,
                     site=site)
Example #5
0
def site(site_id, errormsg=None):
    '''
    UI for listing contents of a given site
    '''
    user = auth.is_logged_in(request)
    site = Site.load(site_id)
    permission = auth.is_site_member(user, site)

    return listing(request, site, site.blogs.select(),
                   'site', 'site_menu',
                   user=user,
                   search_ui='site',
                   search_context=site_search_results,
                   errormsg=errormsg,
                   tags_data={'site':site}
                   )
Example #6
0
    def site_search(self, search_terms_enc, site):
        from core.models import Page, Site

        # ct = 0

        if site is not None:
            site_to_search = Site.load(site).pages.select(Page.id).tuples()

        try:
            search_results = (Page_Search.select(Page_Search.id)
                .where(Page_Search.id << site_to_search,
                    Page_Search.title.contains(search_terms_enc) | Page_Search.text.contains(search_terms_enc))
                .order_by(Page_Search.id.desc()).tuples())
            search_results.count()  # This statement is used to trap FTS4 errors
        except Exception:
            # pass
            # if ct == 0:
            search_results = (Page.select(Page.id)
                .where(Page.blog.site == site,
                    Page.title.contains(search_terms_enc) | Page.text.contains(search_terms_enc))
                .order_by(Page.id.desc()).tuples())

        return search_results
Example #7
0
def blog_create(site_id):

    user = auth.is_logged_in(request)
    site = Site.load(site_id)
    permission = auth.is_site_admin(user, site)

    new_blog = Blog(name="", description="", url="", path="")

    tags = template_tags(site_id=site.id, user=user)

    tags.blog = new_blog
    from core.libs import pytz

    themes = Theme.select()

    return template(
        'ui/ui_blog_settings',
        section_title="Create new blog",
        # search_context=(search_context['sites'], None),
        menu=generate_menu('site_create_blog', site),
        nav_default='all',
        timezones=pytz.all_timezones,
        themes=themes,
        **tags.__dict__)
Example #8
0
def user_edit(user_id, path, context, permission):
    # Obtains user edit in system context.
    user = auth.is_logged_in(request)
    permission = permission(user)
    user_to_edit = User.find(user_id=user_id) if user_id is not None else user

    status = None

    from core.error import PermissionsException

    if request.method == 'POST':

        if request.forms.getunicode('submit_settings') is not None:

            from core.libs import peewee

            user_to_edit.name = request.forms.getunicode('user_name')
            user_to_edit.email = request.forms.getunicode('user_email')

            try:
                user_to_edit.save()

            except peewee.IntegrityError:
                status = utils.Status(
                    type='danger',
                    no_sure=True,
                    message=
                    'Error: user <b>{}</b> cannot be changed to the same name or email as another user.'
                    .format(user_to_edit.for_display))
            else:
                status = utils.Status(
                    type='success',
                    message='Data for user <b>{}</b> successfully updated.'.
                    format(user_to_edit.for_display))

        # TODO: all actions could be consolidated w/o multiple status lines

        if request.forms.getunicode('delete_permissions') is not None:

            deletes = request.forms.getall('del')
            try:
                user.remove_permissions(deletes)
            except PermissionsException as e:
                raise e
            status = utils.Status(
                type='success',
                message='Data for user <b>{}</b> successfully updated.'.format(
                    user_to_edit.for_display))

        if request.forms.getunicode('submit_permissions') is not None:

            permission_to_add = int(
                request.forms.getunicode('permission_list'))
            permission_target = request.forms.getunicode(
                'permission_target_list')
            target_site = None
            target_blog = None
            if permission_to_add != auth.role.SYS_ADMIN:
                permission_target_item = permission_target[:5]
                if permission_target_item == 'site-':
                    target_site = Site.load(permission_target[5:])
                else:
                    target_blog = Blog.load(permission_target[5:])

            user_to_edit.add_permission(permission=permission_to_add,
                                        site=target_site,
                                        blog=target_blog)
            '''
            what we should do:
            - get any existing permission
            - update it with the proper bitmask
            then, when listing permissions,
            go through and compare each bitmask against it
            the bitmask needs to be all in one entry per site/blog/user object
            it *might* work as we have it now but we'll need to test
            we might need to order by level to make sure it works
            '''
    else:
        if user_to_edit.last_login is None:
            status = utils.Status(
                type='success',
                message='User <b>{}</b> successfully created.'.format(
                    user_to_edit.for_display),
            )
            import datetime
            user_to_edit.last_login = datetime.datetime.utcnow()
            user_to_edit.save()

    tags = template_tags(user=User.find(user_id=user.id))
    tags.status = status
    try:
        tags.permissions = auth.get_permissions(user_to_edit)
    except PermissionsException:
        tags.permissions = []
    tags.editor_permissions = auth.get_permissions(user)
    return edit_user(user_to_edit,
                     editing_user=user,
                     context=context(user_to_edit, path),
                     tags=tags)
Example #9
0
def user_edit(user_id, path, context, permission):
    # Obtains user edit in system context.
    user = auth.is_logged_in(request)
    permission = permission(user)
    user_to_edit = User.find(user_id=user_id) if user_id is not None else user

    status = None

    from core.error import PermissionsException

    if request.method == 'POST':

        if request.forms.getunicode('submit_settings') is not None:

            from core.libs import peewee

            user_to_edit.name = request.forms.getunicode('user_name')
            user_to_edit.email = request.forms.getunicode('user_email')

            try:
                user_to_edit.save()

            except peewee.IntegrityError:
                status = utils.Status(
                    type='danger',
                    no_sure=True,
                    message='Error: user <b>{}</b> cannot be changed to the same name or email as another user.'.format(
                        user_to_edit.for_display)
                    )
            else:
                status = utils.Status(
                    type='success',
                    message='Data for user <b>{}</b> successfully updated.'.format(
                        user_to_edit.for_display)
                    )

        # TODO: all actions could be consolidated w/o multiple status lines

        if request.forms.getunicode('delete_permissions') is not None:

            deletes = request.forms.getall('del')
            try:
                user.remove_permissions(deletes)
            except PermissionsException as e:
                raise e
            status = utils.Status(
                type='success',
                message='Data for user <b>{}</b> successfully updated.'.format(user_to_edit.for_display)
                )

        if request.forms.getunicode('submit_permissions') is not None:

            permission_to_add = int(request.forms.getunicode('permission_list'))
            permission_target = request.forms.getunicode('permission_target_list')
            target_site = None
            target_blog = None
            if permission_to_add != auth.role.SYS_ADMIN:
                permission_target_item = permission_target[:5]
                if permission_target_item == 'site-':
                    target_site = Site.load(permission_target[5:])
                else:
                    target_blog = Blog.load(permission_target[5:])


            user_to_edit.add_permission(
                permission=permission_to_add,
                site=target_site,
                blog=target_blog)

            '''
            what we should do:
            - get any existing permission
            - update it with the proper bitmask
            then, when listing permissions,
            go through and compare each bitmask against it
            the bitmask needs to be all in one entry per site/blog/user object
            it *might* work as we have it now but we'll need to test
            we might need to order by level to make sure it works
            '''
    else:
        if user_to_edit.last_login is None:
            status = utils.Status(
                type='success',
                message='User <b>{}</b> successfully created.'.format(
                    user_to_edit.for_display),
                )
            import datetime
            user_to_edit.last_login = datetime.datetime.utcnow()
            user_to_edit.save()

    tags = template_tags(user=User.find(user_id=user.id))
    tags.status = status
    try:
        tags.permissions = auth.get_permissions(user_to_edit)
    except PermissionsException:
        tags.permissions = []
    tags.editor_permissions = auth.get_permissions(user)
    return edit_user(user_to_edit, editing_user=user,
        context=context(user_to_edit, path),
        tags=tags)
Example #10
0
def blog_create_save(site_id):

    user = auth.is_logged_in(request)
    site = Site.load(site_id)
    permission = auth.is_site_admin(user, site)

    errors = []

    new_blog = Blog(
        site=site,
        name=request.forms.getunicode('blog_name'),
        description=request.forms.getunicode('blog_description'),
        url=request.forms.getunicode('blog_url'),
        path=request.forms.getunicode('blog_path'),
        set_timezone=request.forms.getunicode('blog_timezone'),
        # theme=get_default_theme(),
        theme=Theme.default_theme())

    try:
        new_blog.validate()
    except Exception as e:
        errors.extend(e.args[0])

    if len(errors) == 0:
        from core.libs.peewee import IntegrityError
        try:
            new_blog.setup(user, Theme.default_theme())
            # new_blog.theme)
        except IntegrityError as e:
            from core.utils import field_error
            errors.append(field_error(e))

    if len(errors) > 0:

        status = utils.Status(
            type='danger',
            no_sure=True,
            message=
            'The blog could not be created due to the following problems:',
            message_list=errors)
        from core.libs import pytz
        tags = template_tags(site=site, user=user)
        tags.status = status
        tags.blog = new_blog
        themes = Theme.select()

        return template(
            'ui/ui_blog_settings',
            section_title="Create new blog",
            # search_context=(search_context['sites'], None),
            menu=generate_menu('site_create_blog', site),
            nav_default='all',
            themes=themes,
            timezones=pytz.all_timezones,
            **tags.__dict__)

    else:
        tags = template_tags(user=user, site=site, blog=new_blog)

        status = utils.Status(type='success',
                              message='''
Blog <b>{}</b> was successfully created. You can <a href="{}/blog/{}/newpage">start posting</a> immediately.
'''.format(new_blog.for_display, BASE_URL, new_blog.id))
        tags.status = status

        return report(tags, 'site_create_blog', site)
Example #11
0
def blog_create_save(site_id):

    user = auth.is_logged_in(request)
    site = Site.load(site_id)
    permission = auth.is_site_admin(user, site)

    errors = []

    new_blog = Blog(
            site=site,
            name=request.forms.getunicode('blog_name'),
            description=request.forms.getunicode('blog_description'),
            url=request.forms.getunicode('blog_url'),
            path=request.forms.getunicode('blog_path'),
            set_timezone=request.forms.getunicode('blog_timezone'),
            # theme=get_default_theme(),
            theme=Theme.default_theme()
            )

    try:
        new_blog.validate()
    except Exception as e:
        errors.extend(e.args[0])

    if len(errors) == 0:
        from core.libs.peewee import IntegrityError
        try:
            new_blog.setup(user, Theme.default_theme())
                # new_blog.theme)
        except IntegrityError as e:
            from core.utils import field_error
            errors.append(field_error(e))

    if len(errors) > 0:

        status = utils.Status(
            type='danger',
            no_sure=True,
            message='The blog could not be created due to the following problems:',
            message_list=errors)
        from core.libs import pytz
        tags = template_tags(site=site,
            user=user)
        tags.status = status
        tags.blog = new_blog
        themes = Theme.select()

        return template('ui/ui_blog_settings',
            section_title="Create new blog",
            # search_context=(search_context['sites'], None),
            menu=generate_menu('site_create_blog', site),
            nav_default='all',
            themes=themes,
            timezones=pytz.all_timezones,
            ** tags.__dict__
            )

    else:
        tags = template_tags(user=user, site=site,
            blog=new_blog)

        status = utils.Status(
            type='success',
            message='''
Blog <b>{}</b> was successfully created. You can <a href="{}/blog/{}/newpage">start posting</a> immediately.
'''.format(
                new_blog.for_display,
                BASE_URL, new_blog.id)
            )
        tags.status = status

        return report(tags, 'site_create_blog', site)