Ejemplo n.º 1
0
 def add_members(self, request):
     """Grants organization membership to a list of users"""
     organization = self.get_object()
     if not organization.is_owned_by(
             request.user) and not request.user.is_staff:
         messages.error(request,
                        'You cannot add members this organization.')
         return
     form = AddMembersForm(request.POST)
     if form.is_valid():
         new_members = form.cleaned_data['members']
         new_member_count = len(new_members)
         existing_member_count = organization.members.count()
         if new_member_count + existing_member_count > organization.max_users:
             difference = (new_member_count +
                           existing_member_count) - organization.max_users
             seat = 'seats' if difference > 1 else 'seat'
             messages.error(
                 request,
                 'You will need to purchase %d %s.' % (difference, seat))
             return
         if not organization.active:
             messages.error(
                 request,
                 'You may not add members to an inactive organization.')
             return
         members_added = 0
         for member in new_members:
             try:
                 if organization.add_member(member):
                     new_action(request.user,
                                'added',
                                action_object=member,
                                target=organization)
                     logging.info('%s %s %s to %s.', request.user, 'added',
                                  member, organization)
                     members_added += 1
             except AttributeError as exception:
                 messages.error(request, exception)
         if members_added > 0:
             members_plural = 'members' if members_added > 1 else 'member'
             messages.success(
                 request,
                 'You added %d %s.' % (members_added, members_plural))
     return
Ejemplo n.º 2
0
    def get_context_data(self, **kwargs):
        """Add extra context data"""
        context = super(OrganizationDetailView,
                        self).get_context_data(**kwargs)
        organization = context['organization']
        context['is_staff'] = False
        context['is_owner'] = False
        context['is_member'] = False
        user = self.request.user
        if user.is_authenticated():
            context['is_staff'] = user.is_staff
            context['is_owner'] = organization.is_owned_by(user)
            context['is_member'] = user.profile.is_member_of(organization)
        requests = (
            FOIARequest.objects.organization(organization).get_viewable(user))
        context['requests'] = {
            'count': requests.count(),
            'filed': requests.order_by('-composer__datetime_submitted')[:10],
            'completed': requests.get_done().order_by('-datetime_done')[:10]
        }

        context['members'] = organization.members.select_related('user')
        num_requests = organization.get_requests()
        context['available'] = {
            'requests': num_requests,
            'seats': organization.max_users - len(context['members'])
        }
        context['progress'] = {}
        if organization.monthly_requests > 0:
            context['progress']['requests'] = (
                float(num_requests) / organization.monthly_requests) * 100
        else:
            context['progress']['requests'] = 0
        if organization.max_users > 0:
            context['progress']['seats'] = (
                1.0 -
                float(len(context['members'])) / organization.max_users) * 100
        else:
            context['progress']['seats'] = 0

        try:
            date_update = organization.date_update
            refresh_date = datetime.date(date_update.year,
                                         date_update.month + 1, 1)
        except ValueError:
            # ValueError should happen if the current month is December
            refresh_date = datetime.date(date_update.year + 1, 1, 1)
        context['refresh_date'] = refresh_date
        context['add_members_form'] = AddMembersForm()
        context['sidebar_admin_url'] = reverse(
            'admin:organization_organization_change', args=(organization.pk, ))
        return context