Esempio n. 1
0
    def test_clean_source_with_valid_format(self):
        source1 = Source(name="Source Test", url="abc1.com", approved=1)
        db.session.add(source1)

        source2 = Source(url="http://www.abc2.com", approved=1)
        db.session.add(source2)

        source3 = Source(name="", url="abc3.com", approved=1)
        db.session.add(source3)
        db.session.commit()

        data1_json = match_bl.handle_source_data(
            Source.find_source_by_id(source1.id))
        data2_json = match_bl.handle_source_data(
            Source.find_source_by_id(source2.id))
        data3_json = match_bl.handle_source_data(
            Source.find_source_by_id(source3.id))

        self.assertEqual(
            data1_json["url_icon"],
            CONST.SOURCE_URL_ICON.format(match_bl.get_domain(source1.url)))

        self.assertEqual(data2_json["name"], 'www.abc2.com')

        self.assertEqual(data3_json["name"], 'abc3.com')

        db.session.delete(source1)
        db.session.delete(source2)
        db.session.delete(source3)
        db.session.commit()
    def test_add_outcome(self):
        t = datetime.now().timetuple()
        seconds = local_to_utc(t)
        arr_match = []
        Uid = 66
        source1 = Source.find_source_by_id(1)
        if source1 is None:
            source1 = Source(id=1,
                             name="Source",
                             url="htpp://www.abc.com",
                             approved=1)
            db.session.add(source1)
            db.session.commit()

        match = Match(public=1,
                      date=seconds + 100,
                      reportTime=seconds + 200,
                      disputeTime=seconds + 300,
                      source_id=source1.id)
        arr_match.append(match)
        db.session.add(match)
        db.session.commit()

        with self.client:

            params = [{
                "name": "outcome test",
                "match_id": match.id,
                "hid": 88
            }]
            response = self.client.post('/outcome/add/{}'.format(match.id),
                                        data=json.dumps(params),
                                        content_type='application/json',
                                        headers={
                                            "Uid": "{}".format(Uid),
                                            "Fcm-Token": "{}".format(123),
                                            "Payload": "{}".format(123),
                                        })

            data = json.loads(response.data.decode())
            self.assertTrue(data['status'] == 1)
            data_json = data['data']
            self.assertTrue(
                data_json[0]['approved'] == CONST.OUTCOME_STATUS['PENDING'])

            response = self.client.get('/match/{}'.format(match.id),
                                       content_type='application/json',
                                       headers={
                                           "Uid": "{}".format(Uid),
                                           "Fcm-Token": "{}".format(123),
                                           "Payload": "{}".format(123),
                                       })
            data = json.loads(response.data.decode())
            self.assertTrue(data['status'] == 1)
            data_json = data['data']
            self.assertTrue(len(data_json['outcomes']) == 0)

            for item in arr_match:
                db.session.delete(item)
                db.session.commit()
Esempio n. 3
0
    def setUp(self):
        # create source
        source = Source.find_source_by_id(1)
        if source is None:
            source = Source(id=1, name="source1", url="url1")
            db.session.add(source)
            db.session.commit()

        else:
            source.name = "source1"
            source.url = "url1"
            db.session.flush()
            db.session.commit()
Esempio n. 4
0
def approve_source(source_id):
	try:
		source = Source.find_source_by_id(source_id)
		if source is not None:
			if source.approved != 1:
				source.approved = 1
				db.session.flush()
			else:
				return response_error(MESSAGE.SOURCE_APPOVED_ALREADY, CODE.SOURCE_APPOVED_ALREADY)
				
		else:
			return response_error(MESSAGE.SOURCE_INVALID, CODE.SOURCE_INVALID)

		db.session.commit()
		return response_ok(source.to_json())
	except Exception, ex:
		db.session.rollback()
		return response_error(ex.message)
Esempio n. 5
0
def update_image(source_id):
    try:
        source = Source.find_source_by_id(source_id)
        if source is None:
            return False

        request_size = request.headers.get('Content-length')  # string
        is_update_matchs = request.form.get('update_matchs', '0') == '1'

        image_name = '{}_{}'.format(source.id, source.name).lower()
        image_name = re.sub(r'[^A-Za-z0-9\_\-\.]+', '_', image_name)
        image_name = '{}.jpg'.format(image_name)

        if request.files and len(
                request.files) > 0 and request.files['image'] is not None:
            if int(
                    request_size
            ) <= CONST.UPLOAD_MAX_FILE_SIZE and storage_bl.validate_extension(
                    request.files['image'].filename,
                    CONST.UPLOAD_ALLOWED_EXTENSIONS):
                image_name, saved_path = storage_bl.handle_upload_file(
                    request.files['image'], file_name=image_name)
                saved_path, crop_saved_path = storage_bl.handle_crop_image(
                    image_name, saved_path)
            else:
                return response_error(MESSAGE.FILE_TOO_LARGE,
                                      CODE.FILE_TOO_LARGE)

        if image_name is not None and saved_path is not None:
            result_upload = gc_storage_client.upload_to_storage(
                app.config.get('GC_STORAGE_BUCKET'), crop_saved_path,
                '{}/{}'.format(app.config.get('GC_STORAGE_FOLDER'),
                               app.config.get('GC_DEFAULT_FOLDER')),
                image_name, True)
            if result_upload is False:
                return response_error()

        storage_bl.delete_file(crop_saved_path)
        storage_bl.delete_file(saved_path)

        # Update image_url all matchs
        if is_update_matchs:
            t = datetime.now().timetuple()
            seconds = local_to_utc(t)

            matches = db.session.query(Match)\
            .filter(\
             and_(\
              Match.source_id == source.id,
              or_(Match.image_url == None, Match.image_url == ""),
              Match.deleted == 0,\
              Match.date > seconds,\
              Match.public == 1,\
              Match.id.in_(db.session.query(Outcome.match_id).filter(and_(Outcome.result == -1, Outcome.hid != None)).group_by(Outcome.match_id))
             )\
            )\
            .all()

            image_url = CONST.SOURCE_GC_DOMAIN.format(
                app.config['GC_STORAGE_BUCKET'],
                '{}/{}'.format(app.config.get('GC_STORAGE_FOLDER'),
                               app.config.get('GC_DEFAULT_FOLDER')),
                image_name)
            for match in matches:
                match.image_url = image_url
                db.session.flush()
            db.session.commit()

        return response_ok()

    except Exception, ex:
        db.session.rollback()
        return response_error(ex.message)