Esempio 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)
Esempio 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")
Esempio 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
Esempio 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
Esempio 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), ))
Esempio n. 6
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
Esempio n. 7
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), ))
Esempio n. 8
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
Esempio n. 9
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
Esempio n. 10
0
 def test_edge_case(self):
     pnt = GEOSGeometry('SRID=4326;POINT(-124.014723 45.045150)')
     s = Site(geometry = pnt)
     s.save()
     self.assertEqual(s.state.initials, 'OR')
     self.assertEqual(s.county, 'Tillamook')
Esempio n. 11
0
 def test_way_offshore(self):
     pnt = GEOSGeometry('SRID=4326;POINT(-127 44)')
     s = Site(geometry = pnt)
     s.save()
     self.assertEqual(s.state, None)
     self.assertEqual(s.county, None)
Esempio n. 12
0
 def test_offshore(self):
     pnt = GEOSGeometry('SRID=4326;POINT(-125 44)')
     s = Site(geometry = pnt)
     s.save()
     self.assertEqual(s.state.initials, 'OR')
     self.assertEqual(s.county, 'Lane')
Esempio n. 13
0
def create(request):
    if not request.user.is_admin_in_forening(request.active_forening):
        return render(request, 'common/admin/sites/create_disallowed.html')

    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)

    site_templates = Site.objects.filter(
        forening=Forening.DNT_CENTRAL_ID,
        type='mal',
    ).order_by('title')

    context = {
        'available_site_types': available_site_types,
        'site_templates': site_templates,
        'template_types': Site.TEMPLATE_TYPE_CHOICES,
    }

    if request.method == 'GET':
        context['form'] = CreateSiteForm(request.user, auto_id='%s')
        return render(request, 'common/admin/sites/create.html', context)

    elif request.method == 'POST':
        form = CreateSiteForm(request.user, request.POST, auto_id='%s')
        context['form'] = form

        if not form.is_valid():
            return render(request, 'common/admin/sites/create.html', context)

        if not request.POST['domain-type'] in ['fqdn', 'subdomain']:
            raise PermissionDenied

        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, prefix = form.cleaned_data['domain']

        site = Site(
            domain=domain,
            prefix=prefix,
            type=type,
            template='local',
            forening=site_forening,
            title=title,
            template_main=template_main,
            template_type=template_type,
            template_description=template_description,
        )

        site.save()

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

        # Invalidate the forening's homepage site cache
        cache.delete('forening.homepage_site.%s' % site_forening.id)

        if 'use-template' not in request.POST:
            # User explicitly requested not to clone any template
            pass
        elif request.POST.get('template', '').strip() == '':
            # Sherpa-admin error; a site-template for the chosen site type doesn't exist!
            # This needs to be fixed.
            logger.error(u"Sherpa-bruker opprettet en site med en mal som ikke finnes",
                extra={
                    'request': request,
                    'missing_template_type': request.POST.get('missing-template-type', '<unknown>'),
                }
            )
        else:
            # All right, let's clone the entire template site
            # Note that for most objects, we'll just set the primary key to None, change the site field to the
            # new site, and save it, which will insert a new object.
            # For related fields, we'll need to save the related set in memory before saving the new object, so
            # that we can iterate it, clone them and re-relate them to the new object
            # Additionally, replace domain name references in content from the template-domain to the new one
            template_site = Site.objects.get(id=request.POST['template'], type='mal')

            # Menus
            for menu in Menu.objects.filter(site=template_site):
                menu.id = None
                menu.site = site

                # Replace domain references with the new site domain
                menu.url = re.sub(template_site.domain, site.domain, menu.url)

                menu.save()

            # Pages

            # Used to map old IDs to their new corresponding pages. This is needed because the parent reference can't
            # be duplicated, it needs to be updated with its corresponding new parent
            page_id_mapping = {}

            # Order by the left value in order to insert new nodes in a consistent way
            for page in Page.objects.filter(site=template_site).order_by('lft'):
                variants = page.variant_set.all()
                old_id = page.id
                page.id = None
                page.site = site

                # Reset MPTT state and let our library recreate them
                page.tree_id = None
                page.lft = None
                page.rght = None
                page.level = None

                # Set parent to the corresponding clone; use the old parent id to retrieve it
                if page.parent is not None:
                    page.parent = page_id_mapping[page.parent.id]

                # Change creation to the user creating the new site and reset modification
                page.created_by = request.user
                page.created_date = datetime.now()
                page.modified_by = None
                page.modified_date = None

                # Insert the new node appropriately
                if page.parent is None:
                    Page.objects.insert_node(page, target=None, save=True)
                else:
                    Page.objects.insert_node(page, target=page.parent, position='last-child', save=True)

                page.save()

                # Remember which old id maps to this page
                page_id_mapping[old_id] = page

                for variant in variants:
                    versions = variant.version_set.all()
                    variant.id = None
                    variant.page = page
                    variant.save()

                    for version in versions:
                        rows = version.rows.all()
                        version.id = None
                        version.variant = variant
                        version.save()

                        for row in rows:
                            columns = row.columns.all()
                            row.id = None
                            row.version = version
                            row.save()

                            for column in columns:
                                contents = column.contents.all()
                                column.id = None
                                column.row = row
                                column.save()

                                for content in contents:
                                    content.id = None
                                    content.column = column

                                    # Replace domain references with the new site domain
                                    # Use a json dump with prepended/trailing quotes stripped to ensure the
                                    # replacement string is properly escaped if inserted into json-formatted content
                                    json_safe_domain = json.dumps(site.domain)[1:-1]
                                    content.content = re.sub(template_site.domain, json_safe_domain, content.content)

                                    # For aktiviteteslisting-widgets, force arranger-filter to the new site's related
                                    # forening
                                    if content.type == 'widget':
                                        parsed_content = json.loads(content.content)
                                        if parsed_content['widget'] == 'aktivitet_listing':
                                            # Note that the list of ids contains strings, because we forgot to convert
                                            # it to int in the widget-editor save logic, but that's not a problem since
                                            # the filter lookup will implicitly convert it. So force it to str to be
                                            # consistent
                                            parsed_content['foreninger'] = [str(site.forening.id)]
                                            content.content = json.dumps(parsed_content)

                                    content.save()

            # Articles
            for article in Article.objects.filter(site=template_site):
                variants = article.variant_set.all()
                article.id = None
                article.site = site

                # Change creation to the user creating the new site and reset modification
                article.created_by = request.user
                article.created_date = datetime.now()
                article.modified_by = None
                article.modified_date = None

                article.save()

                for variant in variants:
                    versions = variant.version_set.all()
                    variant.id = None
                    variant.article = article
                    variant.save()

                    for version in versions:
                        rows = version.rows.all()
                        version.id = None
                        version.variant = variant
                        version.save()

                        for row in rows:
                            columns = row.columns.all()
                            row.id = None
                            row.version = version
                            row.save()

                            for column in columns:
                                contents = column.contents.all()
                                column.id = None
                                column.row = row
                                column.save()

                                for content in contents:
                                    content.id = None
                                    content.column = column

                                    # Replace domain references with the new site domain
                                    # Use a json dump with prepended/trailing quotes stripped to ensure the
                                    # replacement string is properly escaped if inserted into json-formatted content
                                    json_safe_domain = json.dumps(site.domain)[1:-1]
                                    content.content = re.sub(template_site.domain, json_safe_domain, content.content)

                                    # For aktiviteteslisting-widgets, force arranger-filter to the new site's related
                                    # forening
                                    if content.type == 'widget':
                                        parsed_content = json.loads(content.content)
                                        if parsed_content['widget'] == 'aktivitet_listing':
                                            # Note that the list of ids contains strings, because we forgot to convert
                                            # it to int in the widget-editor save logic, but that's not a problem since
                                            # the filter lookup will implicitly convert it. So force it to str to be
                                            # consistent
                                            parsed_content['foreninger'] = [str(site.forening.id)]
                                            content.content = json.dumps(parsed_content)

                                    content.save()

            # Campaigns
            for campaign in Campaign.objects.filter(site=template_site):
                campaign_texts = campaign.text.all()
                campaign.id = None
                campaign.site = site

                # Replace domain references with the new site domain
                campaign.button_anchor = re.sub(template_site.domain, site.domain, campaign.button_anchor)

                campaign.save()

                for campaign_text in campaign_texts:
                    campaign_text.id = None
                    campaign_text.campaign = campaign
                    campaign_text.save()

        request.session.modified = True
        messages.info(request, 'site_created')
        return redirect('admin.sites.views.show', site.id)
from django.core.management import setup_environ
import os
import settings
setup_environ(settings)
#==================================#
from django.contrib.gis.geos import GEOSGeometry 
from core.models import Site, State
import csv
import sys

with open(sys.argv[1], 'rb') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        pnt = GEOSGeometry('SRID=4326;POINT(%s %s)' % (row['longitude'], row['latitude']))
        state = State.objects.get(name=row['state'])
        s = Site(geometry=pnt, sitename=row['sitename'], county=row['county'], state=state)
        s.save()
        print "Site saved:", s