예제 #1
0
def register(request):
    if not settings.OPPIA_ALLOW_SELF_REGISTRATION:
        raise Http404
    
    if request.method == 'POST': # if form submitted...
        form = RegisterForm(request.POST)
        if form.is_valid(): # All validation rules pass
            # Create new user
            username = form.cleaned_data.get("username")
            email = form.cleaned_data.get("email")
            password = form.cleaned_data.get("password")
            first_name = form.cleaned_data.get("first_name")
            last_name = form.cleaned_data.get("last_name")
            user = User.objects.create_user(username, email, password)
            user.first_name = first_name
            user.last_name = last_name
            user.save()
            user_profile = UserProfile()
            user_profile.user = user
            user_profile.job_title = form.cleaned_data.get("job_title")
            user_profile.organisation = form.cleaned_data.get("organisation")
            user_profile.save()
            u = authenticate(username=username, password=password)
            if u is not None:
                if u.is_active:
                    login(request, u)
                    return HttpResponseRedirect('thanks/')
            return HttpResponseRedirect('thanks/') # Redirect after POST
    else:
        form = RegisterForm(initial={'next':request.GET.get('next'),})

    return render_to_response('oppia/form.html', 
                              {'form': form, 
                               'title': _(u'Register'), },
                               context_instance=RequestContext(request),)
예제 #2
0
def edit(request, user_id=0):
    if user_id != 0:
        if can_edit_user(request, user_id):
            view_user = User.objects.get(pk=user_id)
        else:
            return HttpResponse('Unauthorized', status=401)
    else:
        view_user = request.user
    
    key = ApiKey.objects.get(user = view_user)
    if request.method == 'POST':
        form = ProfileForm(request.POST)
        if form.is_valid():
            # update basic data
            email = form.cleaned_data.get("email")
            first_name = form.cleaned_data.get("first_name")
            last_name = form.cleaned_data.get("last_name")
            view_user.email = email
            view_user.first_name = first_name
            view_user.last_name = last_name
            view_user.save()
            
            try:
                user_profile = UserProfile.objects.get(user=view_user)
                user_profile.job_title = form.cleaned_data.get("job_title")
                user_profile.organisation = form.cleaned_data.get("organisation")
                user_profile.save()
            except UserProfile.DoesNotExist:
                user_profile = UserProfile()
                user_profile.user = view_user
                user_profile.job_title = form.cleaned_data.get("job_title")
                user_profile.organisation = form.cleaned_data.get("organisation")
                user_profile.save()
            messages.success(request, _(u"Profile updated"))
            
            # if password should be changed
            password = form.cleaned_data.get("password")
            if password:
                view_user.set_password(password)
                view_user.save()
                messages.success(request, _(u"Password updated"))
    else:
        try:
            user_profile = UserProfile.objects.get(user=view_user)
        except UserProfile.DoesNotExist:
            user_profile = UserProfile()
        form = ProfileForm(initial={'username':view_user.username,
                                    'email':view_user.email,
                                    'first_name':view_user.first_name,
                                    'last_name':view_user.last_name,
                                    'api_key': key.key,
                                    'job_title': user_profile.job_title,
                                    'organisation': user_profile.organisation,})
        
    return render_to_response( 
                  'oppia/profile/profile.html', 
                  {'form': form,},
                  context_instance=RequestContext(request))
예제 #3
0
def register(request):
    if not settings.OPPIA_ALLOW_SELF_REGISTRATION:
        raise Http404

    if request.method == 'POST':  # if form submitted...
        form = RegisterForm(request.POST)
        form.fields['location'].choices = get_location_choices()
        if form.is_valid():  # All validation rules pass
            # Create new user
            username = form.cleaned_data.get("username")
            email = form.cleaned_data.get("email")
            password = form.cleaned_data.get("password")
            first_name = form.cleaned_data.get("first_name")
            last_name = form.cleaned_data.get("last_name")
            user = User.objects.create_user(username, email, password)
            user.first_name = first_name
            user.last_name = last_name
            user.save()

            try:
                facility = Facility.objects.get(
                    pk=form.cleaned_data.get("location"))
            except Facility.DoesNotExist:
                facility = None

            user_profile = UserProfile()
            user_profile.user = user
            user_profile.job_title = form.cleaned_data.get("job_title")
            user_profile.organisation = form.cleaned_data.get("organisation")
            user_profile.profession = form.cleaned_data.get("profession")
            user_profile.service_entry_date = form.cleaned_data.get(
                "service_entry_date")
            user_profile.location = facility
            user_profile.save()
            u = authenticate(username=username, password=password)
            if u is not None:
                if u.is_active:
                    login(request, u)
                    return HttpResponseRedirect('thanks/')
            return HttpResponseRedirect('thanks/')  # Redirect after POST
    else:
        form = RegisterForm(initial={
            'next': filterRedirect(request.GET),
        })
        form.fields['location'].choices = get_location_choices()

    return render_to_response(
        'oppia/form.html',
        {
            'form': form,
            'title': _(u'Register'),
        },
        context_instance=RequestContext(request),
    )
예제 #4
0
def register(request):
    if not settings.OPPIA_ALLOW_SELF_REGISTRATION:
        raise Http404

    if request.method == 'POST':  # if form submitted...
        form = RegisterForm(request.POST)
        if form.is_valid():  # All validation rules pass
            # Create new user
            username = form.cleaned_data.get("username")
            email = form.cleaned_data.get("email")
            password = form.cleaned_data.get("password")
            first_name = form.cleaned_data.get("first_name")
            last_name = form.cleaned_data.get("last_name")
            user = User.objects.create_user(username, email, password)
            user.first_name = first_name
            user.last_name = last_name
            user.save()
            user_profile = UserProfile()
            user_profile.user = user
            user_profile.job_title = form.cleaned_data.get("job_title")
            user_profile.organisation = form.cleaned_data.get("organisation")
            user_profile.save()
            u = authenticate(username=username, password=password)
            if u is not None and u.is_active:
                login(request, u)
                return HttpResponseRedirect('thanks/')
            return HttpResponseRedirect('thanks/')  # Redirect after POST
    else:
        form = RegisterForm(initial={
            'next': filter_redirect(request.GET),
        })

    return render(request, 'oppia/form.html', {
        'form': form,
        'title': _(u'Register'),
    })
예제 #5
0
    def obj_create(self, bundle, **kwargs):
        if not settings.OPPIA_ALLOW_SELF_REGISTRATION:
            raise BadRequest(_(u'Registration is disabled on this server.'))
        required = [
            'username', 'password', 'passwordagain', 'email', 'firstname',
            'lastname'
        ]

        check_required_params(bundle, required)

        data = {
            'username': bundle.data['username'],
            'password': bundle.data['password'],
            'password_again': bundle.data['passwordagain'],
            'email': bundle.data['email'],
            'first_name': bundle.data['firstname'],
            'last_name': bundle.data['lastname'],
        }
        rf = RegisterForm(data)
        if not rf.is_valid():
            str = ""
            for key, value in rf.errors.items():
                for error in value:
                    str += error + "\n"
            raise BadRequest(str)
        else:
            username = bundle.data['username']
            password = bundle.data['password']
            email = bundle.data['email']
            first_name = bundle.data['firstname']
            last_name = bundle.data['lastname']
        try:
            bundle.obj = User.objects.create_user(username, email, password)
            bundle.obj.first_name = first_name
            bundle.obj.last_name = last_name
            bundle.obj.save()

            user_profile = UserProfile()
            user_profile.user = bundle.obj
            if 'jobtitle' in bundle.data:
                user_profile.job_title = bundle.data['jobtitle']
            if 'organisation' in bundle.data:
                user_profile.organisation = bundle.data['organisation']
            if 'phoneno' in bundle.data:
                user_profile.phone_number = bundle.data['phoneno']
            user_profile.save()

            u = authenticate(username=username, password=password)
            if u is not None and u.is_active:
                login(bundle.request, u)
                # Add to tracker
                tracker = Tracker()
                tracker.user = u
                tracker.type = 'register'
                tracker.ip = bundle.request.META.get(
                    'REMOTE_ADDR', oppia.api.DEFAULT_IP_ADDRESS)
                tracker.agent = bundle.request.META.get(
                    'HTTP_USER_AGENT', 'unknown')
                tracker.save()
            key = ApiKey.objects.get(user=u)
            bundle.data['api_key'] = key.key
        except IntegrityError:
            raise BadRequest(
                _(u'Username "%s" already in use, please select another' %
                  username))
        del bundle.data['passwordagain']
        del bundle.data['password']
        del bundle.data['firstname']
        del bundle.data['lastname']
        return bundle
예제 #6
0
 def obj_create(self, bundle, **kwargs):
     if not settings.OPPIA_ALLOW_SELF_REGISTRATION:
         raise BadRequest(_(u'Registration is disabled on this server.'))
     required = ['username','password','passwordagain', 'email', 'firstname', 'lastname']
     for r in required:
         try:
             bundle.data[r]
         except KeyError:
             raise BadRequest(_(u'Please enter your %s') % r)
     data = {'username': bundle.data['username'],
             'password': bundle.data['password'],
             'password_again': bundle.data['passwordagain'],
             'email': bundle.data['email'],
             'first_name': bundle.data['firstname'],
             'last_name': bundle.data['lastname'],}
     rf = RegisterForm(data)
     if not rf.is_valid():
         str = ""
         for key, value in rf.errors.items():
             for error in value:
                 str += error + "\n"
         raise BadRequest(str)
     else:
         username = bundle.data['username']
         password = bundle.data['password']
         email = bundle.data['email']
         first_name = bundle.data['firstname']
         last_name = bundle.data['lastname']
     try:
         bundle.obj = User.objects.create_user(username, email, password)
         bundle.obj.first_name = first_name
         bundle.obj.last_name = last_name
         bundle.obj.save()
         
         user_profile = UserProfile()
         user_profile.user = bundle.obj
         if 'jobtitle' in bundle.data:
             user_profile.job_title = bundle.data['jobtitle']
         if 'organisation' in bundle.data:
             user_profile.organisation = bundle.data['organisation']
         if 'phoneno' in bundle.data:
             user_profile.phone_number= bundle.data['phoneno']
         user_profile.save()
         
         u = authenticate(username=username, password=password)
         if u is not None:
             if u.is_active:
                 login(bundle.request, u)
                 # Add to tracker
                 tracker = Tracker()
                 tracker.user = u
                 tracker.type = 'register'
                 tracker.ip = bundle.request.META.get('REMOTE_ADDR','0.0.0.0')
                 tracker.agent =bundle.request.META.get('HTTP_USER_AGENT','unknown')
                 tracker.save()
         key = ApiKey.objects.get(user = u)
         bundle.data['api_key'] = key.key
     except IntegrityError:
         raise BadRequest(_(u'Username "%s" already in use, please select another' % username))
     del bundle.data['passwordagain']
     del bundle.data['password']
     del bundle.data['firstname']
     del bundle.data['lastname']
     return bundle   
예제 #7
0
def edit(request, user_id=0):
    if user_id != 0:
        if can_edit_user(request, user_id):
            view_user = User.objects.get(pk=user_id)
        else:
            return HttpResponse('Unauthorized', status=401)
    else:
        view_user = request.user

    key = ApiKey.objects.get(user=view_user)
    if request.method == 'POST':
        form = ProfileForm(request.POST)
        form.fields['location'].choices = get_location_choices()
        if form.is_valid():
            # update basic data
            email = form.cleaned_data.get("email")
            first_name = form.cleaned_data.get("first_name")
            last_name = form.cleaned_data.get("last_name")
            view_user.email = email
            view_user.first_name = first_name
            view_user.last_name = last_name
            view_user.save()

            try:
                facility = Facility.objects.get(
                    pk=form.cleaned_data.get("location"))
            except Facility.DoesNotExist:
                facility = None

            try:
                user_profile = UserProfile.objects.get(user=view_user)
                user_profile.job_title = form.cleaned_data.get("job_title")
                user_profile.organisation = form.cleaned_data.get(
                    "organisation")
                user_profile.profession = form.cleaned_data.get("profession")
                user_profile.service_entry_date = form.cleaned_data.get(
                    "service_entry_date")
                user_profile.location = facility
                user_profile.save()
            except UserProfile.DoesNotExist:
                user_profile = UserProfile()
                user_profile.user = view_user
                user_profile.job_title = form.cleaned_data.get("job_title")
                user_profile.organisation = form.cleaned_data.get(
                    "organisation")
                user_profile.profession = form.cleaned_data.get("profession")
                user_profile.service_entry_date = form.cleaned_data.get(
                    "service_entry_date")
                user_profile.location = facility
                user_profile.save()
            messages.success(request, _(u"Profile updated"))

            # if password should be changed
            password = form.cleaned_data.get("password")
            if password:
                view_user.set_password(password)
                view_user.save()
                messages.success(request, _(u"Password updated"))
    else:
        try:
            user_profile = UserProfile.objects.get(user=view_user)
        except UserProfile.DoesNotExist:
            user_profile = UserProfile()

        if user_profile.location is None:
            location = 0
        else:
            location = user_profile.location.id

        print location
        form = ProfileForm(
            initial={
                'username': view_user.username,
                'email': view_user.email,
                'first_name': view_user.first_name,
                'last_name': view_user.last_name,
                'api_key': key.key,
                'job_title': user_profile.job_title,
                'organisation': user_profile.organisation,
                'profession': user_profile.profession,
                'service_entry_date': user_profile.service_entry_date,
                'location': location,
            })
        form.fields['location'].choices = get_location_choices()

    return render_to_response('oppia/profile/profile.html', {
        'form': form,
    },
                              context_instance=RequestContext(request))
예제 #8
0
def edit(request, user_id=0):
    if user_id != 0:
        if can_edit_user(request, user_id):
            view_user = User.objects.get(pk=user_id)
        else:
            raise exceptions.PermissionDenied
    else:
        view_user = request.user

    key = ApiKey.objects.get(user=view_user)
    if request.method == 'POST':
        form = ProfileForm(request.POST)
        if form.is_valid():
            # update basic data
            email = form.cleaned_data.get("email")
            first_name = form.cleaned_data.get("first_name")
            last_name = form.cleaned_data.get("last_name")
            view_user.email = email
            view_user.first_name = first_name
            view_user.last_name = last_name
            view_user.save()

            try:
                user_profile = UserProfile.objects.get(user=view_user)
                user_profile.job_title = form.cleaned_data.get("job_title")
                user_profile.organisation = form.cleaned_data.get(
                    "organisation")
                user_profile.save()
            except UserProfile.DoesNotExist:
                user_profile = UserProfile()
                user_profile.user = view_user
                user_profile.job_title = form.cleaned_data.get("job_title")
                user_profile.organisation = form.cleaned_data.get(
                    "organisation")
                user_profile.save()
            messages.success(request, _(u"Profile updated"))

            # if password should be changed
            password = form.cleaned_data.get("password")
            if password:
                view_user.set_password(password)
                view_user.save()
                messages.success(request, _(u"Password updated"))
    else:
        try:
            user_profile = UserProfile.objects.get(user=view_user)
        except UserProfile.DoesNotExist:
            user_profile = UserProfile()
        form = ProfileForm(
            initial={
                'username': view_user.username,
                'email': view_user.email,
                'first_name': view_user.first_name,
                'last_name': view_user.last_name,
                'api_key': key.key,
                'job_title': user_profile.job_title,
                'organisation': user_profile.organisation,
            })

    return render(request, 'oppia/profile/profile.html', {
        'form': form,
    })