Exemplo n.º 1
0
def _import_task(app, handler, form, render_forms):
    try:
        empty = True
        n = 0
        n_data = 0
        for task_data in handler.tasks(form):
            n_data += 1
            task = model.Task(app_id=app.id)
            [setattr(task, k, v) for k, v in task_data.iteritems()]
            data = db.session.query(model.Task).filter_by(app_id=app.id).filter_by(info=task.info).first()
            if data is None:
                db.session.add(task)
                db.session.commit()
                n += 1
                empty = False
        if empty and n_data == 0:
            raise importer.BulkImportException(
                gettext('Oops! It looks like the file is empty.'))
        if empty and n_data > 0:
            flash(gettext('Oops! It looks like there are no new records to import.'), 'warning')

        msg = str(n) + " " + gettext('Tasks imported successfully!')
        if n == 1:
            msg = str(n) + " " + gettext('Task imported successfully!')
        flash(msg, 'success')
        return redirect(url_for('.tasks', short_name=app.short_name))
    except importer.BulkImportException, err_msg:
        flash(err_msg, 'error')
Exemplo n.º 2
0
Arquivo: base.py Projeto: jun9/pybossa
    def create(cls):
        user = model.User(
                email_addr = cls.email_addr, 
                name = cls.name, 
                fullname = cls.fullname, 
                api_key = cls.api_key)

        user2 = model.User(
                email_addr = cls.email_addr2, 
                name = cls.name2, 
                fullname = cls.fullname2, 
                api_key=cls.api_key_2)

        info = {
            'total': 150,
            'long_description': 'hello world'
            }

        app = model.App(
                name = u'My New App',
                short_name = cls.app_name,
                description = u'description',
                hidden = 0,
                info = info
            )

        app.owner = user
        task_info = {
            'question': 'My random question',
            'url': 'my url'
            }
        task_run_info = {
            'answer': u'annakarenina'
            }

        model.Session.add_all([user, user2, app])

        # Create the task and taskruns for the first app
        for i in range (0,10):
            task = model.Task(app_id = 1, state = '0', info = task_info)
            task.app = app
            # Taskruns will be assigned randomly to a signed user or an anonymous one
            if random.randint(0,1) == 1:
                task_run = model.TaskRun(
                        app_id = 1, 
                        task_id = 1, 
                        user_id = 1, 
                        info = task_run_info)
                task_run.user = user
            else:
                task_run = model.TaskRun(
                        app_id = 1, 
                        task_id = 1, 
                        user_ip = '127.0.0.1', 
                        info = task_run_info)
            task_run.task = task
            model.Session.add_all([task, task_run])
        model.Session.commit()
        model.Session.remove()
Exemplo n.º 3
0
def new_task(app_id, user_id=None, user_ip=None, offset=0):
    '''Get a new task by calling the appropriate scheduler function.
    '''
    app = db.session.query(model.App).get(app_id)
    if not app.allow_anonymous_contributors and user_id is None:
        error = model.Task(info=dict(error="This application does not allow anonymous contributors"))
        return error
    else:
        sched_map = {
            'default': get_depth_first_task,
            'breadth_first': get_breadth_first_task,
            'depth_first': get_depth_first_task,
            'random': get_random_task,
            'incremental': get_incremental_task}
        sched = sched_map.get(app.info.get('sched'), sched_map['default'])
        return sched(app_id, user_id, user_ip, offset=offset)
Exemplo n.º 4
0
 def create_task_and_run(cls, task_info, task_run_info, app, user, order):
     task = model.Task(app_id=1, state='0', info=task_info)
     task.app = app
     # Taskruns will be assigned randomly to a signed user or an anonymous one
     if random.randint(0, 1) == 1:
         task_run = model.TaskRun(app_id=1,
                                  task_id=1,
                                  user_id=1,
                                  info=task_run_info)
         task_run.user = user
     else:
         task_run = model.TaskRun(app_id=1,
                                  task_id=1,
                                  user_ip='127.0.0.%s' % order,
                                  info=task_run_info)
     task_run.task = task
     return task, task_run
Exemplo n.º 5
0
def _import_task(app, handler, form, render_forms):
    try:
        empty = True
        for task_data in handler.tasks(form):
            task = model.Task(app=app)
            print task_data
            [setattr(task, k, v) for k, v in task_data.iteritems()]
            db.session.add(task)
            db.session.commit()
            empty = False
        if empty:
            raise importer.BulkImportException(
                lazy_gettext('Oops! It looks like the file is empty.'))
        flash(lazy_gettext('Tasks imported successfully!'), 'success')
        return redirect(url_for('.settings', short_name=app.short_name))
    except importer.BulkImportException, err_msg:
        flash(err_msg, 'error')
Exemplo n.º 6
0
def import_task(short_name):
    app = App.query.filter_by(short_name=short_name)\
            .first_or_404()

    form = BulkTaskImportForm(request.form)
    if form.validate_on_submit():
        r = requests.get(form.csv_url.data)
        if r.status_code == 403:
            flash(
                "Oops! It looks like you don't have permission to access"
                " that file!", 'error')
            return render_template('/applications/import.html',
                                   app=app,
                                   form=form)
        if (not 'text/plain' in r.headers['content-type']
                and not 'text/csv' in r.headers['content-type']):
            flash("Oops! That file doesn't look like a CSV file.", 'error')
            return render_template('/applications/import.html',
                                   app=app,
                                   form=form)
        empty = True
        csvcontent = StringIO(r.text)
        csvreader = unicode_csv_reader(csvcontent)
        # TODO: check for errors
        headers = []
        fields = set(
            ['state', 'quorum', 'calibration', 'priority_0', 'n_answers'])
        field_header_index = []
        try:
            for row in csvreader:
                if not headers:
                    headers = row
                    if len(headers) != len(set(headers)):
                        flash(
                            'The CSV file you uploaded has two headers with'
                            ' the same name.', 'error')
                        return render_template('/applications/import.html',
                                               app=app,
                                               form=form)
                    field_headers = set(headers) & fields
                    for field in field_headers:
                        field_header_index.append(headers.index(field))
                else:
                    info = {}
                    task = model.Task(app=app)
                    for index, cell in enumerate(row):
                        if index in field_header_index:
                            setattr(task, headers[index], cell)
                        else:
                            info[headers[index]] = cell
                    task.info = info
                    db.session.add(task)
                    db.session.commit()
                    empty = False
            if empty:
                flash('Oops! It looks like the CSV file is empty.', 'error')
                return render_template('/applications/import.html',
                                       app=app,
                                       form=form)
            flash('Tasks imported successfully!', 'success')
            return redirect(url_for('.details', short_name=app.short_name))
        except:
            flash(
                'Oops! Looks like there was an error with processing '
                'that file!', 'error')
    return render_template('/applications/import.html', app=app, form=form)