def post(self, request, *args, **kwargs): """Save the changes to the user form.""" form = self.form_class(request.POST) if form.is_valid(): team_instance = form.save(commit=False) # team creator team_instance.created_by = request.user # when and by whom the project was created date_now = datetime.now() team_instance.created_date = date_now team_instance.created_by = request.user # when and by whom the project was last modified team_instance.last_modified_date = date_now team_instance.last_modified_by = request.user # save the project team_instance.save() # add a membership for the requesting user membership_instance = TeamMembership() membership_instance.added_date = date_now membership_instance.member = request.user membership_instance.team = team_instance membership_instance.is_owner = True membership_instance.can_edit = True membership_instance.save() return HttpResponseRedirect(reverse('team_manage', kwargs={'team_id': team_instance.id})) return render(request, self.template, locals())
def post(self, request, format=None): """Create a new team.""" serializer = TeamSerializer(data=request.data, context={'request': request}) if serializer.is_valid(): team = serializer.save() # add a membership for the requesting user membership_instance = TeamMembership() membership_instance.added_date = datetime.now() membership_instance.member = request.user membership_instance.team = team membership_instance.is_owner = True membership_instance.can_edit = True membership_instance.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): """Save the changes to the user form.""" ctx = {} ctx['params'] = request.POST ctx['command'] = ctx['params']["command"] if "command" in ctx['params'] else None ctx['team_id'] = kwargs["team_id"] if kwargs is not None and 'team_id' in kwargs else None if ctx['team_id'] is not None and ctx['command'] is not None: if ctx['command'] == 'adduser': # add a user membership to the team ctx['user_id'] = int(ctx['params']['user_id']) if 'user_id' in ctx['params'] else None if ctx['user_id'] is not None: # check if the user already has a membership, this can happen if the user reloads the page ctx['membership_list'] = TeamMembership.objects.filter( team_id=ctx['team_id'], member_id=ctx['user_id']).all() if ctx['membership_list'] is None or not ctx['membership_list']: # add a membership ctx['team_obj'] = Team.objects.get(id=ctx['team_id']) ctx['member_obj'] = User.objects.get(id=ctx['user_id']) ctx['membership'] = TeamMembership() ctx['membership'].added_date = datetime.now() ctx['membership'].team = ctx['team_obj'] ctx['membership'].member = ctx['member_obj'] ctx['membership'].can_edit = True ctx['membership'].is_owner = False ctx['membership'].save() else: ctx['error_msg'] = "Invalid user id specified." elif ctx['command'] == 'deleteuser': # remove a user membership ctx['user_id'] = int(ctx['params']['user_id']) if 'user_id' in ctx['params'] else None if ctx['user_id'] is not None: # add a membership ctx['membership'] = TeamMembership.objects.get(team_id=ctx['team_id'], member_id=ctx['user_id']) if ctx['membership'] is not None: ctx['membership'].delete() else: ctx['error_msg'] = "Invalid user id specified." elif ctx['command'] == 'updatename': # update the team name ctx['team_obj'] = Team.objects.get(id=ctx['team_id']) if ctx['team_obj'] is not None: ctx['name'] = ctx['params']['name'] if 'name' in ctx['params'] else None ctx['name'] = ctx['name'].strip() if ctx['name'] is not None and ctx['name']: ctx['team_obj'].name = ctx['name'] ctx['team_obj'].save() else: ctx['error_msg'] = "Invalid team name." else: ctx['error_msg'] = "Invalid team id specified." # retrieve the updated data and render the view get_request = RequestFactory().get('/') get_request.user = request.user ctx['team_data'] = APITeamDetailView.as_view()( get_request, team_id=ctx['team_id'], format='json').rendered_content ctx['team'] = JSONParser().parse(BytesIO(ctx['team_data'])) ctx['nonmembers_data'] = APITeamMembershipListView.as_view()( get_request, team_id=ctx['team_id'], nonmember=1, format='json').rendered_content ctx['nonmembers'] = JSONParser().parse(BytesIO(ctx['nonmembers_data'])) return render(request, self.template, ctx) # we should never get here, so just redirect to the dashboard return HttpResponseRedirect(reverse('dashboard'))