Beispiel #1
0
def three_tasks(app):
    db.session.add_all([
        Task(
            title="Water the garden 🌷", description="", completed_at=None),
        Task(
            title="Answer forgotten email 📧", description="", completed_at=None),
        Task(
            title="Pay my outstanding tickets 😭", description="", completed_at=None)
    ])
    db.session.commit()
Beispiel #2
0
def handle_tasks():
    if request.method == 'GET':
        #return full list of tasks

        # allows access to keys - sort
        arg = request.args  # better name
        if "sort" in arg:
            if arg['sort'] == "asc":
                tasks = Task.query.order_by(Task.title.asc())
            elif arg['sort'] == "desc":
                tasks = Task.query.order_by(Task.title.desc())
        else:
            tasks = Task.query.all()
        task_response = []
        for task in tasks:
            task_response.append(task.serialize())

        return jsonify(task_response)

    elif request.method == 'POST':
        request_body = request.get_json()
        if "title" not in request_body:
            return {"details": "Invalid data"}, 400
        elif "description" not in request_body:
            return {"details": "Invalid data"}, 400
        elif "completed_at" not in request_body:
            return {"details": "Invalid data"}, 400
        stringify_format = "%a, %d %b %Y %H:%M:%S %Z"
        if request_body["completed_at"]:

            new_task = Task(title=request_body['title'],
                            description=request_body['description'],
                            completed_at=datetime.strptime(
                                request_body["completed_at"],
                                stringify_format))
        else:
            new_task = Task(
                title=request_body['title'],
                description=request_body['description'],
            )
        db.session.add(new_task)
        db.session.commit()
        return {
            'task': {
                'id': new_task.task_id,
                'title': new_task.title,
                'description': new_task.description,
                'is_complete': new_task.completed_at != None
            }
        }, 201
Beispiel #3
0
def handle_tasks():
    if request.method == "POST":
        request_body = request.get_json()
        if 'title' in request_body and 'description' in request_body and 'completed_at' in request_body:
            new_task = Task(title=request_body["title"],
                            description=request_body["description"],
                            completed_at=request_body["completed_at"])
            db.session.add(new_task)
            db.session.commit()

            response = {"task": new_task.task_response()}
            return jsonify(response), 201

        else:
            return make_response({"details": "Invalid data"}, 400)

    elif request.method == "GET":
        sort_by_title = request.args.get("sort")
        if sort_by_title == "asc":
            tasks = Task.query.order_by(Task.title.asc())
        elif sort_by_title == "desc":
            tasks = Task.query.order_by(Task.title.desc())

        else:
            tasks = Task.query.all()

        tasks_response = []
        for task in tasks:
            tasks_response.append(task.task_response())
        return jsonify(tasks_response), 200
Beispiel #4
0
def preprocessing_upload_mongodb():
    file = request.files.get('file')
    json_file = file.read()
    con = json.loads(json_file)
    task_id = con['task_id']
    print(task_id)
    col = mongo.db['%s' % str(task_id)]

    # 插入到mongodb中,这里需要将detail值做优化,只保存里面的prop_id和prop_value,prop_id作为键,prop_value作为值

    preprocess = PreprocessingCollection()
    d1 = preprocess.fill(con['details'], task_id)
    # print(d1)
    # 保存格式:task_details_id:{prop_id:prop_value}
    res = col.insert_one(d1)

    # 在task表中增加一个字段,插入完成之后,将值改为1,
    with db.auto_commit():
        task = Task().query.filter_by(id=task_id).first()
        task.is_preprocess = 1
    # print('inserted_id:%s' % (res.inserted_id))
    # find_res = col.find_one({"_id": res.inserted_id})
    #
    # item = find_res['4593']
    # for k,v in item.items():
    #     print('%s:%s' % (k, v))
    # for k, v in item.items():
    #     print('%s:%s'%(k,v))
    return 'success'
Beispiel #5
0
def tasks():

    if request.method == "GET":
        task_order = request.args.get("sort")
        if task_order == None:
            tasks = Task.query.all()  # get all items in list
        elif task_order == "asc":  #to sort by ascending
            tasks = Task.query.order_by(asc(Task.title))
        elif task_order == "desc":  #to sort by descending
            tasks = Task.query.order_by(desc(Task.title))

        tasks_response = []
        for task in tasks:
            tasks_response.append(task.display_tasks())
        return jsonify(tasks_response)

    else:
        request_body = request.get_json()
        if "title" not in request_body or "description" not in request_body or "completed_at" not in request_body:
            return make_response(jsonify({"details": "Invalid data"}), 400)

        task = Task(title=request_body["title"],
                    description=request_body["description"],
                    completed_at=request_body["completed_at"])

        db.session.add(task)
        db.session.commit()

        return jsonify({"task": task.display_tasks()}), 201
Beispiel #6
0
def update_ranking_score():
    now = datetime.now()
    name = 'update_ranking_score'
    task = Task.query.filter_by(target_type='system', name=name).first()
    if task is None:
        task = Task(name, None)
        db.session.add(task)
    query = Issue.query.order_by('created_at')
    task.start(query.count())
    while task.current < task.total:
        for issue in query.limit(32).offset(task.current).all():
            logger = get_task_logger(__name__)
            hours = (now - issue.created_at).total_seconds() / 3600
            issue.score = get_score(issue.points, hours, issue.comments_count)
            logger.info('update: %s #%d %s', issue.repository.name,
                        issue.number, issue.title)
        task.current += 32
        task.update()
        try:
            db.session.commit()
        except:
            db.session.rollback()
    task.finish()
    try:
        db.session.commit()
    except:
        db.session.rollback()
        return False
    return True
Beispiel #7
0
def deal_tasks():
    if request.method == "GET":
        sort_query = request.args.get("sort")  #<- handles second wave 2
        if sort_query == "desc":
            tasks = Task.query.order_by(Task.title.desc()).all()
        else:
            tasks = Task.query.order_by(
                Task.title).all()  #<- handles first wave 2
        tasks_response = []
        for task in tasks:
            tasks_response.append(task.to_json())
        return jsonify(tasks_response)

    elif request.method == "POST":
        request_body = request.get_json()

        if "title" in request_body and "description" in request_body and "completed_at" in request_body:
            new_task = Task(title=request_body["title"],
                            description=request_body["description"],
                            completed_at=request_body["completed_at"])

            db.session.add(new_task)
            db.session.commit()
            return make_response({"task": new_task.to_json()}, 201)
        else:
            return jsonify({"details": "Invalid data"}), 400
def test_complete_task_does_nothing_when_task_is_already_completed():
    completed_at = date(2020, 12, 31)
    task = Task(name="Foo", completed_at=completed_at)

    task.complete(at=date(2021, 1, 17))

    assert task.completed_at == completed_at
Beispiel #9
0
def handle_tasks():
    if request.method == "GET":
        direction = request.args.get("sort")
        if direction == "asc":
            tasks = Task.query.order_by(asc("title"))
        elif direction == "desc":
            tasks = Task.query.order_by(desc("title"))
        else:
            tasks = Task.query.all()

        tasks_response = [task.to_dict() for task in tasks]

        return jsonify(tasks_response)

    elif request.method == "POST":
        request_body = request.get_json()
        try:
            new_task = Task(title=request_body["title"],
                            description=request_body["description"],
                            completed_at=request_body["completed_at"])
        except KeyError:
            return make_response({"details": "Invalid data"}, 400)

        db.session.add(new_task)
        db.session.commit()

        return make_response({"task": new_task.to_dict()}, 201)
Beispiel #10
0
def main_task(self, func, params):
    # Fetch Task ID
    task_id = self.request.id
    logger.debug('Verifying Task:' + str(task_id))
    logger.debug('Received params: {}'.format(params))

    # Get the state
    task = Task(task_id)
    task.progress = 0

    # Execute the passed function
    logger.debug("Starting task...")
    try:
        result = func(task_id, params)
        logger.info("Got result from task...")
        logger.debug(result)
    except TaskError as te:
        logger.error(te)
        task.progress = -1
        revoke(task_id, terminate=True)
        raise Exception("Couldn't complete task...")
    except Exception as e:
        logger.error(e)
        task.progress = -1
        revoke(task_id, terminate=True)
        raise Exception("Couldn't complete task...")
    # Setting result
    task.progress = 100
    task.result = result
    logger.info("Finishing main task!")
Beispiel #11
0
 def launch_email(self, name, description):
     rq_job = current_app.email_queue.enqueue('app.redis_tasks.' + name, self.email)
     task = Task(id=rq_job.get_id(), name=name, description=description,
                 task_type=Task.Type['EMAIL'], user=self)
     db.session.add(task)
     db.session.commit()
     return task
    def test_13_complete_task_history_product_retailer(self):
        """ Test Price History Product Retailer
        """
        print(">>>>>", "Test Price History Product Retailer")
        # Import celery task
        from app.celery_app import main_task
        from app.models.history_product import Product

        # Filters for the task
        params = {
            "item_uuid": "fd960578-71ae-463e-84d5-0e451d184597",
            "retailer": "walmart"
        }

        celery_task = main_task.apply_async(args=(Product.start_retailer_task,
                                                  params))
        print("Submitted Task: ", celery_task.id)
        # Get the task from the celery task
        time.sleep(2)
        task = Task(celery_task.id)
        print('Created task instance!')

        # Check result of task
        while task.is_running():
            print("Waiting for task to finish")
            print(task.task_id)
            print(task.progress)
            print(task.status)
            time.sleep(1)

        prog = task.status['progress']
        print("Final progress: {}".format(prog))
        print("Result keys: {} ".format(list(task.result.keys())))
        self.assertEqual(prog, 100)
Beispiel #13
0
def task_cancel(task_id):
    """ Endpoint to consult tasks status by task_id
    """
    logger.info("Cancelling task ...")
    task = Task(task_id)
    if not task.status:
        raise AppError("task_error", "Task not found!")
    try:
        logger.info("Getting result")
        celery_task = celery_app.AsyncResult(task_id)
    except Exception as e:
        logger.error(e)
    if celery_task.state == 'SUCCESS' or celery_task.state == 'FAILURE':
        return jsonify(task.status)
    # Celery status
    try:
        task.progress = -1
        revoke(task_id, terminate=True)
    except Exception as e:
        logger.error(e)
    logger.debug(task.status)
    logger.debug(celery_task.state)
    status = task.status
    status['celery_status'] = celery_task.state

    return jsonify(status)
Beispiel #14
0
def tasks():

    if request.method == "GET":
        task_order = request.args.get("sort")
        if task_order == None:
            tasks = Task.query.all(
            )  # Task is the model and query is a class method (query is getting all task)
        elif task_order == "asc":
            tasks = Task.query.order_by(asc(Task.title))
        elif task_order == "desc":
            tasks = Task.query.order_by(desc(Task.title))

        tasks_response = []
        for task in tasks:

            tasks_response.append(task.to_json())

        return jsonify(tasks_response)
    # using the "PUT" to add a new task
    else:
        request_body = request.get_json()
        if "title" not in request_body or "description" not in request_body or "completed_at" not in request_body:
            return make_response(jsonify({"details": "Invalid data"}), 400)

        task = Task(title=request_body["title"],
                    description=request_body["description"],
                    completed_at=request_body["completed_at"])

        db.session.add(task)
        db.session.commit()

        return jsonify({"task": task.to_json()}), 201
    def test_11_complete_task_count_by_retailer_engine(self):
        """ Test count by retailer engine
        """
        print(">>>>>", "Test Count by retailer engine")
        # Import celery task
        from app.celery_app import main_task
        from app.models.history_product import Product

        # Filters for the task
        params = {"date": "2019-05-24 19:17:06", "retailer": "walmart"}

        celery_task = main_task.apply_async(
            args=(Product.count_by_retailer_engine_task, params))
        print("Submitted Task: ", celery_task.id)
        # Get the task from the celery task
        time.sleep(2)
        task = Task(celery_task.id)
        print('Created task instance!')

        # Check result of task
        while task.is_running():
            print("Waiting for task to finish")
            print(task.task_id)
            print(task.progress)
            print(task.status)
            time.sleep(1)

        prog = task.status['progress']
        print("Final progress: {}".format(prog))
        print("Result keys: {} ".format(list(task.result.keys())))
        self.assertEqual(prog, 100)
Beispiel #16
0
def handle_tasks():

    if request.method == "POST":
        request_body = request.get_json()

        if "title" not in request_body or "description" not in request_body or "completed_at" not in request_body:
            return ({"details": "Invalid data"}, 400)
        else:
            task = Task(title=request_body["title"],
                        description=request_body["description"],
                        completed_at=request_body["completed_at"])
            db.session.add(task)
            db.session.commit()

            return make_response({"task": task.to_json()}, 201)
    else:
        title_sort_query = request.args.get("sort")
        if title_sort_query == "desc":
            tasks = Task.query.order_by(Task.title.desc())
        elif title_sort_query == "asc":
            tasks = Task.query.order_by(Task.title.asc())
        else:
            tasks = Task.query.all()

        tasks_response = []
        for task in tasks:
            tasks_response.append(task.to_json())

        return jsonify(tasks_response)
Beispiel #17
0
def test_task(self, params):
    """ Celery test task
    """
    # Fetch Task ID
    task_id = self.request.id
    logger.info('Verifying Task:' + str(task_id))
    logger.info('Received params: {}'.format(params))

    # Get the state
    task = Task(task_id)
    task.progress = 0

    # Make an operation
    import time
    time.sleep(2)
    logger.info('Processing task')
    task.progress = 50

    # Save result
    time.sleep(10)
    logger.info('Getting task result')
    task.progress = 100
    task.result = {
        "msg": "Task completed successfully",
        "data": ["Default text" for i in range(1000)]
    }
Beispiel #18
0
 def batch(cls, project, create_user):
     project.var_dick = {}
     configs = cls.query.filter_by(project_id=project.id,
                                   is_run=True).order_by(cls.order).all()
     if not configs:
         raise ConfigNotFound(msg='工程下无可运行用例')
     # 执行用例总数
     total = len(configs)
     task = Task(project.id, create_user.id, total)
     task.new_task()
     task.update_task_no()
     step = 100 / total
     progress = 0
     with db.session.no_autoflush:
         for config in configs:
             case = Case(0, config.name, config.info, config.url,
                         config.method, config.submit, config.header,
                         config.data, config.deal, config.condition,
                         config.expect, config.assertion, config.type)
             case.id = config.case_id
             # 副本工程类型 不管理原用例分组
             case.case_group = None
             case.case_group_name = None
             case.execute_one(project, task, create_user)
             progress += step
             # 更新工程进度
             project.update_progress(progress)
Beispiel #19
0
def get_tasks():
    if request.method == 'GET':
        response = []
        tasks = Task.query.all()
        for task in tasks:
            response.append(
                {'id': task.id,
                 'name': task.name,
                 'description': task.description,
                 'created_date': task.created_date,
                 'is_active': task.is_active,
                 'updated_date': task.updated_date}
            )

        return json.dumps(response,default=myconverter)
    else:
        name = request.form['name']
        description = request.form['description']

        new_task = Task(created_date=datetime.datetime.now(), updated_date=datetime.datetime.now(), name=name,
                             description=description)
        db.session.add(new_task)
        db.session.commit()

        return "Added new task!" + str(new_task)
Beispiel #20
0
 def launch_course_email(self, course):
     rq_job = current_app.email_queue.enqueue('app.redis_tasks.send_course_email', self.email, course, self.role)
     task = Task(id=rq_job.get_id(), name='send_course_email', description='append course',
                 task_type=Task.Type['EMAIL'], user=self)
     db.session.add(task)
     db.session.commit()
     return task
Beispiel #21
0
    def test_08_fail_task_price_map_decorator(self):
        """ Test Map task without filters
        """
        print(">>>>", "Test Map task without filters")
        # Import celery task
        from app.celery_app import main_task
        from app.models.geo_mapa import Map

        # Filters for the task -> missing filters
        params = {
            "retailers": {
                "walmart": "Walmart",
                "superama": "Superama"
            },
            "date_start": "2019-01-10",
            "date_end": "2019-01-13",
            "interval": "day"
        }
        celery_task = main_task.apply_async(args=(Map.start_task, params))
        task = Task(celery_task.id)
        print("Submitted Task: ", celery_task.id)
        # Check result of task
        while task.is_running():
            time.sleep(1)

        prog = task.status['progress']
        print("Final progress: {}".format(prog))
        print("Result keys: {} ".format(list(task.result.keys())))

        self.assertEqual(prog, -1)
Beispiel #22
0
    def test_06_complete_task_price_map(self):
        """ Test price
        """
        raise Exception("DEPRECATED TEST!")
        # Import celery task
        from app.celery_tasks import test_task
        # Filters for the task
        filters = [{"item_uuid": ""}, {"item_uuid": ""}, {"retailer": ""}]
        params = {
            "filters": filters,
            "retailers": ["walmart", "superama"],
            "date_start": "2018-05-25",
            "date_end": "2018-05-29",
            "interval": "day"
        }
        c_task = price_map.apply_async(args=(params, ))

        # Get the task from the celery task
        task = Task(c_task.id)

        # Check result of task
        while task.status['progress'] < 100:
            print("Waiting for task to finish")
            print(task.status)
            time.sleep(2)

        prog = task.status['progress']
        print("Final progress: {}".format(prog))

        self.assertEqual(prog, 100)
Beispiel #23
0
def task_result(task_id):
    """ Endpoint to query tasks result
    """
    logger.info("Retrieving task result ...")
    # Check if streaming response is requested
    rule = str(request.url_rule)
    logger.debug(rule)
    stream = True if 'stream' in rule else False
    # Getting task status
    task = Task(task_id)
    if not task.status:
        raise AppError("task_error", "Task not found!")

    data = None
    if hasattr(task, 'result'):
        logger.info("Retrieving result data ...")
        data = task.result['data'] if 'data' in task.result else None

    resp = {"status": task.status, "result": data}
    if stream:
        # Generator
        string_resp = json.dumps(resp)
        generate = lambda: [(yield x) for x in string_resp]
        return Response(stream_with_context(generate()),
                        content_type='application/json')
    else:
        return jsonify(resp)
Beispiel #24
0
def upload():
    file = request.files['file']
    ALLOWED_EXTENSIONS = set(['xls', 'xlsx'])
    if file:
        if '.' in file.filename and file.filename.rsplit(
                '.', 1)[1] not in ALLOWED_EXTENSIONS:
            raise FileTypeError()
        else:
            dirpath = os.path.join(current_app.root_path, 'upload')
            dirpath = dirpath.replace('\\', '/')
            now = datetime.datetime.now().strftime('%Y-%m-%d')
            path = dirpath + r'/task' + now + '.xls'
            file.save(path)
            #解析excel
            deal_excel = dealBatchExcel(path)
            task_name, task_url, task_description = deal_excel.get_task()
            task = Task.query.filter_by(name=task_name).all()
            #如果上传文件中的task不存在,则新增一个task
            if not task:
                task = Task()
                task.add_task(task_name, task_description, task_url)
            task = Task.query.filter_by(name=task_name).first_or_404()
            deal_excel.add_task_case(task.id)

    return jsonify(task)
def test_complete_task_sets_completed_at():
    task = Task(name="Foo")
    now = date(2020, 12, 31)

    task.complete(at=now)

    assert task.completed_at is not None
    assert task.completed_at == now
 def add_task(title, description, due_date, user_id):
     task = Task(title, user_id)
     if (description and description.strip()):
         task.set_description(description)
     if (due_date):
         task.set_due_date(due_date)
     db.session.add(task)
     db.session.commit()
Beispiel #27
0
 def create_observation_task(selling_partner_key):
     selling_partner = selling_partner_key.get()
     params = {
         'branch': selling_partner.branch,
         'ops_group': selling_partner.ops_group,
         'section': selling_partner.section,
         'due_date': datetime.datetime.now() + datetime.timedelta(days=30),
     }
     Task(kind='Observation', selling_partner=selling_partner_key, due_date=due_date, **params).put()
Beispiel #28
0
 def test_04_get_task_result(self):
     """ Change task status
     """
     print(">>>> Testing getting task result")
     global task_id
     task = Task(task_id)
     result = task.result
     print("Size of queried result: {}".format(sys.getsizeof(str(result))))
     self.assertEqual(type(result), dict)
Beispiel #29
0
 def test_02_get_task_status(self):
     """ Get task's status 
     """
     print(">>>> Testing get task status")
     global task_id
     task = Task(task_id)
     status = task.status
     print(status)
     self.assertTrue(type(status) == dict)
Beispiel #30
0
 def post(self):
     parser = reqparse.RequestParser()
     parser.add_argument('text')
     parser.add_argument('title')
     args = parser.parse_args()
     task = Task(title=args.title, text=args.text)
     db.session.add(task)
     db.session.commit()
     return {"status": "ok", "task": task.to_dict()}, 200