Ejemplo n.º 1
0
    def handle(self, *args, **options):
        if 'persona' in options and options['persona']:
            try:
                persona_id = int(options['persona'])
            except ValueError:
                print 'The persona ID must be an integer'
                return

            personas = Persona.objects.filter(pk=persona_id)
            if not personas:
                print 'Persona with ID #%d was not found' % persona_id
        else:
            personas = Persona.objects.all()

        # get anonymizer configuration
        config = get_active_configuration()

        # update all personas
        for persona in personas:
            t = datetime.datetime.now()
            # get a user manager
            user_manager = config.get_user_manager(token=persona.uuid)

            # update & save persona
            try:
                persona.update_users(user_manager)
                persona.save()

                # log
                t2 = datetime.datetime.now()
                print 'Persona #%d updated in %s' % (persona.pk, str(t2 - t))
            except:
                print 'Persona #%d could not be updated' % persona.pk

        print '%d personas updated successfully' % len(personas)
    def handle(self, *args, **options):
        if 'persona' in options and options['persona']:
            try:
                persona_id = int(options['persona'])
            except ValueError:
                print 'The persona ID must be an integer'
                return

            personas = Persona.objects.filter(pk=persona_id)
            if not personas:
                print 'Persona with ID #%d was not found' % persona_id
        else:
            personas = Persona.objects.all()

        # get anonymizer configuration
        config = get_active_configuration()

        # update all personas
        for persona in personas:
            t = datetime.datetime.now()
            # get a user manager
            user_manager = config.get_user_manager(token=persona.uuid)

            # update & save persona
            try:
                persona.update_users(user_manager)
                persona.save()

                # log
                t2 = datetime.datetime.now()
                print 'Persona #%d updated in %s' % (persona.pk, str(t2 - t))
            except:
                print 'Persona #%d could not be updated' % persona.pk
                
        print '%d personas updated successfully' % len(personas)
Ejemplo n.º 3
0
def info(request):
    """
    Returns information about the properties supported in the creation of the persona
    """
    if request.method == 'GET':
        config = get_active_configuration()
        return JsonResponse(config.get_user_manager().list_filters(ignore_options=True), safe=False)
    else:
        return JsonResponse({'error': 'Only GET method allowed'}, status=400)
Ejemplo n.º 4
0
def update_query(p, data):
    if 'query' in data:
        # set query
        config = get_active_configuration()
        user_manager = config.get_user_manager(token=p.uuid)
        p_form = PersonaPropertiesForm(user_manager, data)
        if not p_form.is_valid():
            return JsonResponse({'error': p_form.errors}, status=400, safe=False)

        p.uuid = uuid.uuid4()
        user_manager.reset_token(p.uuid)
        p.query = p_form.cleaned_data['query']

        # generate users according to query
        p.update_users(user_manager)

    # save changes & show full persona view
    p.is_ready = True
    p.save()
Ejemplo n.º 5
0
def find_user(request):
    """
    :param request: Must contain actual user pk and the project pk in request GET
    :return: The anonymized user info from the first persona where user was found
    """
    if request.method != 'GET':
        return HttpResponse('Only GET method allowed', status=400)

    # get user & project id
    uid = request.GET.get('user')
    if not uid:
        return HttpResponse('`user` field is required', status=400)

    try:
        uid = int(uid)
    except ValueError:
        return HttpResponse('`user` must be the user ID ("%s" not an int)' % uid, status=400)

    pid = request.GET.get('project')
    if not pid:
        return HttpResponse('`project` field is required', status=400)

    try:
        pid = int(pid)
    except ValueError:
        return HttpResponse('`project` must be the project ID ("%s" is not an int)' % pid, status=400)

    res = PersonaUsers.objects.filter(user_id=uid, persona__project_id=pid).exclude(persona__owner='SYSTEM')
    if not res:
        res = PersonaUsers.objects.filter(user_id=uid, persona__project_id=pid, persona__owner='SYSTEM')

    # user not in system persona yet
    try:
        if not res:
            try:
                project_persona = Persona.objects.filter(project_id=pid, persona__owner='SYSTEM')[0]
            except IndexError:
                project_persona = Persona.objects.create(project_id=pid, owner='SYSTEM', query='',
                                                         description='Generic project persona',
                                                         is_ready=True, is_public=False)

            anonymized_user_info = get_active_configuration().get_user_manager(token=project_persona.uuid).get(pk=uid)
            pu = PersonaUsers.objects.create(user_id=uid, persona=project_persona, info=json.dumps(anonymized_user_info))
        else:
            # get the record
            pu = res[0]

        # no need to load/parse json for better performance
        user_info = pu.info[:-1] + ', "persona": %d}' % pu.persona_id
    except (UserManagerException, IndexError):
        # user does not exist any more - deleted user account?

        # use unique seed per user/project
        random.seed('%s-%s' % (pid, uid))

        # just fake the very basic info
        faked_info = {
            'first_name': first_name(),
            'last_name': last_name_initial(),
            'deleted': True,
        }
        
        # return as json
        user_info = json.dumps(faked_info)

    return HttpResponse(user_info, content_type='application/json')