Example #1
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)
Example #2
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)
Example #3
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)
Example #4
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)
Example #5
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)
Example #6
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()
Example #7
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)
Example #8
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)
Example #9
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)
Example #10
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))
Example #11
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)
Example #12
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)
Example #13
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)
Example #14
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)
Example #15
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))
Example #16
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')
Example #17
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)
Example #18
0
def list(request, site):
    active_site = Site.get_cached_by_id(site)
    ads = Ad.objects.filter(site=site).order_by('name')
    time_placements = AdPlacement.objects.filter(site=active_site, end_date__isnull=False).order_by('start_date', 'end_date')
    view_placements = AdPlacement.objects.filter(site=active_site, view_limit__isnull=False).order_by('views')
    context = {
        'active_site': active_site,
        'ads': ads,
        'time_placements': time_placements,
        'view_placements': view_placements,
        'format_choices': Ad.FORMAT_CHOICES,
    }
    return render(request, 'central/admin/sites/ads/list.html', context)
Example #19
0
def preview(request, site, version):
    active_site = Site.get_cached_by_id(site)
    version = Version.objects.get(id=version)
    # Pretend publish date is now, just for the preivew
    version.article.pub_date = datetime.now()
    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/articles/preview.html', context)
Example #20
0
def update_placement(request, site):
    active_site = Site.get_cached_by_id(site)
    try:
        placement = AdPlacement.objects.get(id=request.POST['id'], site=active_site)
        placement.ad = Ad.objects.get(id=request.POST['ad'], site=active_site)
        placement.start_date = datetime.strptime(request.POST['start_date'], "%d.%m.%Y")
        if placement.end_date is not None:
            placement.end_date = datetime.strptime(request.POST['end_date'], "%d.%m.%Y")
        else:
            placement.view_limit = request.POST['view_limit']
        placement.save()
    except ValueError:
        messages.error(request, 'invalid_date')
    return redirect('admin:sites.ads.list', active_site.id)
Example #21
0
def delete(request, site, page_id):
    delete_children = request.GET.get('delete_children', False)
    active_site = Site.get_cached_by_id(site)

    if not delete_children:
        page = Page.objects.get(id=page_id, site=active_site)
        if page.is_root_node():
            raise PermissionDenied
        new_parent = page.parent
        page.reparent_children(new_parent)

    # Yes, have to get the page again, or things will get messy
    page = Page.objects.get(id=page_id, site=active_site)
    page.delete()

    return redirect('admin:sites.pages.page.list', active_site.id)
Example #22
0
def launch(request, site):
    if not request.user.has_perm('sherpa_admin'):
        raise PermissionDenied

    active_site = Site.get_cached_by_id(site)
    if request.method != 'POST':
        return redirect('admin:sites.settings.launch.index', site)

    if 'new-domain' not in request.POST:
        return redirect('admin:sites.settings.launch.index', site)

    # Set the new domain, and publish the site
    old_domain = active_site.domain
    new_domain = request.POST['new-domain'].strip()

    if Site.objects.filter(domain=new_domain).exclude(pk=active_site.pk).exists():
        messages.error(request, 'domain_already_in_use')
        return redirect('admin:sites.settings.launch.index', site)

    active_site.domain = new_domain
    active_site.published = True
    active_site.save()

    # Replace links in content, menus, campaigns
    if request.POST.get('replace_links') == 'on':
        for content in Content.objects.filter(
                Q(column__row__version__page__site=active_site) |
                Q(column__row__version__article__site=active_site)):
            if old_domain in content.content:
                content.content = re.sub(old_domain, new_domain, content.content)
                content.save()

        for menu in Menu.objects.filter(site=active_site):
            if old_domain in menu.url:
                menu.url = re.sub(old_domain, new_domain, menu.url)
                menu.save()

        for campaign in Campaign.objects.filter(site=active_site):
            if old_domain in campaign.button_anchor:
                campaign.button_anchor = re.sub(old_domain, new_domain, campaign.button_anchor)
                campaign.save()

    # Clear the entire cache. A possible optimization here would be to delete only relevant cache keys for the site.
    cache.clear()

    messages.success(request, 'site_launched')
    return redirect('admin:sites.settings.index', active_site.id)
Example #23
0
def index(request, site):
    active_site = Site.get_cached_by_id(site)

    pages = Page.objects.filter(site=active_site).order_by('title')
    article_versions = Version.objects.filter(
        article__isnull=False,
        article__published=True,
        active=True,
        article__pub_date__lt=datetime.now(),
        article__site=active_site,
    ).order_by('-article__pub_date')

    context = {
        'active_site': active_site,
        'article_versions': article_versions,
        'pages': pages,
    }
    return render(request, 'central/admin/sites/settings/cache/index.html', context)
Example #24
0
def list(request, site):
    active_site = Site.get_cached_by_id(site)
    pages = Page.objects.filter(
        site=active_site,
    ).select_related(
        'created_by',
        'modified_by',
    ).prefetch_related(
        'versions',
        'versions__tags',
    )
    root_page = [p for p in pages if p.level == 0][0]
    context = {
        'active_site': active_site,
        'nodes': pages,
        'root_node': root_page,
    }
    return render(request, 'central/admin/sites/pages/list.html', context)
Example #25
0
def reload_raw_widget(request, site):
    try:
        active_site = Site.get_cached_by_id(site)
        return HttpResponse(render_widget(
            request,
            json.loads(request.POST['content']),
            active_site,
            raw=True,
            admin_context=True,
        ))
    except:
        logger.warning(
            "Feil ved raw widget rendering i editor",
            exc_info=sys.exc_info(),
            extra={
                'request': request,
                'site': site,
            }
        )
        return HttpResponseBadRequest()
Example #26
0
def edit(request, site, version):
    try:
        active_site = Site.get_cached_by_id(site)
        version = Version.objects.get(id=version)
        users = sorted(User.sherpa_users(), key=lambda u: u.get_first_name())
        context = {
            'active_site': active_site,
            'version': version,
            'users': users,
            'image_search_length': settings.IMAGE_SEARCH_LENGTH,
            'widget_data': admin_context(active_site),
            'category_choices': Article.CATEGORY_CHOICES,
        }
        context.update(url_picker_context(active_site))

        # Fake request.site to the edited site; this will make context processors behave accordingly
        request.site = active_site
        return render(request, 'central/admin/sites/articles/edit.html', context)
    except Version.DoesNotExist:
        messages.warning(request, 'article_does_not_exist')
        return redirect('admin:sites.articles.list', active_site.id)
Example #27
0
def delete(request, site):
    active_site = Site.get_cached_by_id(site)
    if not request.is_ajax():
        return redirect('admin:sites.settings.cache.index', active_site.id)

    if request.POST['key'] == 'frontpage':
        page = Page.objects.get(
            slug='',
            site=active_site,
        )
        cache.delete('content.site.%s.slug.%s' % (active_site.id, sha1(page.slug.encode('utf-8')).hexdigest()))
    elif request.POST['key'] == 'page':
        page = Page.objects.get(
            id=request.POST['id'],
            site=active_site,
        )
        cache.delete('content.site.%s.slug.%s' % (active_site.id, sha1(page.slug.encode('utf-8')).hexdigest()))
    elif request.POST['key'] == 'article':
        cache.delete('articles.%s' % request.POST['id'])
        version = Version.objects.get(article__id=request.POST['id'])
        cache.delete('version.%s.thumbnail.small' % version.id)
        cache.delete('version.%s.thumbnail.medium' % version.id)
    return HttpResponse()
Example #28
0
def create_placement(request, site):
    active_site = Site.get_cached_by_id(site)
    try:
        ad = Ad.objects.get(id=request.POST['ad'], site=active_site)
        if request.POST['adplacement_type'] == 'time':
            start_date = datetime.strptime(request.POST['start_date'], "%d.%m.%Y")
            end_date = datetime.strptime(request.POST['end_date'], "%d.%m.%Y")
            view_limit = None
        else:
            start_date = datetime.strptime(request.POST['start_date'], "%d.%m.%Y")
            end_date = None
            view_limit = request.POST['view_limit']
        ap = AdPlacement(
            ad=ad,
            start_date=start_date,
            end_date=end_date,
            view_limit=view_limit,
            site=active_site,
        )
        ap.save()
    except ValueError:
        messages.error(request, 'invalid_date')
    return redirect('admin:sites.ads.list', active_site.id)
Example #29
0
def new(request, site):
    active_site = Site.get_cached_by_id(site)

    if request.method != 'POST':
        return redirect('admin:sites.pages.page.list', active_site.id)

    slug = request.POST['slug'].strip().strip('/')

    unused_system_url = not is_system_url(slug, active_site)
    unused_page_slug = Page.slug_is_unique(slug, active_site)

    if not unused_system_url or not unused_page_slug:
        messages.error(request, 'slug_not_unique')
        return redirect('admin:sites.pages.page.new', active_site.id)

    page = Page(
        title=request.POST['title'],
        slug=slug,
        published=False,
        created_by=request.user,
        parent=Page.objects.get(id=request.POST['parent_id']),
        site=active_site,
    )
    page.save()

    version = Version(
        page=page,
        article=None,
        version=1,
        owner=request.user,
        active=True,
    )
    version.save()

    create_template(request.POST['template'], version)
    return redirect('admin:sites.pages.page.edit', active_site.id, version.id)
Example #30
0
def save(request, site, version):
    active_site = Site.get_cached_by_id(site)

    try:
        version = Version.objects.get(id=version)
    except Version.DoesNotExist:
        return HttpResponse(json.dumps({'version_does_not_exist': True}))

    response = {}

    # Wrap the entire save operation in an atomic transaction
    with transaction.atomic():

        # Delete all existing rows
        version.rows.all().delete()

        posted_rows = json.loads(request.POST['rows'])

        for row in posted_rows:
            row_obj = Row(version=version, order=row['order'])
            row_obj.save()
            for column in row['columns']:
                column_obj = Column(
                    row=row_obj,
                    span=column['span'],
                    offset=column['offset'],
                    order=column['order'],
                )
                column_obj.save()
                for content in column['contents']:
                    content_obj = Content(
                        column=column_obj,
                        content=content['content'],
                        type=content['type'],
                        order=content['order']
                    )
                    content_obj.save()

        # Tags - common for pages and articles
        version.tags.clear()
        for tag in [t.lower() for t in json.loads(request.POST['tags'])]:
            obj, created = Tag.objects.get_or_create(name=tag)
            version.tags.add(obj)

        # Article/Page data
        if version.page is not None:
            page = version.page

            # Clear the cache *before* data assignment in case the slug has changed, to ensure the old slug cache is
            # invalidated
            page.clear_cache()

            ### Title ###
            page.title = request.POST['title']
            page.slug = request.POST['slug'].strip().strip('/')

            ### Published state ###
            page.published = json.loads(request.POST['status'])

            # Record the modification
            page.modified_by = request.user
            page.modified_date = datetime.now()

            version.save()
            page.save()

            # Clear cache again with potentially new slug
            page.clear_cache()

        elif version.article is not None:
            article = version.article

            ### Authors ###
            publisher_list = json.loads(request.POST['authors'])
            if len(publisher_list) > 0:
                publishers = User.get_users().filter(id__in=publisher_list)
                version.publishers = publishers
            else:
                response['author_error'] = 'no_authors'

            ### Category ###
            article.category = request.POST['category']

            ### Published state ###
            try:
                article.published = json.loads(request.POST['status'])
                datetime_string = '%s %s' % (request.POST['publish_date'], request.POST['publish_time'])
                article.pub_date = datetime.strptime(datetime_string, '%d.%m.%Y %H:%M')
            except ValueError:
                if article.published:
                    # We're trying to publish, and an error occured.
                    if request.POST['publish_date'] == '' and request.POST['publish_time'] == '':
                        # Well, since we didn't specify the date, set it to now - and update it clientside
                        now = datetime.now()
                        response['publish_error'] = 'auto_now'
                        response['publish_date'] = now.strftime('%d.%m.%Y')
                        response['publish_time'] = now.strftime('%H:%M')
                        article.pub_date = now
                    else:
                        # Parse error - inform, and don't publish
                        response['publish_error'] = 'unparseable_datetime'
                        article.published = False
                else:
                    # An error occured, but we're not publishing so just nullify
                    response['publish_error'] = 'error_nullify'
                    article.pub_date = None

            ### Unpublish date ###
            try:
                article.unpub_date = datetime.strptime(
                    request.POST['unpublish_date'],
                    '%d.%m.%Y',
                ).date()
            except ValueError:
                article.unpub_date = None

            ### Thumbnail state ###
            if request.POST['thumbnail'] == 'none':
                article.thumbnail = None
                article.hide_thumbnail = True
            elif request.POST['thumbnail'] == 'default':
                # Verify server-side that there's at least one image
                image_content = False
                for row in posted_rows:
                    for column in row['columns']:
                        if any(content['type'] == 'image' for content in column['contents']):
                            image_content = True
                            break

                if not image_content:
                    response['thumbnail_missing_image'] = True
                    article.thumbnail = None
                    article.hide_thumbnail = True
                else:
                    article.thumbnail = None
                    article.hide_thumbnail = False
            elif request.POST['thumbnail'] == 'specified':
                article.thumbnail = request.POST['thumbnail_url']
                article.hide_thumbnail = False

            # Record the modification
            article.modified_by = request.user
            article.modified_date = datetime.now()

            version.save()
            article.save()

            cache.delete('articles.%s' % article.id)
            cache.delete('version.%s.thumbnail.small' % version.id)
            cache.delete('version.%s.thumbnail.medium' % version.id)

    return HttpResponse(json.dumps(response))