Exemplo n.º 1
0
def add(request):

    if not require_login_or_key(request):
        return redirect('/')

    ssn = request.GET.get('ssn')
    username = request.GET.get('username')
    name = request.GET.get('name')
    email = request.GET.get('email')
    added = request.GET.get('added', '')
    groups = request.GET.getlist('group', [])

    member = Member()
    member.ssn = ssn
    member.username = username
    member.name = name
    member.email = email
    member.added = added

    try:
        member.save()
        member = Member.objects.get(id=member.id)
        for group in groups:
            member.groups.add(Group.objects.get(techname=group))        
    except IntegrityError as e:
        return json_error(e)

    response_data = {
        'success': True,
        'data': member_to_dict(member)
    }

    return HttpResponse(json.dumps(response_data), content_type='application/json')
Exemplo n.º 2
0
def get(request, field, searchstring):

    if not require_login_or_key(request):
        return redirect('/')

    try:
        if field == 'name':
            member = Member.objects.get(name=searchstring)
        elif field == 'username':
            member = Member.objects.get(username=searchstring)
        elif field == 'ssn':
            member = Member.objects.get(ssn=searchstring)
    except Member.DoesNotExist as e:
        return json_error('No such member')

    # Update our DB with data from the remote end
    # Currently ignored attributes: ssn, name, added
    try:
        username = request.GET.get('username')
        email = request.GET.get('email')
        if username or email:
            if username:
                member.username = username
            if email:
                # We trust the other guy to have verified the e-mail
                member.email = email
                member.email_verified = True
            member.save()
    except:
        # Update failed somehow, barf details to stderr but keep on truckin'
        #
        # In particular, this is likely to happen if the DBs are out of sync
        # and the email uniqueness constraints are violated.
        #
        traceback.print_exc(file=sys.stderr)

    response_data = {
        'success': True,
        'data': member_to_dict(member)
    }

    return HttpResponse(json.dumps(response_data), content_type='application/json')