Exemplo n.º 1
0
def add_team_member_js(request):
    """
    Add a user as a team member of the current user
    """
    response = {}
    status = 405
    new_member = False
    if request.is_ajax() and request.method == 'POST':
        status = 202
        user_string = request.POST['user_string']
        if "@" in user_string and "." in user_string:
	    email_address = EmailAddress.objects.filter(email=user_string)
	    if len(email_address) > 0:
		email_address = email_address[0]
		new_member = email_address.user
	else:
	    users = User.objects.filter(username=user_string)
	    if len(users) > 0:
		new_member = users[0]
	if new_member:
	    if new_member.pk is request.user.pk:
                response['error'] = 1 #'You cannot add yourself to your contacts!'
            else:
                form_data = {
                    'leader': request.user.pk,
                    'member': new_member.pk
                }
                team_member_form = TeamMemberForm(form_data)
                if team_member_form.is_valid():
                    team_member_form.save()
                    the_avatar = accountutil.get_user_avatar_url(new_member)
                    response['member'] = {
                        'id': new_member.pk,
                        'name': new_member.username,
                        'email': new_member.email,
                        'avatar': the_avatar
                    }
                    status = 201
                else:
                    response['error'] = 2 #'This person is already in your contacts!'
        else:
	    response['error'] = 3 #'User cannot be found'
        
    return HttpResponse(
        json.dumps(response),
        content_type = 'application/json; charset=utf8',
        status=status
    )
Exemplo n.º 2
0
def add_team_member_js(request):
    """
    Add a user as a team member of the current user
    """
    response = {}
    status = 405
    new_member = False
    if request.is_ajax() and request.method == 'POST':
        status = 202
        user_string = request.POST['user_string']
        if "@" in user_string and "." in user_string:
            email_address = EmailAddress.objects.filter(email=user_string)
            if len(email_address) > 0:
                email_address = email_address[0]
                new_member = email_address.user
        else:
            users = User.objects.filter(username=user_string)
            if len(users) > 0:
                new_member = users[0]
        if new_member:
            if new_member.pk is request.user.pk:
                response[
                    'error'] = 1  #'You cannot add yourself to your contacts!'
            else:
                form_data = {
                    'leader': request.user.pk,
                    'member': new_member.pk
                }
                team_member_form = TeamMemberForm(form_data)
                if team_member_form.is_valid():
                    team_member_form.save()
                    the_avatar = accountutil.get_user_avatar_url(new_member)
                    response['member'] = {
                        'id': new_member.pk,
                        'name': new_member.username,
                        'email': new_member.email,
                        'avatar': the_avatar
                    }
                    status = 201
                else:
                    response[
                        'error'] = 2  #'This person is already in your contacts!'
        else:
            response['error'] = 3  #'User cannot be found'

    return HttpResponse(json.dumps(response),
                        content_type='application/json; charset=utf8',
                        status=status)
Exemplo n.º 3
0
def change_team_member_roles_js(request):
    """
    Change the roles of a team member
    """
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        form_data = simplejson.loads(request.POST['form_data'])
        form_data['leader'] = request.user.pk
        member=User.objects.get(pk=form_data['member'])
        team_member_object_instance = request.user.leader.filter(member=member)[0]
        team_member_form = TeamMemberForm(form_data, instance=team_member_object_instance)
        if team_member_form.is_valid():
            team_member_form.save()
            status = 200
    return HttpResponse(
        simplejson.dumps(response),
        content_type = 'application/json; charset=utf8',
        status=status
    )
Exemplo n.º 4
0
def change_team_member_roles_js(request):
    """
    Change the roles of a team member
    """
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        form_data = simplejson.loads(request.POST['form_data'])
        form_data['leader'] = request.user.pk
        member = User.objects.get(pk=form_data['member'])
        team_member_object_instance = request.user.leader.filter(
            member=member)[0]
        team_member_form = TeamMemberForm(form_data,
                                          instance=team_member_object_instance)
        if team_member_form.is_valid():
            team_member_form.save()
            status = 200
    return HttpResponse(simplejson.dumps(response),
                        content_type='application/json; charset=utf8',
                        status=status)
Exemplo n.º 5
0
def add_team_member_js(request):
    """
    Add a user as a team member
    """
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        status = 202
        try:
            new_member = User.objects.get(email=request.POST['email'])
            if new_member.pk is request.user.pk :
                response['error'] = 'You cannot add yourself into your contacts!'
            else :
                form_data = {
                    'leader': request.user.pk,
                    'member': new_member.pk
                }
                team_member_form = TeamMemberForm(form_data)
                if team_member_form.is_valid():
                    team_member_form.save()
                    the_avatar = accountutil.get_user_avatar_url(new_member)
                    response['member'] = {
                        'id': new_member.pk,
                        'name': new_member.username,
                        'email': new_member.email,
                        'avatar': the_avatar
                    }
                    status = 200
                else:
                    response['error'] = 'This member is already in the contacts!'
        except ObjectDoesNotExist:
            response['error'] = 'This e-mail is not registered!'
            pass
        
    return HttpResponse(
        simplejson.dumps(response),
        content_type = 'application/json; charset=utf8',
        status=status
    )
Exemplo n.º 6
0
def add_team_member_js(request):
    """
    Add a user as a team member
    """
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        status = 202
        try:
            new_member = User.objects.get(email=request.POST['email'])
            if new_member.pk is request.user.pk:
                response[
                    'error'] = 'You cannot add yourself into your contacts!'
            else:
                form_data = {
                    'leader': request.user.pk,
                    'member': new_member.pk
                }
                team_member_form = TeamMemberForm(form_data)
                if team_member_form.is_valid():
                    team_member_form.save()
                    the_avatar = accountutil.get_user_avatar_url(new_member)
                    response['member'] = {
                        'id': new_member.pk,
                        'name': new_member.username,
                        'email': new_member.email,
                        'avatar': the_avatar
                    }
                    status = 200
                else:
                    response[
                        'error'] = 'This member is already in the contacts!'
        except ObjectDoesNotExist:
            response['error'] = 'This e-mail is not registered!'
            pass

    return HttpResponse(simplejson.dumps(response),
                        content_type='application/json; charset=utf8',
                        status=status)