예제 #1
0
def index(request):
    query = request.GET.copy()

    # Default organizers: If visiting a local site, default to the owner of the current site (except for DNT central)
    if not request.GET.get("organizers", "") and request.site.forening.id != Forening.DNT_CENTRAL_ID:
        query["organizers"] = "%s:%s" % ("forening", request.site.forening.id)

    # Default minimum duration is always 1
    query["min_duration"] = query.get("min_duration", "1")

    filter, aktivitet_dates = filter_aktivitet_dates(query)
    aktivitet_dates_pagenav = paginate_aktivitet_dates(filter, aktivitet_dates)

    # Usually, the 'sentral' type is sorted first, but in this case we want it last
    all_foreninger = Forening.get_all_sorted()
    sentral = all_foreninger[0]
    if not sentral["code"] == "sentral":
        # We made an incorrect assumption, log it but try to continue rendering instead of raising an error
        logger.error(
            "Assumed first group of forening to be type 'sentral', was really '%s'" % sentral["code"],
            extra={"request": request, "all_foreninger": all_foreninger},
        )
    # Remove and append the sentral group to the end
    all_foreninger.remove(sentral)
    all_foreninger.append(sentral)

    # Look up DNT cabins with serving
    cabins = sorted(
        Sted.list(
            params={
                "tags": "Hytte",
                "betjeningsgrad": "Betjent",
                "privat": {"hytteeier": "DNT"},
                "status": "Offentlig",
                "tilbyder": "DNT",
            }
        ),
        key=lambda s: s["navn"],
    )

    context = {
        "aktivitet_dates": aktivitet_dates_pagenav,
        "difficulties": Aktivitet.DIFFICULTY_CHOICES,
        "categories": Aktivitet.CATEGORY_CHOICES,
        "category_types": Aktivitet.CATEGORY_TYPES_LIST,
        "audiences": Aktivitet.AUDIENCE_CHOICES,
        "omrader": sorted(Område.list(params={"status": "Offentlig", "tilbyder": "DNT"}), key=lambda o: o["navn"]),
        "cabins": cabins,
        "all_foreninger": all_foreninger,
        "filter": filter,
    }
    return render(request, "common/aktiviteter/index.html", context)
예제 #2
0
    def admin_context(self, site):
        # Look up DNT cabins with serving
        cabins = sorted(Sted.list(params={
            'tags': 'Hytte',
            'betjeningsgrad': 'Betjent',
            'grupper': '',
            'privat': {'hytteeier': 'DNT'},
            'status': 'Offentlig',
            'tilbyder': 'DNT',
        }), key=lambda s: s['navn'])

        return {
            'all_foreninger_sorted': Forening.get_all_sorted(),
            'audiences': Aktivitet.AUDIENCE_CHOICES,
            'categories': Aktivitet.CATEGORY_CHOICES,
            'category_types': [{
                'category': [c[1] for c in Aktivitet.CATEGORY_CHOICES if c[0] == category['category']][0],
                'types': category['types']
            } for category in Aktivitet.CATEGORY_TYPES_LIST],
            'cabins': cabins,
        }
예제 #3
0
def index(request):
    filter_ = DEFAULT_FILTER.copy()
    filter_.update(request.GET.dict())

    # Default organizers: If visiting a local site, default to the owner of the current site (except for DNT central)
    if not filter_['organizers'] and request.site.forening.id != Forening.DNT_CENTRAL_ID:
        filter_['organizers'] = '%s:%s' % ('forening', request.site.forening.id)

    # Interestingly, the paginator will perform a COUNT query to count the
    # total number of objects, regardless of whether it is used in the
    # template. Therefore, even if we get a cache hit for the listing template
    # fragment, it will perform the query unnecessarily. So explicitly check
    # if the current filter is cached or not, and if so, skip the pagination.
    cache_key = make_template_fragment_key(
        'aktivitet_listing',
        [filter_hash(filter_)],
    )
    if cache.get(cache_key) is not None:
        aktivitet_dates_pagenav = None
    else:
        filter_, aktivitet_dates = filter_aktivitet_dates(filter_)
        aktivitet_dates_pagenav = paginate_aktivitet_dates(filter_, aktivitet_dates)

    # Usually, the 'sentral' type is sorted first, but in this case we want it last
    all_foreninger = Forening.get_all_sorted()
    sentral = all_foreninger[0]
    if not sentral['code'] == 'sentral':
        # We made an incorrect assumption, log it but try to continue rendering instead of raising an error
        logger.error(
            "Assumed first group of forening to be type 'sentral', was really '%s'" % sentral['code'],
            extra={
                'request': request,
                'all_foreninger': all_foreninger,
            }
        )
    # Remove and append the sentral group to the end
    all_foreninger.remove(sentral)
    all_foreninger.append(sentral)

    # Look up DNT cabins with serving
    cabins = sorted(Sted.list(params={
        'tags': 'Hytte',
        'betjeningsgrad': 'Betjent',
        'privat': {'hytteeier': 'DNT'},
        'status': 'Offentlig',
        'tilbyder': 'DNT',
    }), key=lambda s: s['navn'])

    # Set url query parameters for pagination links
    query_string = get_filter_query_string(request.GET.dict())

    context = {
        'aktivitet_dates': aktivitet_dates_pagenav,
        'difficulties': Aktivitet.DIFFICULTY_CHOICES,
        'categories': Aktivitet.CATEGORY_CHOICES,
        'category_types': Aktivitet.CATEGORY_TYPES_LIST,
        'audiences': Aktivitet.AUDIENCE_CHOICES,
        'omrader': sorted(Område.list(params={'status': 'Offentlig', 'tilbyder': 'DNT'}), key=lambda o: o['navn']),
        'cabins': cabins,
        'all_foreninger': all_foreninger,
        'query_string': query_string,
        'filter': filter_,
        'filter_hash': filter_hash(filter_),
    }
    return render(request, 'common/aktiviteter/index.html', context)