예제 #1
0
def show(request, user_id):
    try:
        other_user = User.objects.filter(
            id=user_id,
        ).prefetch_related(
            'turleder_active_foreninger',
        ).get()
    except User.DoesNotExist:
        messages.warning(request, 'no_such_user')
        return redirect('admin:users.index')

    # Admins can assign user/admin, users can assign users
    assignable_admin = [a for a in request.user.all_foreninger() if a.role == 'admin']
    assignable_user = [a for a in request.user.all_foreninger() if a.role == 'user']
    # Don't let users assign new permissions for those that already have user status
    # Use ForeningRole for other_user, because we can't set permissions for foreninger that are
    # based on parent foreninger (to remove access to a child, you have to remove admin-permission
    # to the parent)
    other_user_foreninger = Forening.objects.filter(users=other_user)
    assignable_user = [a for a in assignable_user if a not in other_user_foreninger]
    assignable_foreninger = assignable_admin + assignable_user

    # Only admins can revoke forening relation
    revokable_foreninger = [a for a in assignable_admin if a in other_user_foreninger]

    today = date.today()

    # We can't just add 365*5 timedelta days because that doesn't account for leap years,
    # this does.
    try:
        five_years_from_now = date(year=(today.year + 5), month=today.month, day=today.day)
    except ValueError:
        # This will only occur when today is February 29th during a leap year (right?)
        five_years_from_now = date(year=(today.year + 5), month=today.month, day=(today.day - 1))

    context = {
        'other_user': other_user,
        'revokable_foreninger': Forening.sort(revokable_foreninger),
        'assignable_foreninger': Forening.sort(assignable_foreninger),
        'all_foreninger': Forening.get_all_sorted(),
        'turleder_roles': Turleder.TURLEDER_CHOICES,
        'five_years_from_now': five_years_from_now,
    }
    return render(request, 'central/admin/users/show/index.html', context)
예제 #2
0
 def children_foreninger_sorted(self):
     return Forening.sort(self.children_foreninger())
예제 #3
0
def show(request, user_id):
    try:
        other_user = User.objects.filter(
            id=user_id,
        ).prefetch_related(
            'turledere',
            'turleder_active_foreninger',
        ).get()
    except User.DoesNotExist:
        messages.warning(request, 'no_such_user')
        return redirect('admin:users.index')

    # Admins can assign user/admin, users can assign users
    assignable_admin = [
        a for a in request.user.all_foreninger() if a.role == 'admin'
    ]
    assignable_user = [
        a for a in request.user.all_foreninger() if a.role == 'user'
    ]
    # Don't let users assign new permissions for those that already have user
    # status.
    # Use UserPermissionGrant for other_user, because we can't set permissions
    # for foreninger that are based on parent foreninger (to remove access to
    # a child, you have to remove admin-permission to the parent)
    p_name = 'sherpa/association/user'
    other_user_foreninger = Forening.objects.filter(
        user_permission_grants__user_permission__name=p_name,
        user_permission_grants__user=other_user
    )

    assignable_user = [
        a for a in assignable_user if a not in other_user_foreninger
    ]
    assignable_foreninger = assignable_admin + assignable_user

    # Only admins can revoke forening relation
    revokable_foreninger = [
        a for a in assignable_admin if a in other_user_foreninger
    ]

    today = date.today()

    # We can't just add 365*5 timedelta days because that doesn't account for
    # leap years, this does.
    try:
        five_years_from_now = date(
            year=(today.year + 5),
            month=today.month,
            day=today.day
        )
    except ValueError:
        # This will only occur when today is February 29th during a leap year
        # (right?)
        five_years_from_now = date(
            year=(today.year + 5),
            month=today.month,
            day=(today.day - 1)
        )

    # Select participant groups where user is signee or participant
    aktivitet_data = get_aktivitet_list_participant_groups(other_user)

    # Sort by user / other
    aktiviteter_user = {
        'past': [a for a in aktivitet_data['past'] if a['user_is_signed_up']],
        'future': [
            a for a in aktivitet_data['future'] if a['user_is_signed_up']],
    }
    aktiviteter_other = {
        'past': [
            a for a in aktivitet_data['past'] if not a['user_is_signed_up']],
        'future': [
            a for a in aktivitet_data['future'] if not a['user_is_signed_up']],
    }

    # Get aktiviteter from montis
    montis_error = False
    montis_data = None
    if other_user.is_member():
        montis_data = get_aktivitet_list_from_montis(other_user)
        montis_error = montis_data.get('montis_error', False)

        if (len(montis_data['past']) + len(montis_data['future'])) == 0:
            montis_data = None
        else:
            montis_data['past'].sort(key=lambda d: d['start_date'])
            montis_data['future'].sort(key=lambda d: d['start_date'])

    aktiviteter_count = (
        len(aktivitet_data['past']) + len(aktivitet_data['future']))
    if montis_data:
        aktiviteter_count += (
            len(montis_data['past']) + len(montis_data['future']))

    # Select dates where user is turleder
    turleder_dates = other_user.turleder_aktivitet_dates.all()
    turleder_dates = {
        'future': [d for d in turleder_dates if d.start_date >= date.today()],
        'past': [d for d in turleder_dates if d.start_date < date.today()],
    }

    context = {
        'other_user': other_user,
        'revokable_foreninger': Forening.sort(revokable_foreninger),
        'assignable_foreninger': Forening.sort(assignable_foreninger),
        'all_foreninger': Forening.get_all_sorted(),
        'turleder_roles': Turleder.TURLEDER_CHOICES,
        'five_years_from_now': five_years_from_now,
        'aktiviteter_user': aktiviteter_user,
        'aktiviteter_other': aktiviteter_other,
        'montis_data': montis_data,
        'aktiviteter_count': aktiviteter_count,
        'turleder_dates': turleder_dates,
        'montis_error': montis_error,
        'consent_pretty': json.dumps(other_user.consent, indent=2),
        'consent': other_user.get_consent_preferences(),
        'consent_json': json.dumps(other_user.consent),
    }

    if other_user.is_member() and other_user.can_reserve_against_publications():
        context.update({
            'enable_publications_toggle': True,
            'can_reserve_against_publications': other_user.can_reserve_against_publications(),
            'reserved_against_fjellogvidde': other_user.get_reserved_against_fjellogvidde(),
            'reserved_against_yearbook': other_user.get_reserved_against_yearbook(),
        })

    return render(request, 'central/admin/users/show/index.html', context)
예제 #4
0
 def all_foreninger_sorted(self):
     return Forening.sort(self.all_foreninger())