Exemplo n.º 1
0
    def clean(self):
        cleaned_data = super(TeamForm, self).clean()

        #   We've been passed both org & new_org, *sigh*
        if ('organisation' in cleaned_data and
                cleaned_data['new_organisation'] != ''):
                    raise forms.ValidationError({
                            "organisation": [self.error_both_fields]
                        }
                    )

        #   We've been passed neither, *double sigh*
        if ('organisation' not in cleaned_data and
                cleaned_data['new_organisation'] == ''):
                    raise forms.ValidationError({
                            "organisation": [self.error_neither_field]
                        }
                    )

        #   We've been passed a new organisation
        if ('organisation' not in cleaned_data and
                cleaned_data['new_organisation'] != ''):

            #   Check if the team name already exists
            t_n = cleaned_data.get('name')
            check_team = Team.objects.filter(name=t_n).exists()

            #   If it doesn't then we carry on dealing with the new org
            if check_team is False:

                #   Grab the new org name
                o_n = cleaned_data.get('new_organisation')

                #   See if it exists
                check_org = \
                    Organisation.objects.filter(name=o_n).exists()

                #   If it does, then we just grab it, otherwise create it
                if check_org is True:
                    new_organisation = Organisation.objects.get(name=o_n)
                else:
                    new_organisation = Organisation()
                    new_organisation.name = cleaned_data['new_organisation']
                    new_organisation.save()

                #   put the record into the organisation field, which *should*
                #   contain a valid organisaiton record
                cleaned_data['organisation'] = new_organisation

        #   Just to be neat & tidy, remove the new_organisation field
        del cleaned_data['new_organisation']
        return cleaned_data
Exemplo n.º 2
0
    def form_valid(self, form):
        userDetails = form.save(commit=False)

        #   Don't do any of this is the user isn't currently logged in
        #   or different to the currently logged in user
        if self.request.user.is_authenticated() is False or userDetails.id != self.request.user.id:
            return HttpResponseRedirect(self.get_success_url())

        #   If we have been passed a name then we have come from the
        #   user details form, if we don't have a name, then we are dealing
        #   with teams.
        if form.data.get("name") is not None:
            userDetails.save()
        else:
            user = User.objects.get(pk=userDetails.pk)
            #   Now we need to dump all the current links to teams and
            #   then add them all back in.
            user.teams.clear()
            for team in form.data.getlist("team"):
                user.teams.add(int(team))

            #   We need to see if we have been passed over a new team name
            #   if so then we have a bunch of work to do around adding that
            #   team
            team_name = form.data.get("teamname")
            if team_name is not None and team_name is not "":
                new_organisation_name = form.data.get("new_organisation")
                organisation_id = form.data.get("organisation")

                #   Now check to see if this team is using an existing
                #   organisation or a new_organisation.
                #   If it a new organisation then we need to create it.
                if new_organisation_name is not None and new_organisation_name is not "":
                    check_org = Organisation.objects.filter(name=new_organisation_name).exists()
                    if check_org is True:
                        new_organisation = Organisation.objects.get(name=new_organisation_name)
                    else:
                        new_organisation = Organisation()
                        new_organisation.name = new_organisation_name
                        new_organisation.save()
                else:
                    #   Otherwise we are going to use the organisation we
                    #   have been passed over.
                    check_org = Organisation.objects.filter(pk=organisation_id).exists()
                    if check_org is True:
                        new_organisation = Organisation.objects.get(pk=organisation_id)
                    else:
                        # TODO: Raise an error here to display on the form
                        return self.render_to_response(self.get_context_data())

                #   Either way we now have a new_organisation object that we
                #   can use to create the team.
                check_team = Team.objects.filter(name=team_name).exists()

                if check_team is True:
                    new_team = Team.objects.filter(name=team_name)
                else:
                    new_team = Team(name=team_name, organisation=new_organisation)
                    new_team.save()

                #   Now add the new team to the teams join on the user
                user.teams.add(new_team.pk)
                user.save()

        #   If the user wants to add another team, do that here
        #   TODO: add a #team thingy to the URL so we can jump down to the
        #   teams section
        submit_action = form.data.get("submit_action")
        if submit_action is not None and submit_action is not "":
            if submit_action in ["Save and add a new team", "Save and manage team membership"]:
                return HttpResponseRedirect(reverse("user-update-teams", kwargs={"slug": self.request.user.slug}))

        #   Normally we'd just go back to their profile page. So we'll do
        #   that here.
        return HttpResponseRedirect(self.get_success_url())
Exemplo n.º 3
0
    def form_valid(self, form):
        userDetails = form.save(commit=False)

        #   Don't do any of this is the user isn't currently logged in
        #   or different to the currently logged in user
        if (self.request.user.is_authenticated() is False or
                userDetails.id != self.request.user.id):
            return HttpResponseRedirect(self.get_success_url())

        #   If we have been passed a name then we have come from the
        #   user details form, if we don't have a name, then we are dealing
        #   with teams.
        if form.data.get('name') is not None:
            userDetails.save()
        else:
            user = User.objects.get(pk=userDetails.pk)
            #   Now we need to dump all the current links to teams and
            #   then add them all back in.
            user.teams.clear()
            for team in form.data.getlist('team'):
                user.teams.add(int(team))

            #   We need to see if we have been passed over a new team name
            #   if so then we have a bunch of work to do around adding that
            #   team
            team_name = form.data.get('teamname')
            if (team_name is not None and team_name is not ''):
                new_organisation_name = form.data.get('new_organisation')
                organisation_id = form.data.get('organisation')

                #   Now check to see if this team is using an existing
                #   organisation or a new_organisation.
                #   If it a new organisation then we need to create it.
                if (new_organisation_name is not None and
                        new_organisation_name is not ''):
                    check_org = Organisation.objects.filter(
                        name=new_organisation_name
                    ).exists()
                    if check_org is True:
                        new_organisation = Organisation.objects.get(
                            name=new_organisation_name
                        )
                    else:
                        new_organisation = Organisation()
                        new_organisation.name = new_organisation_name
                        new_organisation.save()
                else:
                    #   Otherwise we are going to use the organisation we
                    #   have been passed over.
                    check_org = Organisation.objects.filter(
                        pk=organisation_id).exists()
                    if check_org is True:
                        new_organisation = Organisation.objects.get(
                            pk=organisation_id
                        )
                    else:
                        # TODO: Raise an error here to display on the form
                        return self.render_to_response(self.get_context_data())

                #   Either way we now have a new_organisation object that we
                #   can use to create the team.
                check_team = Team.objects.filter(name=team_name).exists()

                if check_team is True:
                    new_team = Team.objects.filter(name=team_name)
                else:
                    new_team = Team(
                        name=team_name,
                        organisation=new_organisation
                    )
                    new_team.save()

                #   Now add the new team to the teams join on the user
                user.teams.add(new_team.pk)
                user.save()

        #   If the user wants to add another team, do that here
        #   TODO: add a #team thingy to the URL so we can jump down to the
        #   teams section
        submit_action = form.data.get('submit_action')
        if (submit_action is not None and submit_action is not ''):
            if submit_action in ['Save and add a new team',
                                 'Save and manage team membership']:
                return HttpResponseRedirect(
                    reverse(
                        'user-update-teams',
                        kwargs={
                            'slug': self.request.user.slug
                        }
                    )
                )

        #   Normally we'd just go back to their profile page. So we'll do
        #   that here.
        return HttpResponseRedirect(self.get_success_url())