Beispiel #1
0
 def remove_membership_request(self, user, group):
     """
     The administrator of the group removes
     the request to join the group.
     """
     self.delete()
     cache_bust([('requests', self.to_administrator.pk)])
     return True
Beispiel #2
0
 def unmark_viewed_membership_request(self, user, group):
     """
     The administrator can be manually unmark
     as viewed the membership request.
     """
     if self.viewed:
         self.viewed = ''
         self.saved()
         cache_bust([('requests', self.to_administrator.pk)])
         return True
Beispiel #3
0
 def remove_sent_request(self, user):
     """
     The administrator of the group removes
     the request to join the group.
     """
     if user.is_authenticated() and user == self.from_user:
         self.delete()
         cache_bust([('sent_requests', user.pk)])
         return True
     return False
Beispiel #4
0
 def remove_group(self, user):
     """
     Remove selected group by its administrator.
     """
     group_pk = self.pk
     response = group_and_membership_remove.send(sender=self.__class__,
                                                 user=user,
                                                 group=self)
     receiver, deleted = response[0]
     if deleted:
         cache_bust([('groups', user.pk), ('memberships', group_pk)])
         return True
     return False
Beispiel #5
0
 def reject_membership_request(self, user, group):
     """
     The administrator of the group rejects
     the request to join the group.
     """
     if not self.rejected:
         self.rejected = timezone.now()
         self.save()
         membership_request_rejectd.send(sender=self.__class__,
                                         user=self.from_user,
                                         request=self)
         cache_bust([('requests', self.to_administrator.pk)])
         return True
Beispiel #6
0
 def mark_viewed_membership_request(self, user, group):
     """
     The membership request if marked as viewed
     the first time administrator reads it.
     Also can be manually marked and unmark as viewed.
     """
     if not self.viewed:
         self.viewed = timezone.now()
         self.saved()
         membership_request_viewed.send(sender=self.__class__,
                                        user=self.from_user,
                                        request=self)
         cache_bust([('requests', self.to_administrator.pk)])
         return True
Beispiel #7
0
 def accept_membership_request(self, user, group):
     """
     The administrator of the group accepts
     the request to join the group.
     """
     membership, created = GroupMembership.objects.get_or_create(
         member=self.from_user, group=self.group, permit='PART')
     if created:
         self.remove_membership_request(user)
         membership_request_accepted.send(sender=self.__class__,
                                          user=self.from_user,
                                          request=self)
         cache_bust([('memberships', group.pk)])
         return membership
Beispiel #8
0
 def send_membership_request(self, from_user, to_admin, group, message):
     """
     Send membership request to private group administrator to join.
     After trying to join a private group the user is redirected to
     membership request form if the group is private once checked the
     user is not already a group member.
     The membership request form saving executes this method.
     """
     if user.is_authenticated():
         request, created = self.get_or_create(from_user=from_user, to_administrator=to_admin, group=group, message=message)
         if not created:
             raise SendRequestError('Membership request for this group has already been sent.')
         cache_bust([('requests', to_admin.pk), ('sent_requests', from_user.pk)])
         membership_request_sent.send(sender=self.model.__class__)
         return request
     return False
Beispiel #9
0
 def create_new_group(self, user, name, access='PRIVATE'):
     """
     Create a new group defined by its name and access type.
     Group access is set as private ('PRIV') by default.
     When created send signal to set administrator permit to the group creator.
     """
     if not self.filter(name=name).exists():
         group, created = self.get_or_create(name=name, access=access)
         if created:
             response = group_created.send(sender=self.model.__class__, user=user, group=group)
             receiver, administrator = response[0]
             cache_bust([('groups', user.pk)])
             return group, administrator
         return False
     else:
         raise GroupError('Already exists a group with name \'{name}\''.format(name=name))
Beispiel #10
0
 def remove_membership(self, user):
     """
     Delete user's membership to a group.
     User can leave the group or be removed by the
     group administrator.
     If the administrator leaves the group the group
     is deleted.
     """
     if user.is_authenticated() and self.objects.is_member(user):
         administrator = self.objects.get_group_admin(self.group)
         if user == self.member != administrator or user == administrator:
             group = self.group
             self.delete()
             cache_bust([('groups', user.pk), ('memberships', group.pk)])
             return True
         elif user == self.member == administrator:
             response = Group.objects.remove_group(user)
             return response
     return False
Beispiel #11
0
 def add_membership(self, user, group, permit='PART'):
     """
     New group membership.
     If the group is public the user joins it automatically, whereas
     if it is private the user must be admitted by the group administrator
     via requeset approval.
     """
     if self.is_member(user, group):
         raise GroupMembershipError('User is already member of this group.')
     if group.access == 'PUBLIC':
         membership, created = self.get_or_create(member=user, group=group, permit=permit)
         if created:
             membership_created.send(sender=self.model)
             cache_bust([('groups', user.pk), ('memberships', group.pk)])
             return reverse('group:group_detail', kwargs={'group_id': group.pk})
         else:
             raise GroupMembershipError('Error creating group membership.')
     elif group.access == 'PRIVATE':
         return reverse('group:membership_request', kwargs={'group_id': group.pk})
     else:
         raise GroupError('Group access has to be either PUBLIC or PRIVATE.')