Beispiel #1
0
 def put(self,request,list_id):
     list = get_list(list_id)
     if request.user.profile not in list.board.members.all():
         raise Http404
     data = request.data
     if 'board' in data:
         try:
             board = request.user.profile.member_in_boards.get(id=data['board'])
             data['board'] = board
         except:
             return Response({"detail": "Make sure you are allowed to move list to that board."},status=status.HTTP_400_BAD_REQUEST)
         if board.team != None:
             if request.profile.user not in board.admins.all():
                 return Response(status=status.HTTP_403_FORBIDDEN)
         mail_body = f"{request.user.profile.name} moved the list {list.name} to the board {list.board.name}."
         Activity.objects.create(description=f"{request.user.profile.name} moved {list.name} from {list.board.name} to {board.name}.",user=request.user.profile,board=board,list=list)
         Activity.objects.create(description=f"{request.user.profile.name} moved {list.name} from {list.board.name} to {board.name}.",user=request.user.profile,board=list.board,list=list)
     serializer = ListSerializer(list)
     serializer.update(instance=list,validated_data=data)
     mail_body = f"{request.user.profile.name} edited the list {list.name} in the board {list.board.name}."
     if 'archived' in data:
         if data['archive'] == True:
             mail_body = f"{request.user.profile.name} archived the list {list.name} in the board {list.board.name}."
             Activity.objects.create(description=f"{request.user.profile.name} archived {list.name}.",user=request.user.profile,board=list.board,list=list)
         else:
             Activity.objects.create(description=f"{request.user.profile.name} unarchived {list.name}.",user=request.user.profile,board=list.board,list=list)
             mail_body = f"{request.user.profile.name} unarchived the list {list.name} in the board {list.board.name}."
     mail_subject = f"{list.board.name}(Board)"
     send_email_to_object_watchers(list.board,mail_body,mail_subject)
     mail_subject = f"{list.name}(List)"
     send_email_to_object_watchers(list,mail_body,mail_subject)
     return Response({"detail": "list edited successfully"},status=status.HTTP_200_OK)
Beispiel #2
0
 def get(self, request, id):  #self join
     try:
         board = models.Board.objects.get(id=id)
     except:
         raise Http404
     if board.team is None or request.user.profile not in board.team.members.all(
     ):
         raise Http404
     user = request.user.profile
     if user in board.members.all():
         return Response(
             {"detail": "You are already a member of this board."},
             status=status.HTTP_400_BAD_REQUEST)
     if board.preference.self_join and board.preference.permission_level == models.Preference.permission.team_members:
         board.members.add(request.user.profile)
         # sending email notification
         mail_body = f"{user.name} joined the board {board.name}. You are receiving this email because you are watching the board {board.name}. If you don't want to receive such emails, you can unwatch the board."
         mail_subject = f"{board.name}(Board)"
         send_email_to_object_watchers(board, mail_body, mail_subject)
         #------
         serializer = serializers.BoardSerializer(
             board, context={'request': request})
         # activity creation
         Activity.objects.create(description="{} joined {}.".format(
             user.name, board.name),
                                 board=board,
                                 user=user)
         return Response(serializer.data, status=status.HTTP_200_OK)
     else:
         return Response(
             {"detail": "You dont have permission of joining this board"},
             status=status.HTTP_403_FORBIDDEN)
Beispiel #3
0
 def put(self, request, id):  # leave board
     board = get_board(request, id)
     if board.members.count() == 1 or (request.user.profile
                                       in board.admins.all()
                                       and board.admins.count() == 1):
         return Response(
             {"detail": "board must have at least one member and admin"},
             status=status.HTTP_400_BAD_REQUEST)
     board.members.remove(request.user.profile)
     if request.user.profile in board.admins.all():
         board.admins.remove(request.user.profile)
     if not is_allowed_to_watch_or_star(request.user.profile, board):
         board.starred_by.remove(request.user.profile)
         board.watched_by.remove(request.user.profile)
         lists_to_unwatch = request.user.profile.watching_lists.filter(
             board=board)
         request.user.profile.starred_lists.remove(*lists_to_unwatch)
         cards_to_unwatch = request.user.profile.watching_cards.filter(
             list__board=board)
         request.user.profile.starred_lists.remove(*cards_to_unwatch)
     mail_body = f"{request.user.profile.name} left the board {board.name}.\n You are receiving this email because you are watching the board {board.name}. If you don't want to receive such emails, you can unwatch the board."
     mail_subject = f"{board.name}(Board)"
     send_email_to_object_watchers(board, mail_body, mail_subject)
     #activity creation
     Activity.objects.create(description="{} left {}.".format(
         request.user.profile.name, board.name),
                             user=request.user.profile,
                             board=board)
     return Response({"detail": "succesfully left board"},
                     status=status.HTTP_200_OK)
Beispiel #4
0
 def post(self, request, id):  #add multiple members
     board = get_board(request, id)
     if board.preference.pref_invitation == models.Preference.invitations.members or request.user.profile in board.admins.all(
     ):
         members = request.data.get("members", None)
         if members is not None:
             for member in members:
                 try:
                     user = models.UserProfile.objects.get(
                         user__email=member)
                 except:
                     print("user does not exist")
                     continue
                 if member in board.members.all():
                     continue
                 board.members.add(user)
                 mail_body = f"{request.user.profile.name} added you to board {board.name}.\n If you don't want to be in that board you can leave the board anytime."
                 mail_subject = f"You are invited to join board"
                 send_email_to(member, mail_body, mail_subject)
             mail_body = f"{request.user.profile.name} added some members to the board {board.name}.\n You are receiving this email because you are watching the board {board.name}. If you don't want to receive such emails, you can unwatch the board."
             mail_subject = f"{board.name}(Board)"
             send_email_to_object_watchers(board, mail_body, mail_subject)
             #activity creation
             Activity.objects.create(
                 description="{} added some members in {}.".format(
                     request.user.profile.name, board.name),
                 user=request.user.profile,
                 board=board)
             return Response({"detail": "Members were added"},
                             status=status.HTTP_200_OK)
         return Response(status=status.HTTP_400_BAD_REQUEST)
     return Response(status=status.HTTP_403_FORBIDDEN)
Beispiel #5
0
 def post(self,request,id,format=None):
     board = self.get_board(request,id)
     data = request.data
     data['board'] = board.id
     serializer = ListSerializer(data=data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     mail_body = f"{request.user.profile.name} created a list {data['name']} to the board {board.name}.\n You are receiving this email because you are watching the board {board.name}. If you don't want to receive such emails, you can unwatch the board."
     mail_subject = f"A list was added in {board.name}(Board)"
     send_email_to_object_watchers(board,mail_body,mail_subject)
     Activity.objects.create(description=f"{request.user.profile.name} created list {serializer.instance.name}",user=request.user.profile,board=board,list=serializer.instance)
     return Response(serializer.data,status=status.HTTP_201_CREATED)       
Beispiel #6
0
 def patch(self, request, id):  # remove member
     board = get_board(request, id)
     request_user_is_admin = bool(
         request.user.profile in board.admins.all())
     if board.preference.pref_invitation == models.Preference.invitations.members or request_user_is_admin:
         member = request.data.get("member", None)
         try:
             member = board.members.get(id=member)
         except:
             return Response({"detail": "No such member found."},
                             status=status.HTTP_400_BAD_REQUEST)
         member_is_admin = bool(member in board.admins.all())
         if board.members.count() == 1 or (member_is_admin
                                           and board.admins.count() == 1):
             return Response(
                 {
                     "detail":
                     "board must have at least one member and admin"
                 },
                 status=status.HTTP_400_BAD_REQUEST)
         if member_is_admin and not request_user_is_admin:
             return Response(
                 {
                     "detail":
                     "You are not allowed to remove an admin from board."
                 },
                 status=status.HTTP_403_FORBIDDEN)
         board.members.remove(member)
         if member in board.admins.all():
             board.admins.remove(member)
         if not is_allowed_to_watch_or_star(member, board):
             board.starred_by.remove(member)
             board.watched_by.remove(member)
             lists_to_unwatch = member.watching_lists.filter(board=board)
             member.starred_lists.remove(*lists_to_unwatch)
             cards_to_unwatch = member.watching_cards.filter(
                 list__board=board)
             member.starred_lists.remove(*cards_to_unwatch)
         mail_body = f"{request.user.profile.name} removed you from board {board.name}."
         mail_subject = "you were removed from a board"
         send_email_to(member.user.email, mail_body, mail_subject)
         mail_body = f"{member.name} was removed from the board {board.name}.\n You are receiving this email because you are watching the board {board.name}. If you don't want to receive such emails, you can unwatch the board."
         mail_subject = f"{board.name}(Board)"
         send_email_to_object_watchers(board, mail_body, mail_subject)
         #activity creation
         Activity.objects.create(
             description="{} removed {} from {}.".format(
                 request.user.profile.name, member.name, board.name),
             user=request.user.profile,
             board=board)
         return Response({"detail": "succesfully removed from board"},
                         status=status.HTTP_200_OK)
     return Response(status=status.HTTP_403_FORBIDDEN)
Beispiel #7
0
 def post(self, request, list_id):
     data = request.data
     list = self.get_list(request, list_id)
     if not (request.user.profile in list.board.members.all()):
         return Response(status=status.HTTP_403_FORBIDDEN)
     data["list"] = list.id
     data['members'] = None
     data['index'] = list.cards.count()
     serializer = serializers.CardSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     mail_body = f"{request.user.profile.name} created a card {data['name']} in list {list.name} in board {list.board.name}."
     mail_subject = f"{list.board.name}(Board)"
     send_email_to_object_watchers(list.board, mail_body, mail_subject)
     mail_subject = f"{list.name}(List)"
     send_email_to_object_watchers(list, mail_body, mail_subject)
     Activity.objects.create(
         description=
         f"{request.user.profile.name} created card {serializer.instance.name} in {list.name}.",
         user=request.user.profile,
         board=list.board,
         list=list)
     return Response(serializer.data, status=status.HTTP_201_CREATED)
Beispiel #8
0
 def put(self, request, card_id):
     data = request.data
     card = self.get_card(card_id)
     if not (request.user.profile in card.list.board.members.all()):
         return Response(status=status.HTTP_403_FORBIDDEN)
     mail_body = f"{request.user.profile.name} edited the card {card.name} in list {card.list.name} in board {card.list.board.name}."
     if 'name' in data:
         Activity.objects.create(
             description=
             f"{request.user.profile.name} renamed card {card.name} to {data['name']}.",
             user=request.user.profile,
             board=card.list.board,
             list=card.list,
             card=card)
         mail_body = f"{request.user.profile.name} renamed the card {card.name} to {data['name']} in list {card.list.name} in board {card.list.board.name}."
     elif 'desc' in data:
         Activity.objects.create(
             description=
             f"{request.user.profile.name} changed description of {card.name}.",
             user=request.user.profile,
             board=card.list.board,
             list=card.list,
             card=card)
         mail_body = f"{request.user.profile.name} changed description of {card.name} in list {card.list.name} in board {card.list.board.name}."
     elif 'due_date' in data:
         Activity.objects.create(
             description=
             f"{request.user.profile.name} changed due date of {card.name}.",
             user=request.user.profile,
             board=card.list.board,
             list=card.list,
             card=card)
         mail_body = f"{request.user.profile.name} changed due date of {card.name} in list {card.list.name} in board {card.list.board.name}."
     elif 'complete' in data:
         if data['complete']:
             Activity.objects.create(
                 description=
                 f"{request.user.profile.name} marked as complete {card.name}.",
                 user=request.user.profile,
                 board=card.list.board,
                 list=card.list,
                 card=card)
             mail_body = f"{request.user.profile.name} marked as complete {card.name} in list {card.list.name} in board {card.list.board.name}."
         else:
             Activity.objects.create(
                 description=
                 f"{request.user.profile.name} marked as uncomplete {card.name}.",
                 user=request.user.profile,
                 board=card.list.board,
                 list=card.list,
                 card=card)
             mail_body = f"{request.user.profile.name} marked as uncomplete {card.name} in list {card.list.name} in board {card.list.board.name}."
     elif 'archived' in data:
         if data['archived']:
             Activity.objects.create(
                 description=
                 f"{request.user.profile.name} archived {card.name}.",
                 user=request.user.profile,
                 board=card.list.board,
                 list=card.list,
                 card=card)
             mail_body = f"{request.user.profile.name} archived {card.name} in list {card.list.name} in board {card.list.board.name}."
         else:
             Activity.objects.create(
                 description=
                 f"{request.user.profile.name} unarchived {card.name}.",
                 user=request.user.profile,
                 board=card.list.board,
                 list=card.list,
                 card=card)
             mail_body = f"{request.user.profile.name} unarchived {card.name} in list {card.list.name} in board {card.list.board.name}."
     if 'list' in data:
         try:
             list = List.objects.get(id=data['list'])
             data['list'] = list
         except:
             return Reponse({"detail": "Couldn't find given list."},
                            status=status.HTTP_400_BAD_REQUEST)
         if not (request.user.profile in list.board.members.all()):
             return Response(status=status.HTTP_403_FORBIDDEN)
         if not (list in card.list.board.lists.all()):
             return Response(
                 {"detail": "Can't move card outside the board"},
                 status=status.HTTP_403_FORBIDDEN)
         if list == card.list:
             if 'index' in data:
                 if data['index'] > card.list.cards.count(
                 ) + 1 or data['index'] < 0:
                     return Reponse(
                         {"detail": "Index can't hold this value"},
                         status=status.HTTP_400_BAD_REQUEST)
                 if card.index > data['index']:
                     card.list.cards.filter(
                         index__gte=data['index'],
                         index__lt=card.index).update(index=F('index') + 1)
                 else:
                     card.list.cards.filter(
                         index__gt=card.index,
                         index__lte=data['index']).update(index=F('index') -
                                                          1)
         else:
             if 'index' in data:
                 if data['index'] > list.cards.count(
                 ) + 1 or data['index'] < 0:
                     return Reponse(
                         {"detail": "Index can't hold this value"},
                         status=status.HTTP_400_BAD_REQUEST)
                 list.cards.filter(index__gte=data['index']).update(
                     index=F('index') + 1)
                 card.list.cards.filter(index_gt=card.index).update(
                     index=F('index') - 1)
                 mail_body = f"{request.user.profile.name} moved card {card.name} from list {card.list.name} to list {list.name}."
                 Activity.objects.create(
                     description=
                     f"{request.user.profile.name} moved {card.name} from {card.list.name} to list {list.name}.",
                     user=request.user.profile,
                     board=list.board,
                     list=list,
                     card=card)
         send_email_to_object_watchers(card.list.board, mail_body,
                                       f"{card.list.board.name}(Board)")
         send_email_to_object_watchers(card.list, mail_body,
                                       f"{card.list.name}(List)")
     send_email_to_object_watchers(card, mail_body, f"{card.name}(Card)")
     serializer = serializers.CardSerializer(card)
     serializer.update(instance=card, validated_data=data)
     return Response({"detail": "card edited successfully"},
                     status=status.HTTP_200_OK)