Example #1
0
def trait(request, output=None):
    whitelist = trait_whitelist.clear()
    # Translate the input
    args = whitelist.process(request.GET)

    # Check the input for anyproblems
    whitelist.leftover(PersOALeftoverField)
    output.error(whitelist.errors())

    # Find the Trait
    trait = None

    results = WhooshIndex.get(INDEX_DIR).search(
        name=[args['trait_name']],
        type=WhooshIndex.CLASSES['trait'],
        page=1,
        pagelen=1
    )
    if len(results):
        cls = WhooshIndex.CLASSES['index'][results[0]['type']]

        trait = (cls.objects
            .select_related()
            .get(id=results['results'][0]['id'])
        )
    else:
        output.error(PersOANotFound())

    # Generate a choice
    include = args[Whitelist.INCLUDE_NAME]
    generated = {
        'choices': [
            i.details(include)
            for i in trait.generate(num=args['num'], seed=args['seed'])
        ],
    }
    if include['trait']:
        generated['trait'] = trait.details(include)
    # Format the choice
    output.output(generated)
Example #2
0
def group(request, output=None):
    whitelist = group_whitelist.clear()
    # Translate the input
    args = whitelist.process(request.GET)

    # Check the input for any problems
    whitelist.leftover(PersOALeftoverField)
    output.error(whitelist.errors())

    # Find the Group
    group = None

    results = WhooshIndex.get(INDEX_DIR).search(
        name=[args['group_name']],
        type=WhooshIndex.CLASSES['group'],
        page=1,
        pagelen=1
    )
    if len(results):
        cls = WhooshIndex.CLASSES['index'][results['results'][0]['type']]

        group = (cls.objects
            .select_related()
            .get(id=results['results'][0]['id'])
        )
    else:
        output.error(PersOANotFound())

    # Generate a choice
    include = args[Whitelist.INCLUDE_NAME]
    generated = {
        'choices': group.generate(
            args['num'],
            args['seed'],
            include
    )}
    if include['group']:
        generated['group'] = group.details(include)
    output.output(generated)
Example #3
0
 def handle(self, *args, **options):
     index = WhooshIndex.get(INDEX_DIR)
     index.create_schema(flush=options['flush'])
Example #4
0
def find(request, output=None):
    # TODO add query
    whitelist.clear()
    # Translate the input
    kwargs = whitelist.process(request.GET)

    # Check the input for any problems
    whitelist.leftover(PersOALeftoverField)
    output.error(whitelist.errors())

    # Prepare the input
    if not(kwargs['query'] and kwargs['name'] and kwargs['desc'] and kwargs['type']):
        # If whoosh doesn't return all items then...?
        pass

    if not kwargs['type'] is None:
        types = kwargs['type']
        kwargs['type'] = set()
        for t in types:
            if t == 'all':
                kwargs['type'] = ALL_TYPES.values()
                break
            elif t in ALL_TYPES.keys():
                kwargs['type'].add(ALL_TYPES[t])
            elif t == 'trait':
                kwargs['type'] = args['type'] | WhooshIndex.CLASSES['trait']
            elif t == 'choice':
                kwargs['type'] = args['type'] | WhooshIndex.CLASSES['choice']

    # Find the Items
    results = WhooshIndex.get(INDEX_DIR).search(**kwargs)

    types = {}
    for hit in results['results']:
        if not hit['type'] in types.keys():
            types[hit['type']] = []
        types[hit['type']].append(hit)

    # Get the items from the database and format them
    include = kwargs[Whitelist.INCLUDE_NAME]
    print include
    found = {'all': []}
    for cls in types.keys():
        items = (WhooshIndex.CLASSES['index'][cls].objects
            .select_related()
            .filter(id__in=[i['id'] for i in types[cls]])
        )
        items = [i.details(include) for i in items]

        # TODO: properly name this
        found[cls] = items
        found['all'] += items

    if include['combine']:
        found.pop('all')

    # Format the result
    found['page'] = results['page']
    found['pages'] = results['pages']
    found['total'] = results['total']
    output.output(found)
Example #5
0
from django.views.decorators.http import require_GET

from app.constants.index import INDEX_DIR, ALL_TYPES
from app.errors import PersOARequiredFieldError, PersOANotFound, PersOALeftoverField
from app.models.choice import BasicChoice, LinearChoice, SubChoice
from app.models.group import TraitGroup
from app.models.trait import BasicTrait, LinearTrait
from app.views.field import Field
from app.views.whitelist import Whitelist
from app.views.search import WhooshIndex
from app.views.sanitize import json_return, persoa_output

# Do any preparatory work before starting requests
WhooshIndex.get(INDEX_DIR)

############################################################
# Find
############################################################
whitelist = (Whitelist()
    .add(Field(['query'], 'query', basestring)
        .default(None)
    )
    .add(Field(['name'], 'name', basestring)
        .default(None)
        .setting(Field.SETTINGS_LIST)
    )
    .add(Field(['desc'], 'desc', basestring)
        .default(None)
        .setting(Field.SETTINGS_LIST)
    )
    .add(Field(['type'], 'type', basestring)