Exemplo n.º 1
0
    def test_post_bad_site(self):
        self.client.login(username='******', password='******')
        url = '/datasheet/bulk_import/'
        with open(self.fpath_bad_site) as f:
            response = self.client.post(url, {
                'datasheet_id': self.ds.pk,
                'organization': 'Coast Savers', 
                'project_id': 1, 
                'csvfile': f
                }
            )
        d = pq(response.content)
        el = d("ul.errorlist li")
        self.assertEqual(response.status_code, 400, response.content)
        self.assertEqual(len(el), 1)
        self.assertTrue("TestSite3" in el[0].text_content(), el[0].text_content())
        self.assertTrue("is not in the database" in el[0].text_content(), el[0].text_content())

        # Now add the site
        ca = State.objects.get(name="California")
        testsite3 = Site(sitename="TestSite3", state=ca, county="Santa Cruz")
        testsite3.save()
        with open(self.fpath_bad_site) as f:
            response = self.client.post(url, {
                'datasheet_id': self.ds.pk,
                'organization': 'Coast Savers', 
                'project_id': 1, 
                'csvfile': f
                }
            )
        d = pq(response.content)
        el = d("ul.errorlist li")
        self.assertEqual(response.status_code, 200, response.content)
        self.assertEqual(len(el), 0)
Exemplo n.º 2
0
 def test_presave(self):
     pnt = GEOSGeometry('SRID=4326;POINT(-124.014723 45.045150)')
     site = Site(sitename="TestSite3", geometry=pnt)
     self.assertEqual(site.state, None)
     self.assertEqual(site.county, None)
     site.save()
     self.assertEqual(site.state.initials, "OR")
     self.assertEqual(site.county, "Tillamook")
Exemplo n.º 3
0
def add_site(auth, fields):
    user = authenticate(username=auth.get('username'),
                        password=auth.get('password'))
    auth['tenant'] = user.site.login_base

    site = Site(**fields)
    site.os_manager = OpenStackManager(auth=auth, caller=user)
    site.save()
    return site
Exemplo n.º 4
0
def add_site(auth, fields):
    user = authenticate(username=auth.get('username'),
                        password=auth.get('password'))
    auth['tenant'] = user.site.login_base

    site = Site(**fields)
    site.os_manager = OpenStackManager(auth=auth, caller = user)
    site.save()
    return site
Exemplo n.º 5
0
    def create(self):
        nodetemplate = self.nodetemplate
        siteName = nodetemplate.name

        xos_args = self.get_xos_args()

        site = Site(**xos_args)
        site.caller = self.user
        site.save()

        self.postprocess(site)

        self.info("Created Site '%s'" % (str(site), ))
Exemplo n.º 6
0
    def create(self):
        nodetemplate = self.nodetemplate
        siteName = nodetemplate.name

        xos_args = self.get_xos_args()

        site = Site(**xos_args)
        site.caller = self.user
        site.save()

        self.postprocess(site)

        self.info("Created Site '%s'" % (str(site), ))
Exemplo n.º 7
0
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'site':
            kwargs['queryset'] = Site.select_by_user(request.user)

        if db_field.name == 'user':
            kwargs['queryset'] = User.select_by_user(request.user)
        return super(SitePrivilegeInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
Exemplo n.º 8
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
Exemplo n.º 9
0
def logout(request):
    """Log out from this site and the central site"""
    if request.site.is_central():
        # Central site
        if 'sso' not in request.GET:
            # Simply logging out from the central site - no SSO action required
            log_user_out(request)
            return redirect('cms:page')
        else:
            # This is an SSO logout request from a non-central site; log out here first and send them back
            log_user_out(request)
            return redirect(request.GET.get('next', reverse('cms:page')))
    else:
        # Non-central site
        if 'sso' not in request.GET:
            # Logging out of a non-central site - send a logout request to the central site first, then return here.
            # Add 'sso' parameter to the central site logout request to signal that this is an SSO logout.
            # Also add 'sso' parameter in the return address to signal that SSO is complete.
            return_address = 'https://%s%s?sso' % (request.site.domain, reverse('user:login.logout'))
            return redirect('https://%s%s?sso&next=%s' % (
                Site.get_central().domain,
                reverse('user:login.logout'),
                urlquote(return_address),
            ))
        else:
            # Single signout is completed; now log out here
            log_user_out(request)
            return redirect('cms:page')
Exemplo n.º 10
0
    def process_request(self, request):
        domain = request.get_host().lower().split(":")[0]
        status = self.lookup_site(request, domain)

        # Site found
        if status:
            return

        # If in domain-test mode
        if settings.SITE_DOMAIN_TEST_SUFFIX is not None:
            suffix = settings.SITE_DOMAIN_TEST_SUFFIX
            test_domain = domain.replace(suffix, '')
            test_domain = test_domain.replace('--', '.')
            status = self.lookup_site(request, test_domain)

            # Site found
            if status:
                return

        # Unknown host name, redirect to the main site
        central_domain = Site.get_central().domain
        if settings.SITE_DOMAIN_TEST_SUFFIX is not None:
            central_domain = "%s%s" % (
                central_domain.replace('.', '--'),
                settings.SITE_DOMAIN_TEST_SUFFIX,
            )
        return redirect('//%s/' % central_domain)
Exemplo n.º 11
0
def system_sites(errormsg=None):
    user = auth.is_logged_in(request)
    permission = auth.is_sys_admin(user)

    return listing(request, None, Site.select(),
                   'all_sites', 'manage_sites',
                   user=user)
Exemplo n.º 12
0
def analytics_ua(request, site):
    active_site = Site.get_cached_by_id(site)

    if 'analytics-ua' in request.POST:
        ua = request.POST['analytics-ua'].strip()
        if ua == '':
            ua = None

        active_site.analytics_ua = ua

    if 'gtm-id' in request.POST:
        gtm = request.POST['gtm-id'].strip()
        if gtm == '':
            gtm = None

        active_site.gtm_id = gtm

    if 'gsv-id' in request.POST:
        gsv = request.POST['gsv-id'].strip()
        if gsv == '':
            gsv = None

        active_site.gsv_id = gsv

    active_site.save()
    return redirect('admin:sites.settings.analytics.index', active_site.id)
Exemplo n.º 13
0
def list(request, site):
    active_site = Site.get_cached_by_id(site)
    context = {
        'active_site': active_site,
        'category_choices': Article.CATEGORY_CHOICES,
    }
    return render(request, 'central/admin/sites/articles/list.html', context)
Exemplo n.º 14
0
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'site':
            kwargs['queryset'] = Site.select_by_user(request.user)

        if db_field.name == 'deployment':
            kwargs['queryset'] = Deployment.select_by_user(request.user)
        return super(SiteDeploymentInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
Exemplo n.º 15
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__
        )
Exemplo n.º 16
0
def update_ad(request, site):
    active_site = Site.get_cached_by_id(site)
    try:
        ad = Ad.objects.get(id=request.POST['id'], site=active_site)
        ad.name = request.POST['name']
        ad.format = request.POST['format']
        ad.viewcounter = request.POST['viewcounter']

        if not ad.is_adform_script():
            ad.destination = request.POST['destination'].strip()
            if 'ad' in request.FILES:
                ad.delete_file()
                ad.sha1_hash, ad.extension, ad.content_type = upload(request.FILES['ad'])
                if not validate_size(request.FILES['ad'], request.POST['format']):
                    messages.error(request, 'invalid_size')
            if 'ad_fallback' in request.FILES:
                ad.delete_fallback_file()
                ad.fallback_sha1_hash, ad.fallback_extension, ad.fallback_content_type = upload(request.FILES['ad_fallback'])
                if not validate_size(request.FILES['ad_fallback'], request.POST['format']):
                    messages.error(request, 'invalid_size')
        else:
            script = request.POST['script'].strip()
            ad.destination = parse_adform_script_destination(script)
            ad.content_script = script

        ad.save()
    except IndexError:
        messages.error(request, 'unparseable_script')

    return redirect('admin:sites.ads.list', active_site.id)
Exemplo n.º 17
0
def save_menu(request, site):
    active_site = Site.get_cached_by_id(site)
    menus = json.loads(request.POST['menus'])

    # Empty string should be prevented client-side; enforce it here because it would be impossible to edit that item
    if any([m['name'].strip() == '' for m in menus]):
        raise PermissionDenied

    # Delete all existing menus
    Menu.objects.filter(site=active_site).delete()

    # Recreate the new menu set
    for i, menu in enumerate(menus):
        menu = Menu(
            name=menu['name'],
            url=menu['url'],
            order=i,
            site=active_site,
        )
        menu.save()

    # Reset the cache with the new query set
    cache.set('main.menu.%s' % active_site.id, Menu.objects.filter(site=active_site).order_by('order'), 60 * 60 * 24)

    # An empty http response will be considered success
    return HttpResponse()
Exemplo n.º 18
0
    def system_site_index():
        from core.models import Site
        sites = Site.select()

        tpl = '''
<p>This is the local web server for an installation of {}.
<p><a href='{}'>Open the site dashboard</a>
<hr/>
<p>You can also preview sites and blogs available on this server:
<ul>'''.format(PRODUCT_NAME, BASE_PATH) + '''
% for site in sites:
    <li>{{site.name}}</li>
    % if site.blogs.count()>0:
        <ul>
        % for blog in site.blogs:
        % if blog.published_pages().count()>0:
            <li><a href="/?_={{blog.id}}">{{blog.name}}</a></li>
        % else:
        <li>{{blog.name}} [No published pages on this blog]</li>
        % end
        % end
        </ul>
    % end
%end
</ul><hr/>
'''
        return template(tpl, sites=sites)
Exemplo n.º 19
0
def save_redirect(request, site):
    active_site = Site.get_cached_by_id(site)

    if request.method != 'POST':
        return redirect('admin:sites.navigation.index', active_site.id)

    existing_redirect = request.POST['existing-redirect'].strip()
    if existing_redirect != '':
        site_redirect = Redirect.objects.get(
            id=existing_redirect,
            site=active_site,
        )
        if request.POST['delete'].strip() == '':
            # Editing
            site_redirect.path = request.POST['path'].strip()
            site_redirect.destination = request.POST['destination'].strip()
            site_redirect.wildcard = request.POST.get('wildcard', '') == 'on'
            site_redirect.save()
        else:
            # Deleting
            site_redirect.delete()
    else:
        # Creating new
        site_redirect = Redirect(
            site=active_site,
            path=request.POST['path'].strip(),
            destination=request.POST['destination'].strip(),
        )
        site_redirect.save()

    return redirect('admin:sites.navigation.index', active_site.id)
Exemplo n.º 20
0
def save(request, site):
    if request.method != 'POST':
        return redirect('admin.sites.settings.views.index', site)

    active_site = Site.objects.get(id=site)
    form = SiteForm(request.user, request.POST, auto_id='%s')

    if not form.is_valid():
        request.session['form_post_data'] = request.POST
        return redirect('admin.sites.settings.views.index', site)

    site_forening = form.cleaned_data['forening']
    type = form.cleaned_data['type']
    title = form.cleaned_data['title']
    template_main = form.cleaned_data['template_main']
    template_type = form.cleaned_data['template_type']
    template_description = form.cleaned_data['template_description']

    domain = request.POST['domain'].strip().lower().replace('http://', '').rstrip('/')
    errors = False

    active_site.forening = site_forening
    active_site.type = type
    active_site.title = title
    active_site.template_main = template_main
    active_site.template_type = template_type
    active_site.template_description = template_description

    if domain == active_site.domain:
        # Special case; the domain wasn't changed - so just pretend that it's updated
        pass
    else:
        result = Site.verify_domain(domain)
        if not result['valid']:
            messages.error(request, result['error'])
            errors = True
            if result['error'] == 'site_exists':
                request.session['message_context'] = {'existing_forening': result['existing_forening']}
        else:
            active_site.domain = result['domain']
            active_site.prefix = result['prefix']

    active_site.is_published = 'published' in request.POST
    active_site.save()

    # If this is a main template, clear other templates of this type in case any of them were previous main
    if active_site.type == 'mal' and active_site.template_main:
        Site.objects.filter(
            type=active_site.type,
            template_type=active_site.template_type,
        ).exclude(
            id=active_site.id,
        ).update(
            template_main=False
        )

    request.session.modified = True
    if not errors:
        messages.info(request, 'settings_saved')
    return redirect('admin.sites.settings.views.index', active_site.id)
Exemplo n.º 21
0
def new(request, site):
    if request.method != 'POST':
        return redirect('admin:sites.articles.list', site)

    active_site = Site.get_cached_by_id(site)
    category = request.POST['category']
    if category not in [c[0] for c in Article.CATEGORY_CHOICES]:
        raise PermissionDenied

    article = Article(
        category=category,
        thumbnail=None,
        hide_thumbnail=False,
        published=False,
        pub_date=None,
        unpub_date=None,
        created_by=request.user,
        site=active_site,
    )
    article.save()
    version = Version(page=None, article=article, version=1, owner=request.user, active=True)
    version.save()
    version.publishers.add(request.user)
    create_template(request.POST['template'], version, request.POST['title'])
    return redirect('admin:sites.articles.edit', active_site.id, version.id)
Exemplo n.º 22
0
    def system_site_index():
        from core.models import Site
        from core.libs.bottle import template
        sites = Site.select()

        tpl = '''
<p>This is the local web server for an installation of {}.
<p><a href='{}'>Open the site dashboard</a>
<hr/>
<p>You can also preview sites and blogs available on this server:
<ul>'''.format(PRODUCT_NAME, BASE_PATH) + '''
% for site in sites:
    <li>{{site.name}}</li>
    % if site.blogs.count()>0:
        <ul>
        % for blog in site.blogs:
        % if blog.pages.published.count()>0:
            <li><a href="/?_={{blog.id}}">{{blog.name}}</a></li>
        % else:
        <li>{{blog.name}} [No published pages on this blog]</li>
        % end
        % end
        </ul>
    % end
%end
</ul><hr/>
'''
        return template(tpl, sites=sites)
Exemplo n.º 23
0
def index(request, site):
    active_site = Site.get_cached_by_id(site)

    available_site_types = []
    for t in Site.TYPE_CHOICES:
        if t[0] == 'mal':
            if not request.user.has_perm('sherpa_admin'):
                continue
        available_site_types.append(t)

    if 'form_post_data' in request.session:
        form = SiteForm(request.user, request.session['form_post_data'], auto_id='%s')
        del request.session['form_post_data']
    else:
        form = SiteForm(request.user, auto_id='%s')

    context = {
        'form': form,
        'active_site': active_site,
        'available_site_types': available_site_types,
        'template_types': Site.TEMPLATE_TYPE_CHOICES,
        'language_code_choices': Site.LANGUAGE_CODE_CHOICES,
    }

    if 'message_context' in request.session:
        context['message_context'] = request.session['message_context']
        del request.session['message_context']

    return render(request, 'central/admin/sites/settings/index.html', context)
Exemplo n.º 24
0
def site_create(**new_site_data):

    new_site = Site()

    new_site.name = new_site_data['name']
    new_site.description = new_site_data['description']
    new_site.url = new_site_data['url']
    new_site.path = new_site_data['path']
    new_site.local_path = new_site.path

    new_site.save()

    return new_site
Exemplo n.º 25
0
def index(request, site):
    active_site = Site.get_cached_by_id(site)
    context = {
        'active_site': active_site,
        'ga_account_username': settings.GA_ACCOUNT_USERNAME,
        'ga_account_password': settings.GA_ACCOUNT_PASSWORD,
    }
    return render(request, 'central/admin/sites/settings/analytics/index.html', context)
Exemplo n.º 26
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)
Exemplo n.º 27
0
def delete(request, site, article):
    try:
        active_site = Site.get_cached_by_id(site)
        Article.objects.get(id=article).delete()
    except Article.DoesNotExist:
        # Probably not a code error but a double-click or something, ignore
        pass
    return redirect('admin:sites.articles.list', active_site.id)
Exemplo n.º 28
0
 def process_request(self, request):
     """
     Set request.site to contain the Site object responsible for handling this request,
     according to hostname matching rules
     """
     try:
         request.site = Site.find_for_request(request)
     except Site.DoesNotExist:
         request.site = None
Exemplo n.º 29
0
def list_load(request, site):
    active_site = Site.get_cached_by_id(site)
    if not request.is_ajax():
        return redirect('admin:sites.articles.list', active_site.id)
    context = {
        'versions': list_bulk(request, int(request.POST['bulk']), active_site),
        'active_site': active_site,
    }
    return HttpResponse(render_to_string('central/admin/sites/articles/list-elements.html', context, request=request))
Exemplo n.º 30
0
def index(request, site):
    if not request.user.has_perm('sherpa_admin'):
        raise PermissionDenied

    active_site = Site.get_cached_by_id(site)
    context = {
        'active_site': active_site,
    }
    return render(request, 'central/admin/sites/settings/launch/index.html', context)
Exemplo n.º 31
0
def index(request, site):
    active_site = Site.get_cached_by_id(site)
    menus = Menu.objects.filter(site=active_site).order_by('order')
    context = {
        'active_site': active_site,
        'menus': menus,
    }
    context.update(url_picker_context(active_site))
    return render(request, 'central/admin/sites/navigation/index.html', context)
Exemplo n.º 32
0
def system_sites(errormsg=None):
    user = auth.is_logged_in(request)
    permission = auth.is_sys_admin(user)

    return listing(request,
                   None,
                   Site.select(),
                   'all_sites',
                   'manage_sites',
                   user=user)
Exemplo n.º 33
0
def show(request, site):
    active_site = Site.get_cached_by_id(site)

    context = {
        'active_site': active_site,
        'ga_profile_id': Site.GA_PROFILE_ID_MAPPING.get(active_site.analytics_ua),
        'ga_account_username': settings.GA_ACCOUNT_USERNAME,
        'ga_account_password': settings.GA_ACCOUNT_PASSWORD,
    }
    return render(request, 'central/admin/sites/show.html', context)
Exemplo n.º 34
0
def save_site(data):
    """Create or update an site content type"""

    content_type = content_type_check_and_load("Site Content Type")

    if data["hidden_node_id"] and data["hidden_node_version"]:  # Assume update
        existing_content = load_and_revise(data["hidden_node_id"], SiteRevision)
        update_object_hash_and_save(existing_content, data)

        return existing_content

    else:  # Assume new site
        node = _register_node()

        site = Site(
            _version=1,
            _node_id=node._id,
            _lock="",
            site_name=data["site_name"],
            environment_name=data["environment_name"],
            local_build_dir=data["local_build_dir"],
            static_files_dir=data["static_files_dir"],
            hosting_type=data["hosting_type"],
            index_content=data["index_content"],
            menu_content=data["menu_content"],
            groups_content=data["groups_content"],
        )
        # First save get's our article ID to include in the hash
        #!!! <- being first save
        db.session.add(site)
        db.session.commit()
        db.session.refresh(site)
        #!!! -> end first saved
        ### reuse hash and save here
        site._hash = _hash_table(site)  # Hash after getting id
        site._hash_chain = _hash_table(site, chain=True)
        db.session.add(site)
        db.session.commit()
        db.session.refresh(site)

        content_obj = _associate_node(node, site, content_type)

        return content_obj
Exemplo n.º 35
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)
Exemplo n.º 36
0
def preview(request, site, version):
    active_site = Site.get_cached_by_id(site)
    version = Version.objects.get(id=version)
    context = {
        'active_site': active_site,
        'version': version,
        'preview_context': True,
    }

    # Fake request.site to the edited site; this will make context processors behave accordingly
    request.site = active_site
    return render(request, 'central/admin/sites/pages/preview.html', context)
Exemplo n.º 37
0
def delete(request, site):
    if request.method != 'POST':
        raise PermissionDenied

    active_site = Site.get_cached_by_id(site)
    if request.POST.get('confirm', '').lower() != active_site.get_title().lower():
        # This should have been verified client-side, so don't handle it gracefully here
        raise PermissionDenied

    active_site.delete()
    messages.success(request, 'site_deleted')
    return redirect('admin:sites.index')
Exemplo n.º 38
0
def confirm_delete(request, site, article):
    try:
        active_site = Site.get_cached_by_id(site)
        version = Version.objects.get(article=article, active=True)
        context = {
            'active_site': active_site,
            'version': version,
        }
        return render(request, 'central/admin/sites/articles/confirm-delete.html', context)
    except Version.DoesNotExist:
        # Probably not a code error but a double-click or something, ignore
        return redirect('admin:sites.articles.list', active_site.id)
Exemplo n.º 39
0
def children(request, site):
    active_site = Site.get_cached_by_id(site)
    versions = Version.objects.filter(
        page__parent=request.POST['page_id'],
        active=True,
    ).order_by('page__title')
    context = {
        'active_site': active_site,
        'versions': versions,
        'level': request.POST['level'],
    }
    return HttpResponse(render_to_string('central/admin/sites/pages/list_result.html', context, request=request))
Exemplo n.º 40
0
def push_to_queue(**ka):
    '''
    Inserts a single job item into the work queue.

    :param job_type:
        A string representing the type of job to be inserted.
        'Page','Index', eventually 'Fileinfo'

    :param data_integer:
        Any integer data passed along with the job. For a job control item, this
        is the number of items remaining for that particular job.

    :param blog:
        The blog object associated with the job.

    :param site:
        The site object associated with the job.

    :param priority:
        An integer, from 0-9, representing the processing priority associated with the job.
        Higher-priority jobs are processed first. Most individual pages are given a high
        priority; indexes are lower.
    '''

    try:
        queue_job = Queue.get(Queue.job_type == ka['job_type'],
                              Queue.data_integer == ka['data_integer'],
                              Queue.blog == ka['blog'],
                              Queue.site == ka['site'])
    except Queue.DoesNotExist:
        queue_job = Queue()
    else:
        return

    queue_job.job_type = ka['job_type']
    queue_job.data_integer = int(ka.get('data_integer', None))
    queue_job.blog = ka.get('blog', Blog()).id
    queue_job.site = ka.get('site', Site()).id
    queue_job.priority = ka.get('priority', 9)
    queue_job.is_control = ka.get('is_control', False)

    if queue_job.is_control:
        queue_job.data_string = (queue_job.job_type +
                                 ": Blog {}".format(queue_job.blog.for_log))
    else:
        queue_job.data_string = (
            queue_job.job_type + ": " +
            FileInfo.get(FileInfo.id == queue_job.data_integer).file_path)

    queue_job.date_touched = datetime.datetime.now()
    queue_job.save()
Exemplo n.º 41
0
    def run(self):
        db_sites = Site.objects.all()
        for db_site in db_sites:
            self.local_sites[db_site.login_base] = db_site
        print "%s local sites" % len(db_sites)

        sites = self.api.GetSites({'peer_id': None})
        print "%s remote sites" % len(sites)
        count = 0
        for site in sites:
            self.remote_sites[site['site_id']] = site
            if site['login_base'] not in self.local_sites:
                new_site = Site(name=site['name'],
                                login_base=site['login_base'],
                                site_url=site['url'],
                                enabled=site['enabled'],
                                longitude=site['longitude'],
                                latitude=site['latitude'],
                                is_public=site['is_public'],
                                abbreviated_name=site['abbreviated_name'])
                new_site.save()
                count += 1
                self.local_sites[new_site.login_base] = new_site
        print "imported %s sites" % count
Exemplo n.º 42
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}
                   )
Exemplo n.º 43
0
def create_site(request):
    if request.method == "POST":
        serializer = SiteSerializer(data=request.data)
        if serializer.is_valid():
            now = str(datetime.today().timestamp())
            image_name = "".join(
                [request.user.username, "_", now, "_image.png"])
            Site(
                category=Category.objects.get(
                    name=serializer.data["category"]),
                deadline=default_date(),
                image_path=image_name,
                url=serializer.data["url"],
                user=request.user,
            ).save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 44
0
def sites_create(request):
    if request.method == "POST":
        form = SiteForm(request.user, request.POST)
        if form.is_valid():
            url = form.cleaned_data["url"]
            category = form.cleaned_data["category"]
            deadline = form.cleaned_data["deadline"]
            now = str(datetime.today().strftime("%a%b%d%H:%M:%S%Y"))
            image_name = "".join(
                [request.user.username, "_", now, "_image.png"])
            Site(
                category=category,
                deadline=deadline,
                image_path=image_name,
                url=url,
                user=request.user,
            ).save()
            messages.success(request,
                             "Entry sucessfully saved - Saving a screen shot")
            return redirect("core:site_management")
    else:
        form = SiteForm(request.user)
    return render(request, "core/site_create.html", {"form": form})
Exemplo n.º 45
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__)
Exemplo n.º 46
0
def step_4_pre():

    if get_ini('main', 'DO_DB_CHECK') is None:
        store_ini('main', 'DO_DB_CHECK', 'Y')
        from core.utils import reboot
        reboot()

    report = []

    from core.models import db, Template
    try:
        db.connect()
    except:
        raise

    db.close()

    report.append("Database connection successful.")

    from settings import DB
    DB.recreate_database()

    report.append("Database tables created successfully.")

    username = "******"
    email = get_ini("user", "email")
    password = get_ini("user", "password")
    blog_path = get_ini("install", "blog_path")

    from core.utils import encrypt_password
    p_key = get_ini('key', 'PASSWORD_KEY')
    password = encrypt_password(password, p_key)

    db.connect()

    with db.atomic():

        from core.models import Site
        new_site = Site.create(
            name="Your first site",
            description="The description for your first site.",
            url=get_ini('main', 'base_url_root'),
            path=blog_path)

        report.append("Initial site created successfully.")

        from core.models import User
        new_user = User(name='Administrator',
                        email=email,
                        encrypted_password=password)

        new_user.save_pwd()

        from core.auth import role

        new_user_permissions = new_user.add_permission(
            permission=role.SYS_ADMIN, site=new_site)

        new_user_permissions.save()

        report.append("Initial admin user created successfully.")

        plugindir = _join((_s.APPLICATION_PATH, 'data', 'plugins'))

        import shutil

        # TODO: warn on doing this?
        # this should only happen with a totally fresh install, not an upgrade

        install_directory = _join((_s.APPLICATION_PATH, _s.INSTALL_SRC_PATH))

        if (os.path.isdir(plugindir)):
            shutil.rmtree(plugindir)

        shutil.copytree(_join((install_directory, 'plugins')), plugindir)

        report.append("Default plugins copied successfully to data directory.")

        themedir = _join((_s.APPLICATION_PATH, 'data', 'themes'))

        if (os.path.isdir(themedir)):
            shutil.rmtree(themedir)

        shutil.copytree(_join((install_directory, 'themes')), themedir)

        report.append("Default themes copied successfully to data directory.")

        from core import plugins

        for x in os.listdir(plugindir):
            if (os.path.isdir(_join((plugindir, x))) is True
                    and x != '__pycache__'):
                new_plugin = plugins.register_plugin(x, enable=True)
                report.append("New plugin '{}' installed successfully.".format(
                    new_plugin.name))

        from settings.defaults import DEFAULT_THEME
        from core.models import Theme
        new_theme = Theme.install_to_system(DEFAULT_THEME)

        report.append(
            "Default theme created and installed successfully to system.")

        from core.models import Blog

        new_blog = Blog(site=new_site,
                        name="Your first blog",
                        description="The description for your first blog.",
                        url=new_site.url,
                        path=new_site.path,
                        local_path=new_site.path,
                        theme=new_theme)

        # TODO: add blog-level permission for new user as well

        new_blog.setup(new_user, new_theme)

        # TODO: Add default post

        report.append("Initial blog created successfully with default theme.")

    db.close()

    output_file_name = _join(
        (_s.APPLICATION_PATH + _s.DATA_FILE_PATH, _s.INI_FILE_NAME))

    config_parser = ConfigParser()

    sections = ('db', 'path', 'key')

    for s in sections:
        for name, value in parser.items(s):
            try:
                config_parser.add_section(s)
            except DuplicateSectionError:
                pass
            config_parser.set(s, name, value)


#     if request.environ['HTTP_HOST'] == _s.DEFAULT_LOCAL_ADDRESS + _s.DEFAULT_LOCAL_PORT:
#         config_parser.add_section('server')
#         config_parser.set('server', 'DESKTOP_MODE', 'True')

    try:
        with open(output_file_name, "w", encoding='utf-8') as output_file:
            config_parser.write(output_file)
    except BaseException as e:
        raise SetupError(str(e.__class__.__name__) + ": " + str(e))

    try:
        os.remove(config_file_name)
    except OSError as e:
        from core.error import not_found
        if not_found(e) is False:
            raise e
    except Exception as e:
        raise e

    finished = '''
    <p>Installation is complete. <a href="{}">Return to the main page to begin using the application.</a>
    <script>
    $.get('/reboot',function(data){{}});
    </script>
    '''.format(_s.BASE_URL)

    return {'report': report, 'finished': finished}
Exemplo n.º 47
0
from xosconfig import Config
from multistructlog import create_logger

log = create_logger(Config().get("logging"))


class XOSDefaultSecurityContext(object):
    grant_access = True
    write_access = True
    read_access = True


xos_anonymous_site = Site(
    name="XOS Anonymous Site",
    enabled=True,
    hosts_nodes=False,
    hosts_users=True,
    login_base="xos",
    abbreviated_name="xos-anonymous",
)

xos_anonymous_user = User(
    username="******",
    email="*****@*****.**",
    is_admin=False,
    site=xos_anonymous_site,
)


class XOSAPIHelperMixin(XOSAuthHelperMixin):
    """ This helper contains several functions used to implement the autogenerated core API.
        It translates between the gRPC representation of objects and the django representation
Exemplo n.º 48
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)
Exemplo n.º 49
0
    def get_network_links(self):

        objs = []

        # Connectivity object - Point to Point
        cordpod1device = NetworkDevice()
        cordpod1device.id = self.networkdevice.id

        # Edge to Edge Point Connectivity Objects
        edgetoedgeconnectivity = NetworkEdgeToEdgePointConnection()
        edgetoedgeconnectivity.uni1_createbuffer = cordpod1device.id + "." + "of:000000001/1"
        edgetoedgeconnectivity.uni2_createbuffer = cordpod1device.id + "." + "of:000000001/2"
        edgetoedgeconnectivity.type = 'direct'
        edgetoedgeconnectivity.operstate = 'active'
        edgetoedgeconnectivity.adminstate = 'enabled'
        edgetoedgeconnectivity.sid = 'EdgeToEdgePointConnectivity_1'
        objs.append(edgetoedgeconnectivity)


        # Multipoint to Multipoint Connectivity Objects
        multipoint2multipointconnectivity=NetworkMultipointToMultipointConnection()
        multipoint2multipointconnectivity.operstate = 'active'
        multipoint2multipointconnectivity.adminstate = 'enabled'
        multipoint2multipointconnectivity.type = 'ethernet'
        multipoint2multipointconnectivity.sid = 'MultipointToMultipointConnectivity_1'

        #
        # Create JSON array for post-save behaviour
        #
        eps = []
        eps.append(cordpod1device.id + "." + "of:000000001/3")
        eps.append(cordpod1device.id + "." + "of:000000001/4")
        eps.append(cordpod1device.id + "." + "of:000000001/5")

        myjsonstr = {'eps': eps, 'foo':0, 'bar':0}
        multipoint2multipointconnectivity.eps_createbuffer = json.dumps(myjsonstr)
        objs.append(multipoint2multipointconnectivity)

        # Edge to Multipoint Connectivity Objects
        edge2multipointconnectivity = NetworkEdgeToMultipointConnection()
        edge2multipointconnectivity.operstate = 'active'
        edge2multipointconnectivity.adminstate = 'enabled'
        edge2multipointconnectivity.type = 'ethernet'
        edge2multipointconnectivity.sid = 'EdgeToMultipointConnectivity_1'
        edge2multipointconnectivity.root_createbuffer = cordpod1device.id + "." + "of:000000001/7"
        #
        # Create JSON array for post-save behaviour
        #
        eps = []
        eps.append(cordpod1device.id + "." + "of:000000001/6")
        eps.append(cordpod1device.id + "." + "of:000000001/8")

        myjsonstr = {'eps': eps, 'foo': 0, 'bar': 0}
        edge2multipointconnectivity.eps_createbuffer = json.dumps(myjsonstr)
        objs.append(edge2multipointconnectivity)

        # Create Objects for VnodGlobal Sort of Testing

        # Bandwidth Profile

        bwprofile = BandwidthProfile()
        bwprofile.bwpcfgcbs  = 0
        bwprofile.bwpcfgcir = 0
        bwprofile.bwpcfgebs = 0
        bwprofile.bwpcfgeir = 0
        bwprofile.name = 'TestBWP'
        objs.append(bwprofile)

        # Two Sites
        site1 = Site()
        site1.name = 'CORDPod1'
        site1.login_base = 'CordPod1'
        site1.site_url = 'http://1.2.3.4:8080/VnodLocalApi'
        objs.append(site1)

        site2 = Site()
        site2.name = 'CORDPod2'
        site2.login_base = 'CordPod2'
        site2.site_url = 'http://10.11.12.13:8080/VnodLocalApi'
        objs.append(site2)

        # Two Ports - one per Site

        remoteport1 = RemotePort()
        remoteport1.name = "CORDPOD1:Port1"
        remoteport1.sitename = 'CordPod1'
        remoteport1.edgeportname = cordpod1device.id + "." + "of:000000001/1"
        objs.append(remoteport1)

        remoteport2 = RemotePort()
        remoteport2.name = "CORDPOD2:Port1"
        remoteport2.sitename = 'CordPod2'
        remoteport2.edgeportname = cordpod1device.id + "." + "of:000000001/2"
        objs.append(remoteport2)

        # One Spoke/Site
        spoke1 = ServiceSpoke()
        spoke1.name = 'TestSpoke1'
        spoke1.remoteportname = "CORDPOD1:Port1"
        spoke1.remotevnodid = 'CORDPod1:VnodLocal:1'
        spoke1.operstate = 'inactive'
        spoke1.adminstate = 'enabled'
        spoke1.sitename = 'CordPod1'
        objs.append(spoke1)

        spoke2 = ServiceSpoke()
        spoke2.name = 'TestSpoke2'
        spoke2.remoteportname = "CORDPOD2:Port1"
        spoke2.remotevnodid = 'CORDPod2:VnodLocal:1'
        spoke2.operstate = 'active'
        spoke2.adminstate = 'enabled'
        spoke2.sitename = 'CordPod2'
        objs.append(spoke2)

        # One VnodGlobal Service
        vnodglobal = VnodGlobalService()
        vnodglobal.name = 'VnodGlobalPtToPtTest1'
        vnodglobal.type = 'eline'
        vnodglobal.vlanid = '100'
        vnodglobal.operstate = 'active'
        vnodglobal.adminstate = 'enabled'
        vnodglobal.servicehandle = 'testhandle1'
        vnodglobal.pointtopointsid = 'onos_eline_id'
        vnodglobal.bwpname = 'TestBWP'

        # Create JSON array for post-save behaviour
        #
        spokes = ['TestSpoke1', 'TestSpoke2']
        myjsonstr = {'spokes': spokes}
        vnodglobal.spokes_createbuffer = json.dumps(myjsonstr)
        objs.append(vnodglobal)

        return objs
Exemplo n.º 50
0
        'url_to_crawl': 'http://mlweekly.com/',
        'domain': '',
        'slug': 'machine-learning-weekly',
    },
    {
        'name': 'MIT News',
        'main_url': 'http://news.mit.edu/topic/machine-learning/',
        'url_to_crawl': 'http://news.mit.edu/topic/machine-learning/',
        'domain': 'http://news.mit.edu',
        'slug': 'mit-news',
    },
)

REDDIT = sites_info[0]['name']
ML_MASTERY = sites_info[1]['name']
ML_WEEKLY = sites_info[2]['name']
MIT_NEWS = sites_info[3]['name']

PERIODS = ['all', 'today', 'yesterday', 'week', 'month']
ALL, TODAY, YESTERDAY, WEEK, MONTH = PERIODS

if __name__ == '__main__':
    django_setup()
    from core.models import Site

    for site in sites_info:
        try:
            Site(**site).save()
        except IntegrityError:
            pass
Exemplo n.º 51
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)