Esempio n. 1
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)
Esempio n. 2
0
def index(request, site):
    active_site = Site.objects.get(id=site)
    menus = Menu.on(active_site).all().order_by('order')
    context = {
        'active_site': active_site,
        'menus': menus,
    }
    context.update(url_picker_context(active_site))
    return render(request, 'common/admin/sites/navigation/index.html', context)
Esempio n. 3
0
def edit(request, site, version):
    active_site = Site.objects.get(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),
    }
    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, 'common/admin/sites/articles/edit.html', context)
Esempio n. 4
0
def edit(request, site, version):
    active_site = Site.objects.filter(
        id=site,
    ).select_related(
        'forening',
    ).prefetch_related(
        # This is a bit backwards; based on our current active site, fetch the forening owning the site, and then
        # refetch *all* sites related to that forening - this will be used when rendering the template layout; for
        # example here: https://github.com/Turistforeningen/sherpa/blob/5d33684/templates/local/layout.html#L151
        # (Note that we're faking the request site below)
        'forening__sites',
    ).get()

    try:
        version = Version.objects.filter(
            id=version,
            page__site=active_site,
        ).prefetch_related(
            'rows',
            'rows__columns',
            'rows__columns__contents',
            'tags',
        ).select_related(
            'page',
            'page__parent',
        ).get()
    except Version.DoesNotExist:
        messages.error(request, 'version_does_not_exist')
        return redirect('admin:sites.pages.page.list', active_site.id)

    context = {
        'active_site': active_site,
        'version': version,
        'widget_data': admin_context(active_site),
        'image_search_length': settings.IMAGE_SEARCH_LENGTH,
        'preview_context': True, # To prevent counting ad view. Should probably introduce `editor_context` instead
    }
    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/pages/edit.html', context)
Esempio n. 5
0
def edit(request, site, version):
    active_site = Site.objects.get(id=site)
    root_page = Page.on(active_site).get(level=0)
    pages = Page.objects.filter(site=active_site)
    version = Version.objects.get(id=version)
    is_editing_root_page = root_page.id == version.variant.page.id
    context = {
        'active_site': active_site,
        'version': version,
        'widget_data': admin_context(active_site),
        'pages': pages,
        'root_page': root_page,
        'is_editing_root_page': is_editing_root_page,
        'image_search_length': settings.IMAGE_SEARCH_LENGTH,
    }
    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, 'common/admin/sites/pages/edit.html', context)
Esempio n. 6
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)
Esempio n. 7
0
def edit(request, forening_id):
    current_forening = Forening.objects.filter(
        id=forening_id,
    ).prefetch_related(
        # We need just one level of parents
        'parents',

        # Get all children and their children
        'children',
        'children__children',

        # Get the parents of all those children
        'children__parents',
        'children__children__parents',
    ).get()

    if current_forening not in request.user.all_foreninger():
        raise PermissionDenied

    # The parent choices are tricky to define in the forms API, so do it here.
    # Note that we're intentionally letting users choose parents among only
    # those they have permission to.
    all_sorted = request.user.all_foreninger_sorted()
    parents_choices = {
        'forening': all_sorted['forening'],
        'turlag': all_sorted['turlag'],
    }

    # If the parent of the current forening isn't in the user's permissions, we
    # still need to include that one as an available parent so that they're
    # able to make changes.
    if current_forening.type != 'sentral':
        for current_parent in current_forening.get_main_foreninger():
            if (current_parent not in parents_choices['forening'] and
                    current_parent not in parents_choices['turlag']):
                parents_choices[current_parent.type].append(current_parent)

    zipcode = current_forening.zipcode
    form_zipcode_area = zipcode.area if zipcode is not None else ''

    form = EditForeningForm(request.user, initial={
        'forening': current_forening.id,
        'focus_id': current_forening.focus_id,
        'parents': current_forening.parents.all(),
        'name': current_forening.name,
        'type': current_forening.type,
        'group_type': current_forening.group_type,
        'description': current_forening.description,
        'turbasen_object_id': current_forening.turbasen_object_id,
        'post_address': current_forening.post_address,
        'visit_address': current_forening.visit_address,
        'zipcode': zipcode.zipcode if zipcode is not None else '',
        'counties': current_forening.counties.all(),
        'choose_contact': 'person' if current_forening.contact_person != '' else 'forening',
        'contact_person': current_forening.contact_person,
        'phone': current_forening.phone,
        'email': current_forening.email,
        'organization_no': current_forening.organization_no,
        'gmap_url': current_forening.gmap_url,
        'facebook_url': current_forening.facebook_url,
        'homepage_url': current_forening.homepage_url,
        'offers_family_membership': current_forening.offers_family_membership,
        'aktivitet_signup_terms_url': current_forening.aktivitet_signup_terms_url,
        'handles_child_payments': current_forening.handles_child_payments,
        'accounting_software': current_forening.accounting_software,
        'accounting_aktivitet_department': current_forening.accounting_aktivitet_department,
        'accounting_aktivitet_debit_account': current_forening.accounting_aktivitet_debit_account,
        'accounting_aktivitet_debit_fee_account': current_forening.accounting_aktivitet_debit_fee_account,
        'accounting_aktivitet_credit_account': current_forening.accounting_aktivitet_credit_account,
    })

    context = {
        'current_forening': current_forening,
        'parents_choices': parents_choices,
        'form': form,
        'form_zipcode_area': form_zipcode_area,
        'admin_user_search_char_length': settings.ADMIN_USER_SEARCH_CHAR_LENGTH,
    }

    # Give URLPicker the homepage site for the current forening. If they don't
    # have one, just use the central site
    relevant_site = current_forening.get_homepage_site() or Site.get_central()
    context.update(url_picker_context(relevant_site))

    if request.method == 'GET':
        return render(request, 'central/admin/foreninger/edit.html', context)

    elif request.method == 'POST':
        form = EditForeningForm(request.user, request.POST)
        if form.is_valid():
            forening = form.cleaned_data['forening']
            forening.parents = form.cleaned_data['parents']
            forening.focus_id = form.cleaned_data['focus_id']
            forening.name = form.cleaned_data['name']
            forening.type = form.cleaned_data['type']
            if forening.type == 'turgruppe':
                forening.group_type = form.cleaned_data['group_type']
            else:
                forening.group_type = ''
            forening.description = form.cleaned_data['description']
            forening.post_address = form.cleaned_data['post_address']
            forening.visit_address = form.cleaned_data['visit_address']
            forening.zipcode = form.cleaned_data['zipcode']
            forening.counties = form.cleaned_data['counties']

            if form.cleaned_data['choose_contact'] == 'person':
                forening.contact_person = form.cleaned_data['contact_person']
            else:
                forening.contact_person = ''

            forening.phone = form.cleaned_data['phone']
            forening.email = form.cleaned_data['email']

            forening.organization_no = form.cleaned_data['organization_no']
            forening.gmap_url = form.cleaned_data['gmap_url']
            forening.facebook_url = form.cleaned_data['facebook_url']
            forening.homepage_url = form.cleaned_data['homepage_url']
            forening.offers_family_membership = form.cleaned_data['offers_family_membership']
            forening.aktivitet_signup_terms_url = form.cleaned_data['aktivitet_signup_terms_url']

            forening.handles_child_payments = form.cleaned_data['handles_child_payments']
            forening.stripe_forening = form.cleaned_data['stripe_forening']
            forening.accounting_software = form.cleaned_data['accounting_software']
            forening.accounting_aktivitet_department = form.cleaned_data['accounting_aktivitet_department']
            forening.accounting_aktivitet_debit_account = form.cleaned_data['accounting_aktivitet_debit_account']
            forening.accounting_aktivitet_debit_fee_account = form.cleaned_data['accounting_aktivitet_debit_fee_account']
            forening.accounting_aktivitet_credit_account = form.cleaned_data['accounting_aktivitet_credit_account']

            forening.save()
            messages.info(request, 'forening_save_success')
            cache.delete('foreninger.all.sorted_by_name')
            cache.delete('foreninger.all.sorted_by_type')
            cache.delete('foreninger.rendered_select')
            cache.delete('forening.%s' % forening.id)
            cache.delete('forening.main_foreninger.%s' % forening.id)
            return redirect('admin:foreninger.edit', current_forening.id)
        else:
            context.update({'form': form})
            return render(
                request,
                'central/admin/foreninger/edit.html',
                context)

    else:
        return redirect('admin:foreninger.edit', current_forening.id)
Esempio n. 8
0
def create(request, forening_id):
    current_forening = Forening.objects.get(id=forening_id)

    if current_forening not in request.user.all_foreninger():
        raise PermissionDenied

    # The parent choices are tricky to define in the forms API, so do it here.
    # Note that we're intentionally letting users choose parents among only
    # those they have permission to.
    all_sorted = request.user.all_foreninger_sorted()
    parents_choices = {
        'forening': all_sorted['forening'],
        'turlag': all_sorted['turlag'],
    }

    # If the parent of the current forening isn't in the user's permissions, we
    # still need to include that one as an available parent so that they're
    # able to make changes.
    if current_forening.type != 'sentral':
        for current_parent in current_forening.get_main_foreninger():
            if (current_parent not in parents_choices['forening'] and
                    current_parent not in parents_choices['turlag']):
                parents_choices[current_parent.type].append(current_parent)

    form = CreateForeningForm(request.user, initial={
        'zipcode': '',
    })

    context = {
        'current_forening': current_forening,
        'parents_choices': parents_choices,
        'admin_user_search_char_length': settings.ADMIN_USER_SEARCH_CHAR_LENGTH,
        'form': form,
    }

    # Give URLPicker the homepage site for the current forening. If they don't
    # have one, just use the central site
    relevant_site = current_forening.get_homepage_site() or Site.get_central()
    context.update(url_picker_context(relevant_site))

    if request.method == 'GET':
        return render(request, 'central/admin/foreninger/create.html', context)

    elif request.method == 'POST':

        form = CreateForeningForm(request.user, request.POST)
        if form.is_valid():
            forening = Forening()
            forening.name = form.cleaned_data['name']
            forening.type = form.cleaned_data['type']
            if forening.type == 'turgruppe':
                forening.group_type = form.cleaned_data['group_type']
            else:
                forening.group_type = ''

            forening.save()

            # Set M2M-fields after the initial db-save
            forening.parents = form.cleaned_data['parents']

            # Ensure the Turbasen object is re-saved with applicable
            # M2M-relations
            forening.save_turbasen_object()

            # Add the current user as admin on the new forening
            request.user.add_perm(
                'sherpa/association/user',
                association_id=forening.id,
                created_by=request.user
            )
            request.user.add_perm(
                'sherpa/association/admin',
                association_id=forening.id,
                created_by=request.user
            )
            cache.clear()
            return redirect('admin:foreninger.edit', forening.id)
        else:
            context.update({'form': form})
            return render(
                request,
                'central/admin/foreninger/create.html',
                context)

    else:
        return redirect('admin:foreninger.create', current_forening.id)
Esempio n. 9
0
def create(request, forening_id):
    current_forening = Forening.objects.get(id=forening_id)

    if current_forening not in request.user.all_foreninger():
        raise PermissionDenied

    # The parent choices are tricky to define in the forms API, so do it here.
    # Note that we're intentionally letting users choose parents among only those they have permission to.
    all_sorted = request.user.all_foreninger_sorted()
    parents_choices = {
        'forening': all_sorted['forening'],
        'turlag': all_sorted['turlag'],
    }

    # If the parent of the current forening isn't in the user's permissions, we still need to include that one as an
    # available parent so that they're able to make changes.
    if current_forening.type != 'sentral':
        for current_parent in current_forening.get_main_foreninger():
            if current_parent not in parents_choices['forening'] and current_parent not in parents_choices['turlag']:
                parents_choices[current_parent.type].append(current_parent)

    form = CreateForeningForm(request.user, initial={
        'zipcode': '',
    })

    context = {
        'current_forening': current_forening,
        'parents_choices': parents_choices,
        'admin_user_search_char_length': settings.ADMIN_USER_SEARCH_CHAR_LENGTH,
        'form': form,
    }

    # Give URLPicker the homepage site for the current forening. If they don't have one, just use the central site
    relevant_site = current_forening.get_homepage_site() or Site.get_central()
    context.update(url_picker_context(relevant_site))

    if request.method == 'GET':
        return render(request, 'central/admin/foreninger/create.html', context)

    elif request.method == 'POST':

        form = CreateForeningForm(request.user, request.POST)
        if form.is_valid():
            forening = Forening()
            forening.name = form.cleaned_data['name']
            forening.type = form.cleaned_data['type']
            if forening.type == 'turgruppe':
                forening.group_type = form.cleaned_data['group_type']
            else:
                forening.group_type = ''

            forening.save()

            # Set M2M-fields after the initial db-save
            forening.parents = form.cleaned_data['parents']

            # Ensure the Turbasen object is re-saved with applicable M2M-relations
            forening.save_turbasen_object()

            # Add the current user as admin on the new forening
            role = ForeningRole(
                user=request.user,
                forening=forening,
                role='admin',
            )
            role.save()
            # FIXME: We can't delete the permission cache for all users, so some may find that they can't edit
            # the new forening even though they should be able, until the cache (which is currently 24h) expires.
            cache.delete('user.%s.all_foreninger' % request.user.id)

            messages.info(request, 'forening_create_success')
            request.session['active_forening'] = forening.id
            cache.delete('foreninger.all.sorted_by_name')
            cache.delete('foreninger.all.sorted_by_type')
            cache.delete('foreninger.rendered_select')
            cache.delete('forening.%s' % forening.id)
            cache.delete('forening.main_foreninger.%s' % forening.id)
            return redirect('admin:foreninger.edit', forening.id)
        else:
            context.update({'form': form})
            return render(request, 'central/admin/foreninger/create.html', context)

    else:
        return redirect('admin:foreninger.create', current_forening.id)