Esempio n. 1
0
def update_template_assignment(db_obj_project, request):
    if not code_shared.validate_form(request, [
        {
            'type': 'string',
            'keys': ['name'],
            'message': 'Invalid name'
        },
    ]):
        return

    template = None
    if request.POST['html_template'].strip() == '':
        if 'file_template' in request.FILES:
            if request.FILES['file_template'].charset == None:
                template = request.FILES['file_template'].read().decode(
                    'utf-8')
            else:
                template = request.FILES['file_template'].read().decode(
                    request.FILES['file_template'].charset)
    else:
        template = request.POST['html_template']

    try:
        if template == None:
            m_Template_Assignment.objects.filter(id=request.POST['id']).update(
                name=request.POST['name'], )
        else:
            m_Template_Assignment.objects.filter(id=request.POST['id']).update(
                name=request.POST['name'], template=template)
    except IntegrityError:
        messages.error(request, 'A template with this name already exists')
        return

    messages.success(request, 'Updated template successfully')
Esempio n. 2
0
def add_message_reject(db_obj_project, request):
    if not code_shared.validate_form(request, [
        {
            'type': 'string',
            'keys': ['message'],
            'message': 'Invalid message'
        },
    ]):
        return

    m_Message_Reject.objects.create(fk_project=db_obj_project,
                                    message=request.POST['message'])

    messages.success(request, 'Added reject message successfully')
Esempio n. 3
0
def update_message_reject(db_obj_project, request):
    if not code_shared.validate_form(request, [
        {
            'type': 'string',
            'keys': ['message'],
            'message': 'Invalid message'
        },
    ]):
        return

    m_Message_Reject.objects.filter(id=request.POST['id']).update(
        message=request.POST['message'], )

    messages.success(request, 'Updated reject message successfully')
Esempio n. 4
0
def update_template(db_obj_project, request):
    if not code_shared.validate_form(request, [
        {
            'type': 'string',
            'keys': ['name'],
            'message': 'Invalid name'
        },
        {
            'type': 'number',
            'keys': ['height_frame'],
            'message': 'Invalid frame height'
        },
    ]):
        return

    print(request.POST)
    db_obj_template = m_Template.objects.get(id=request.POST['id'])

    if 'template_assignment' in request.POST and request.POST[
            'template_assignment'].strip() != '':
        template_assignment = m_Template_Assignment.objects.get(
            id=request.POST['template_assignment'])
    else:
        template_assignment = db_obj_template.fk_template_assignment

    if 'template_hit' in request.POST and request.POST['template_hit'].strip(
    ) != '':
        template_hit = m_Template_Hit.objects.get(
            id=request.POST['template_hit'])
    else:
        template_hit = db_obj_template.fk_template_hit

    db_obj_template.name = request.POST['name']
    db_obj_template.height_frame = request.POST['height_frame']
    db_obj_template.fk_template_assignment = template_assignment
    db_obj_template.fk_template_hit = template_hit

    try:
        db_obj_template.save()
    except IntegrityError:
        messages.error(request, 'A template with this name already exists')
        return

    messages.success(request, 'Updated template successfully')
Esempio n. 5
0
def add_template_hit(db_obj_project, request):
    if not code_shared.validate_form(request, [
        {
            'type': 'string',
            'keys': ['name'],
            'message': 'Invalid name'
        },
        {
            'type': 'template',
            'keys': ['html_template', 'file_template'],
            'message': 'Invalid template'
        },
    ]):
        return

    template = None
    if request.POST['html_template'].strip() == '':
        if 'file_template' in request.FILES:
            if request.FILES['file_template'].charset == None:
                template = request.FILES['file_template'].read().decode(
                    'utf-8')
            else:
                template = request.FILES['file_template'].read().decode(
                    request.FILES['file_template'].charset)
    else:
        template = request.POST['html_template']

    try:
        m_Template_Hit.objects.create(name=request.POST['name'],
                                      template=template,
                                      fk_project=db_obj_project)
    except IntegrityError:
        messages.error(request, 'A template with this name already exists')
        return

    messages.success(request, 'Added template successfully')
Esempio n. 6
0
def create_batch(db_obj_project, request):
    if not code_shared.validate_form(request, [
        {
            'type': 'number',
            'keys': ['count_assignments'],
            'message': 'Invalid number of assignments'
        },
        {
            'type': 'number',
            'keys': ['lifetime'],
            'message': 'Invalid lifetime'
        },
        {
            'type': 'number',
            'keys': ['duration'],
            'message': 'Invalid duration'
        },
        {
            'type': 'string',
            'keys': ['reward'],
            'message': 'Invalid reward'
        },
        {
            'type': 'string',
            'keys': ['title'],
            'message': 'Invalid title'
        },
        {
            'type': 'string',
            'keys': ['description'],
            'message': 'Invalid description'
        },
        {
            'type': 'string',
            'keys': ['template'],
            'message': 'Invalid worker template'
        },
    ]):
        return

    if not 'file_csv' in request.FILES:
        valid = False
        messages.error(request, 'Invalid csv file')
        return

    db_obj_batch = code_shared.glob_create_batch(db_obj_project, request)
    client = code_shared.get_client(
        db_obj_project, True if request.POST['use_sandbox'] == '1' else False)
    reader = csv.DictReader(
        io.StringIO(request.FILES['file_csv'].read().decode('utf-8')))
    db_obj_template = m_Template.objects.get(fk_project=db_obj_project,
                                             id=request.POST['template'])
    # list_entities = []
    for dict_parameters in reader:
        try:
            mturk_obj_hit = client.create_hit(
                Keywords=request.POST['keywords'],
                MaxAssignments=int(request.POST['count_assignments']),
                LifetimeInSeconds=int(request.POST['lifetime']),
                AssignmentDurationInSeconds=int(request.POST['duration']),
                Reward=request.POST['reward'],
                Title=request.POST['title'],
                Description=request.POST['description'],
                Question=code_shared.create_question(
                    db_obj_template.template, db_obj_template.height_frame,
                    dict_parameters))
            # print(mturk_obj_hit)
        except ClientError as e:
            messages.error(
                request, '''
                An error occured
                <a href="#alert_1" data-toggle="collapse" class="alert-link">details</a>
                <p class="collapse mb-0" id="alert_1">
                    {}
                </p>
            '''.format(e))
            db_obj_batch.delete()
            return

        db_obj_tag = m_Tag.objects.create(name=glob_prefix_name_tag_hit +
                                          mturk_obj_hit['HIT']['HITId'],
                                          key_corpus=db_obj_project.name)

        # print(mturk_obj_hit)
        db_obj_hit = m_Hit.objects.create(
            # id_hit=str(random.randint(0, 9999999)),
            id_hit=mturk_obj_hit['HIT']['HITId'],
            fk_batch=db_obj_batch,
            parameters=json.dumps(dict_parameters),
            datetime_expiration=mturk_obj_hit['HIT']['Expiration'],
            datetime_creation=mturk_obj_hit['HIT']['CreationTime'],
        )

        # list_assignments

        # list_entities.append(db_obj_hit.id)

    db_obj_tag = m_Tag.objects.get_or_create(name=glob_prefix_name_tag_batch +
                                             db_obj_batch.name,
                                             key_corpus=db_obj_project.name)[0]

    messages.success(request, 'Created batch successfully')
Esempio n. 7
0
def update_settings(db_obj_project, request):
    code_shared.validate_form(request, [
        {
            'type': 'string',
            'keys': ['title'],
            'message': 'Invalid title',
            'state': 'warning'
        },
        {
            'type': 'string',
            'keys': ['description'],
            'message': 'Invalid description',
            'state': 'warning'
        },
        {
            'type': 'string',
            'keys': ['reward'],
            'message': 'Invalid reward',
            'state': 'warning'
        },
        {
            'type': 'number',
            'keys': ['count_assignments'],
            'message': 'Invalid number of assignments',
            'state': 'warning'
        },
        {
            'type': 'number',
            'keys': ['lifetime'],
            'message': 'Invalid hit lifetime',
            'state': 'warning'
        },
        {
            'type': 'number',
            'keys': ['duration'],
            'message': 'Invalid hit duration',
            'state': 'warning'
        },
        {
            'type': 'string',
            'keys': ['use_sandbox'],
            'message': 'Invalid sandbox mode',
            'state': 'warning'
        },
        {
            'type': 'string',
            'keys': ['template_main'],
            'message': 'Invalid main template',
            'state': 'warning'
        },
    ])

    db_obj_project.title = request.POST['title']
    db_obj_project.description = request.POST['description']
    db_obj_project.keywords = request.POST['keywords']
    db_obj_project.reward = request.POST['reward']
    db_obj_project.lifetime = request.POST['lifetime']
    db_obj_project.duration = request.POST['duration']
    db_obj_project.count_assignments = request.POST['count_assignments']
    db_obj_project.use_sandbox = True if request.POST[
        'use_sandbox'] == '1' else False

    if request.POST['message_reject_default'] != '':
        db_obj_project.fk_message_reject_default = m_Message_Reject.objects.get(
            fk_project=db_obj_project,
            id=request.POST['message_reject_default'])
    else:
        db_obj_project.fk_message_reject_default = None

    if request.POST['template_main'] != '':
        db_obj_project.fk_template_main = m_Template.objects.get(
            fk_project=db_obj_project, id=request.POST['template_main'])
    else:
        db_obj_project.fk_template_main = None

    if request.POST['template_assignment_main'] != '':
        db_obj_project.fk_template_assignment_main = m_Template_Assignment.objects.get(
            fk_project=db_obj_project,
            id=request.POST['template_assignment_main'])
    else:
        db_obj_project.fk_template_assignment_main = None

    if request.POST['template_hit_main'] != '':
        db_obj_project.fk_template_hit_main = m_Template_Hit.objects.get(
            fk_project=db_obj_project, id=request.POST['template_hit_main'])
    else:
        db_obj_project.fk_template_hit_main = None

    db_obj_project.save()

    messages.success(request, 'Updated settings successfully')
Esempio n. 8
0
def add_template(db_obj_project, request):
    if not code_shared.validate_form(request, [
        {
            'type': 'string',
            'keys': ['name'],
            'message': 'Invalid name'
        },
        {
            'type': 'number',
            'keys': ['height_frame'],
            'message': 'Invalid frame height'
        },
        {
            'type': 'template',
            'keys': ['html_template', 'file_template'],
            'message': 'Invalid template'
        },
    ]):
        return

    template = None
    if request.POST['html_template'].strip() == '':
        if 'file_template' in request.FILES:
            if request.FILES['file_template'].charset == None:
                template = request.FILES['file_template'].read().decode(
                    'utf-8')
            else:
                template = request.FILES['file_template'].read().decode(
                    request.FILES['file_template'].charset)
    else:
        template = request.POST['html_template']

    try:
        db_obj_template_assignment = m_Template_Assignment.objects.get(
            fk_project=db_obj_project, id=request.POST['template_assignment'])
    except ValueError:
        db_obj_template_assignment = m_Template_Assignment.objects.get(
            fk_project=db_obj_project,
            name="default_template_assignment__" + db_obj_project.name)

    try:
        db_obj_template_hit = m_Template_Hit.objects.get(
            fk_project=db_obj_project, id=request.POST['template_hit'])
    except ValueError:
        db_obj_template_hit = m_Template_Hit.objects.get(
            fk_project=db_obj_project,
            name="default_template_hit__" + db_obj_project.name)

    try:
        m_Template.objects.create(
            name=request.POST['name'],
            template=template,
            height_frame=request.POST['height_frame'],
            fk_project=db_obj_project,
            fk_template_assignment=db_obj_template_assignment,
            fk_template_hit=db_obj_template_hit)
    except IntegrityError:
        messages.error(request, 'A template with this name already exists')
        return

    messages.success(request, 'Added template successfully')