Best Python code snippet using tempest_python
loading.py
Source:loading.py
...10from buildstream.exceptions import ErrorDomain, LoadErrorReason11from buildstream.testing import cli # pylint: disable=unused-import12from buildstream import _yaml13DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "loading")14def update_project(project_path, updated_configuration):15 project_conf_path = os.path.join(project_path, "project.conf")16 project_conf = _yaml.roundtrip_load(project_conf_path)17 project_conf.update(updated_configuration)18 _yaml.roundtrip_dump(project_conf, project_conf_path)19# Sets up the element.bst file so that it requires a source20# or element plugin.21#22def setup_element(project_path, plugin_type, plugin_name):23 element_dir = os.path.join(project_path, "elements")24 element_path = os.path.join(element_dir, "element.bst")25 os.makedirs(element_dir, exist_ok=True)26 if plugin_type == "elements":27 element = {"kind": plugin_name}28 else:29 element = {"kind": "manual", "sources": [{"kind": plugin_name}]}30 _yaml.roundtrip_dump(element, element_path)31# This function is used for pytest skipif() expressions.32#33# Tests which require our plugins in tests/plugins/pip-samples need34# to check if these plugins are installed, they are only guaranteed35# to be installed when running tox, but not when using pytest directly36# to test that BuildStream works when integrated in your system.37#38def pip_sample_packages():39 import pkg_resources40 required = {"sample-plugins"}41 installed = {pkg.key for pkg in pkg_resources.working_set} # pylint: disable=not-an-iterable42 missing = required - installed43 if missing:44 return False45 return True46SAMPLE_PACKAGES_SKIP_REASON = """47The sample plugins package used to test pip plugin origins is not installed.48This is usually tested automatically with `tox`, if you are running49`pytest` directly then you can install these plugins directly using pip.50The plugins are located in the tests/plugins/sample-plugins directory51of your BuildStream checkout.52"""53####################################################54# Tests #55####################################################56@pytest.mark.datafiles(DATA_DIR)57@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])58def test_nosetup(cli, datafiles, plugin_type):59 project = str(datafiles)60 update_project(project, {"plugins": [{"origin": "local", "path": "plugins/nosetup", plugin_type: ["nosetup"]}]})61 setup_element(project, plugin_type, "nosetup")62 result = cli.run(project=project, args=["show", "element.bst"])63 result.assert_main_error(ErrorDomain.PLUGIN, "missing-setup-function")64@pytest.mark.datafiles(DATA_DIR)65@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])66def test_setup_not_function(cli, datafiles, plugin_type):67 project = str(datafiles)68 update_project(69 project,70 {"plugins": [{"origin": "local", "path": "plugins/setupnotfunction", plugin_type: ["setupnotfunction"]}]},71 )72 setup_element(project, plugin_type, "setupnotfunction")73 result = cli.run(project=project, args=["show", "element.bst"])74 result.assert_main_error(ErrorDomain.PLUGIN, "setup-is-not-function")75@pytest.mark.datafiles(DATA_DIR)76@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])77def test_setup_returns_not_type(cli, datafiles, plugin_type):78 project = str(datafiles)79 update_project(80 project,81 {82 "plugins": [83 {"origin": "local", "path": "plugins/setupreturnsnottype", plugin_type: ["setupreturnsnottype"]}84 ]85 },86 )87 setup_element(project, plugin_type, "setupreturnsnottype")88 result = cli.run(project=project, args=["show", "element.bst"])89 result.assert_main_error(ErrorDomain.PLUGIN, "setup-returns-not-type")90@pytest.mark.datafiles(DATA_DIR)91@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])92def test_setup_returns_bad_type(cli, datafiles, plugin_type):93 project = str(datafiles)94 update_project(95 project,96 {97 "plugins": [98 {"origin": "local", "path": "plugins/setupreturnsbadtype", plugin_type: ["setupreturnsbadtype"]}99 ]100 },101 )102 setup_element(project, plugin_type, "setupreturnsbadtype")103 result = cli.run(project=project, args=["show", "element.bst"])104 result.assert_main_error(ErrorDomain.PLUGIN, "setup-returns-bad-type")105@pytest.mark.datafiles(DATA_DIR)106@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])107def test_missing_min_version(cli, datafiles, plugin_type):108 project = str(datafiles)109 update_project(110 project,111 {112 "plugins": [113 {114 "origin": "local",115 "path": os.path.join("plugins", plugin_type, "nominversion"),116 plugin_type: ["nominversion"],117 }118 ]119 },120 )121 setup_element(project, plugin_type, "nominversion")122 result = cli.run(project=project, args=["show", "element.bst"])123 result.assert_main_error(ErrorDomain.PLUGIN, "missing-min-version")124@pytest.mark.datafiles(DATA_DIR)125@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])126@pytest.mark.parametrize("plugin", [("badstring"), ("number"), ("dict"), ("list")])127def test_malformed_min_version(cli, datafiles, plugin_type, plugin):128 project = str(datafiles)129 update_project(130 project,131 {132 "plugins": [133 {134 "origin": "local",135 "path": os.path.join("plugins", plugin_type, "malformedminversion"),136 plugin_type: [plugin],137 }138 ]139 },140 )141 setup_element(project, plugin_type, plugin)142 result = cli.run(project=project, args=["show", "element.bst"])143 result.assert_main_error(ErrorDomain.PLUGIN, "malformed-min-version")144@pytest.mark.datafiles(DATA_DIR)145@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])146def test_incompatible_major_version(cli, datafiles, plugin_type):147 project = str(datafiles)148 update_project(149 project,150 {151 "plugins": [152 {153 "origin": "local",154 "path": os.path.join("plugins", plugin_type, "incompatiblemajor"),155 plugin_type: ["incompatiblemajor"],156 }157 ]158 },159 )160 setup_element(project, plugin_type, "incompatiblemajor")161 result = cli.run(project=project, args=["show", "element.bst"])162 result.assert_main_error(ErrorDomain.PLUGIN, "incompatible-major-version")163@pytest.mark.datafiles(DATA_DIR)164@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])165def test_incompatible_minor_version(cli, datafiles, plugin_type):166 project = str(datafiles)167 update_project(168 project,169 {170 "plugins": [171 {172 "origin": "local",173 "path": os.path.join("plugins", plugin_type, "incompatibleminor"),174 plugin_type: ["incompatibleminor"],175 }176 ]177 },178 )179 setup_element(project, plugin_type, "incompatibleminor")180 result = cli.run(project=project, args=["show", "element.bst"])181 result.assert_main_error(ErrorDomain.PLUGIN, "incompatible-minor-version")182@pytest.mark.datafiles(DATA_DIR)183@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])184def test_plugin_not_found(cli, datafiles, plugin_type):185 project = str(datafiles)186 setup_element(project, plugin_type, "notfound")187 result = cli.run(project=project, args=["show", "element.bst"])188 result.assert_main_error(ErrorDomain.PLUGIN, "plugin-not-found")189@pytest.mark.datafiles(DATA_DIR)190@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])191def test_plugin_found(cli, datafiles, plugin_type):192 project = str(datafiles)193 update_project(194 project,195 {196 "plugins": [197 {"origin": "local", "path": os.path.join("plugins", plugin_type, "found"), plugin_type: ["found"],}198 ]199 },200 )201 setup_element(project, plugin_type, "found")202 result = cli.run(project=project, args=["show", "element.bst"])203 result.assert_success()204@pytest.mark.datafiles(DATA_DIR)205@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])206def test_deprecation_warnings(cli, datafiles, plugin_type):207 project = str(datafiles)208 update_project(209 project,210 {211 "plugins": [212 {213 "origin": "local",214 "path": os.path.join("plugins", plugin_type, "deprecated"),215 plugin_type: ["deprecated"],216 }217 ]218 },219 )220 setup_element(project, plugin_type, "deprecated")221 result = cli.run(project=project, args=["show", "element.bst"])222 result.assert_success()223 assert "Here is some detail." in result.stderr224@pytest.mark.datafiles(DATA_DIR)225@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])226def test_deprecation_warning_suppressed_by_origin(cli, datafiles, plugin_type):227 project = str(datafiles)228 update_project(229 project,230 {231 "plugins": [232 {233 "origin": "local",234 "path": os.path.join("plugins", plugin_type, "deprecated"),235 "allow-deprecated": True,236 plugin_type: ["deprecated"],237 }238 ]239 },240 )241 setup_element(project, plugin_type, "deprecated")242 result = cli.run(project=project, args=["show", "element.bst"])243 result.assert_success()244 assert "Here is some detail." not in result.stderr245@pytest.mark.datafiles(DATA_DIR)246@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])247def test_deprecation_warning_suppressed_specifically(cli, datafiles, plugin_type):248 project = str(datafiles)249 update_project(250 project,251 {252 "plugins": [253 {254 "origin": "local",255 "path": os.path.join("plugins", plugin_type, "deprecated"),256 plugin_type: [{"kind": "deprecated", "allow-deprecated": True}],257 }258 ]259 },260 )261 setup_element(project, plugin_type, "deprecated")262 result = cli.run(project=project, args=["show", "element.bst"])263 result.assert_success()264 assert "Here is some detail." not in result.stderr265@pytest.mark.datafiles(DATA_DIR)266@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])267@pytest.mark.skipif("not pip_sample_packages()", reason=SAMPLE_PACKAGES_SKIP_REASON)268def test_pip_origin_load_success(cli, datafiles, plugin_type):269 project = str(datafiles)270 update_project(271 project, {"plugins": [{"origin": "pip", "package-name": "sample-plugins", plugin_type: ["sample"],}]},272 )273 setup_element(project, plugin_type, "sample")274 result = cli.run(project=project, args=["show", "element.bst"])275 result.assert_success()276@pytest.mark.datafiles(DATA_DIR)277@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])278@pytest.mark.skipif("not pip_sample_packages()", reason=SAMPLE_PACKAGES_SKIP_REASON)279def test_pip_origin_with_constraints(cli, datafiles, plugin_type):280 project = str(datafiles)281 update_project(282 project,283 {284 "plugins": [285 {"origin": "pip", "package-name": "sample-plugins>=1.0,<1.2.5,!=1.1.3", plugin_type: ["sample"],}286 ]287 },288 )289 setup_element(project, plugin_type, "sample")290 result = cli.run(project=project, args=["show", "element.bst"])291 result.assert_success()292@pytest.mark.datafiles(DATA_DIR)293@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])294def test_pip_origin_package_not_found(cli, datafiles, plugin_type):295 project = str(datafiles)296 update_project(297 project, {"plugins": [{"origin": "pip", "package-name": "not-a-package", plugin_type: ["sample"],}]},298 )299 setup_element(project, plugin_type, "sample")300 result = cli.run(project=project, args=["show", "element.bst"])301 result.assert_main_error(ErrorDomain.PLUGIN, "package-not-found")302@pytest.mark.datafiles(DATA_DIR)303@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])304@pytest.mark.skipif("not pip_sample_packages()", reason=SAMPLE_PACKAGES_SKIP_REASON)305def test_pip_origin_plugin_not_found(cli, datafiles, plugin_type):306 project = str(datafiles)307 update_project(308 project, {"plugins": [{"origin": "pip", "package-name": "sample-plugins", plugin_type: ["notfound"],}]},309 )310 setup_element(project, plugin_type, "notfound")311 result = cli.run(project=project, args=["show", "element.bst"])312 result.assert_main_error(ErrorDomain.PLUGIN, "plugin-not-found")313@pytest.mark.datafiles(DATA_DIR)314@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])315@pytest.mark.skipif("not pip_sample_packages()", reason=SAMPLE_PACKAGES_SKIP_REASON)316def test_pip_origin_version_conflict(cli, datafiles, plugin_type):317 project = str(datafiles)318 update_project(319 project, {"plugins": [{"origin": "pip", "package-name": "sample-plugins>=1.4", plugin_type: ["sample"],}]},320 )321 setup_element(project, plugin_type, "sample")322 result = cli.run(project=project, args=["show", "element.bst"])323 result.assert_main_error(ErrorDomain.PLUGIN, "package-version-conflict")324@pytest.mark.datafiles(DATA_DIR)325@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])326@pytest.mark.skipif("not pip_sample_packages()", reason=SAMPLE_PACKAGES_SKIP_REASON)327def test_pip_origin_malformed_constraints(cli, datafiles, plugin_type):328 project = str(datafiles)329 update_project(330 project, {"plugins": [{"origin": "pip", "package-name": "sample-plugins>1.4,A", plugin_type: ["sample"],}]},331 )332 setup_element(project, plugin_type, "sample")333 result = cli.run(project=project, args=["show", "element.bst"])334 result.assert_main_error(ErrorDomain.PLUGIN, "package-malformed-requirement")335@pytest.mark.datafiles(DATA_DIR)336@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])337def test_junction_plugin_found(cli, datafiles, plugin_type):338 project = str(datafiles)339 subproject = os.path.join(project, "subproject")340 shutil.copytree(os.path.join(project, "plugins"), os.path.join(subproject, "plugins"))341 update_project(342 project, {"plugins": [{"origin": "junction", "junction": "subproject-junction.bst", plugin_type: ["found"],}]},343 )344 update_project(345 subproject,346 {347 "plugins": [348 {"origin": "local", "path": os.path.join("plugins", plugin_type, "found"), plugin_type: ["found"],}349 ]350 },351 )352 setup_element(project, plugin_type, "found")353 result = cli.run(project=project, args=["show", "element.bst"])354 result.assert_success()355@pytest.mark.datafiles(DATA_DIR)356@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])357def test_junction_plugin_not_found(cli, datafiles, plugin_type):358 project = str(datafiles)359 subproject = os.path.join(project, "subproject")360 shutil.copytree(os.path.join(project, "plugins"), os.path.join(subproject, "plugins"))361 # The toplevel says to search for the "notfound" plugin in the subproject362 #363 update_project(364 project,365 {"plugins": [{"origin": "junction", "junction": "subproject-junction.bst", plugin_type: ["notfound"],}]},366 )367 # The subproject only configures the "found" plugin368 #369 update_project(370 subproject,371 {372 "plugins": [373 {"origin": "local", "path": os.path.join("plugins", plugin_type, "found"), plugin_type: ["found"],}374 ]375 },376 )377 setup_element(project, plugin_type, "notfound")378 result = cli.run(project=project, args=["show", "element.bst"])379 result.assert_main_error(ErrorDomain.PLUGIN, "junction-plugin-not-found")380@pytest.mark.datafiles(DATA_DIR)381@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])382def test_junction_deep_plugin_found(cli, datafiles, plugin_type):383 project = str(datafiles)384 subproject = os.path.join(project, "subproject")385 subsubproject = os.path.join(subproject, "subsubproject")386 shutil.copytree(os.path.join(project, "plugins"), os.path.join(subsubproject, "plugins"))387 update_project(388 project, {"plugins": [{"origin": "junction", "junction": "subproject-junction.bst", plugin_type: ["found"],}]},389 )390 update_project(391 subproject,392 {"plugins": [{"origin": "junction", "junction": "subsubproject-junction.bst", plugin_type: ["found"],}]},393 )394 update_project(395 subsubproject,396 {397 "plugins": [398 {"origin": "local", "path": os.path.join("plugins", plugin_type, "found"), plugin_type: ["found"],}399 ]400 },401 )402 setup_element(project, plugin_type, "found")403 result = cli.run(project=project, args=["show", "element.bst"])404 result.assert_success()405@pytest.mark.datafiles(DATA_DIR)406@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])407def test_junction_deep_plugin_not_found(cli, datafiles, plugin_type):408 project = str(datafiles)409 subproject = os.path.join(project, "subproject")410 subsubproject = os.path.join(subproject, "subsubproject")411 shutil.copytree(os.path.join(project, "plugins"), os.path.join(subsubproject, "plugins"))412 # The toplevel says to search for the "notfound" plugin in the subproject413 #414 update_project(415 project,416 {"plugins": [{"origin": "junction", "junction": "subproject-junction.bst", plugin_type: ["notfound"],}]},417 )418 # The subproject says to search for the "notfound" plugin in the subproject419 #420 update_project(421 subproject,422 {"plugins": [{"origin": "junction", "junction": "subsubproject-junction.bst", plugin_type: ["notfound"],}]},423 )424 # The subsubproject only configures the "found" plugin425 #426 update_project(427 subsubproject,428 {429 "plugins": [430 {"origin": "local", "path": os.path.join("plugins", plugin_type, "found"), plugin_type: ["found"],}431 ]432 },433 )434 setup_element(project, plugin_type, "notfound")435 result = cli.run(project=project, args=["show", "element.bst"])436 result.assert_main_error(ErrorDomain.PLUGIN, "junction-plugin-load-error")437@pytest.mark.datafiles(DATA_DIR)438@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])439@pytest.mark.skipif("not pip_sample_packages()", reason=SAMPLE_PACKAGES_SKIP_REASON)440def test_junction_pip_plugin_found(cli, datafiles, plugin_type):441 project = str(datafiles)442 subproject = os.path.join(project, "subproject")443 shutil.copytree(os.path.join(project, "plugins"), os.path.join(subproject, "plugins"))444 update_project(445 project,446 {"plugins": [{"origin": "junction", "junction": "subproject-junction.bst", plugin_type: ["sample"],}]},447 )448 update_project(449 subproject, {"plugins": [{"origin": "pip", "package-name": "sample-plugins", plugin_type: ["sample"],}]},450 )451 setup_element(project, plugin_type, "sample")452 result = cli.run(project=project, args=["show", "element.bst"])453 result.assert_success()454@pytest.mark.datafiles(DATA_DIR)455@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])456@pytest.mark.skipif("not pip_sample_packages()", reason=SAMPLE_PACKAGES_SKIP_REASON)457def test_junction_pip_plugin_version_conflict(cli, datafiles, plugin_type):458 project = str(datafiles)459 subproject = os.path.join(project, "subproject")460 shutil.copytree(os.path.join(project, "plugins"), os.path.join(subproject, "plugins"))461 update_project(462 project,463 {"plugins": [{"origin": "junction", "junction": "subproject-junction.bst", plugin_type: ["sample"],}]},464 )465 update_project(466 subproject, {"plugins": [{"origin": "pip", "package-name": "sample-plugins>=1.4", plugin_type: ["sample"],}]},467 )468 setup_element(project, plugin_type, "sample")469 result = cli.run(project=project, args=["show", "element.bst"])470 result.assert_main_error(ErrorDomain.PLUGIN, "junction-plugin-load-error")471@pytest.mark.datafiles(DATA_DIR)472@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])473def test_junction_full_path_found(cli, datafiles, plugin_type):474 project = str(datafiles)475 subproject = os.path.join(project, "subproject")476 subsubproject = os.path.join(subproject, "subsubproject")477 shutil.copytree(os.path.join(project, "plugins"), os.path.join(subsubproject, "plugins"))478 update_project(479 project,480 {481 "plugins": [482 {483 "origin": "junction",484 "junction": "subproject-junction.bst:subsubproject-junction.bst",485 plugin_type: ["found"],486 }487 ]488 },489 )490 update_project(491 subsubproject,492 {493 "plugins": [494 {"origin": "local", "path": os.path.join("plugins", plugin_type, "found"), plugin_type: ["found"],}495 ]496 },497 )498 setup_element(project, plugin_type, "found")499 result = cli.run(project=project, args=["show", "element.bst"])500 result.assert_success()501@pytest.mark.datafiles(DATA_DIR)502@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])503def test_junction_full_path_not_found(cli, datafiles, plugin_type):504 project = str(datafiles)505 subproject = os.path.join(project, "subproject")506 subsubproject = os.path.join(subproject, "subsubproject")507 shutil.copytree(os.path.join(project, "plugins"), os.path.join(subsubproject, "plugins"))508 # The toplevel says to search for the "notfound" plugin in the subproject509 #510 update_project(511 project,512 {513 "plugins": [514 {515 "origin": "junction",516 "junction": "subproject-junction.bst:subsubproject-junction.bst",517 plugin_type: ["notfound"],518 }519 ]520 },521 )522 # The subsubproject only configures the "found" plugin523 #524 update_project(525 subsubproject,526 {527 "plugins": [528 {"origin": "local", "path": os.path.join("plugins", plugin_type, "found"), plugin_type: ["found"],}529 ]530 },531 )532 setup_element(project, plugin_type, "notfound")533 result = cli.run(project=project, args=["show", "element.bst"])534 result.assert_main_error(ErrorDomain.PLUGIN, "junction-plugin-not-found")535@pytest.mark.datafiles(DATA_DIR)536@pytest.mark.parametrize(537 "plugin_type,provenance",538 [("elements", "project.conf [line 10 column 2]"), ("sources", "project.conf [line 10 column 2]")],539)540def test_junction_invalid_full_path(cli, datafiles, plugin_type, provenance):541 project = str(datafiles)542 subproject = os.path.join(project, "subproject")543 subsubproject = os.path.join(subproject, "subsubproject")544 shutil.copytree(os.path.join(project, "plugins"), os.path.join(subsubproject, "plugins"))545 # The toplevel says to search for the "notfound" plugin in the subproject546 #547 update_project(548 project,549 {550 "plugins": [551 {552 "origin": "junction",553 "junction": "subproject-junction.bst:pony-junction.bst",554 plugin_type: ["notfound"],555 }556 ]557 },558 )559 setup_element(project, plugin_type, "notfound")560 result = cli.run(project=project, args=["show", "element.bst"])561 result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.MISSING_FILE)...
project.py
Source:project.py
1from app import app2from flask import jsonify, request, Response3from helpers.data_functions import *4from helpers.db_helpers import run_query5@app.get('/api/project')6def project_get():7 # org owners can view all projects within their company8 #proj_managers and employees can view all projects they are associated to 9 token = request.headers.get('token')10 params = request.args11 auth_level = get_authorization(token)12 if auth_level == 'invalid':13 return jsonify('ERROR, invalid token submitted'), 40014 15 if auth_level == 'org_owner' or auth_level == 'proj_man' or auth_level == 'employee':16 if len(params.keys()) == 0:17 18 user_id = run_query('SELECT user_id FROM user_session WHERE token=? ', [token])19 #get all company projects20 company_id = run_query('SELECT company_id FROM user WHERE id=? ', [user_id[0][0]])21 project = run_query('SELECT project_id FROM company WHERE id =?', [company_id[0][0]])22 project_info = run_query('SELECT * FROM project WHERE id=?', [project[0][0]])23 24 get_project_info = []25 info = populate_project_dict(project_info[0])26 get_project_info.append(info)27 28 return jsonify(get_project_info)29 if len(params.keys()) ==1:30 project_id = params.get('project_id')31 project_info = run_query('SELECT * FROM project WHERE id=?', [project_id])32 33 get_single_project = []34 info = populate_project_dict(project_info[0])35 get_single_project.append(info)36 return jsonify(get_single_project)37 38 return 39@app.post('/api/project')40def project_post():41 # only org_owners can create new projects and assign proj_managers42 data = request.json43 params = request.args44 token = params.get('token')45 46 auth_level = get_authorization(token)47 if auth_level == 'invalid':48 return jsonify('ERROR, invalid token submitted'), 40049 50 if auth_level != 'org_owner':51 return jsonify('ERROR, not authorized to create a project'), 40152 53 if len(data.keys()) >= 3 and len(data.keys()) <= 7:54 new_project = new_dictionary_request(data)55 else:56 return jsonify('ERROR, invalid amount of submitted keys'), 40057 58 # Required keys to have are: name, estimated_completion_date, project_manager59 # Additional accepted keys are: description60 61 if 'name' in new_project:62 if not check_length(new_project['name'], 1, 75):63 return jsonify('ERROR, project name submitted is an invalid length, must be between 1 and 75 characters')64 else:65 return jsonify('ERROR, name is required to create a project'), 40166 67 if 'estimated_completion_date' in new_project:68 # Regex to check if the syntax submitted for the estimated project completion date is correctly formatted69 if not check_date(new_project['estimated_completion_date']):70 return jsonify('ERROR, date syntax is incorrectly formatted. Try XXXX-XX-XX'), 40071 else:72 return jsonify('ERROR, estimated_completion_date is required to complete a project'), 40173 74 if 'project_manager' in new_project:75 # check that the user id selected is of level 2 authorization76 # use reference table to compare the id with authorization77 auth_level = run_query('SELECT authorization FROM user WHERE id=?', [new_project['project_manager']])78 level_name = run_query('SELECT level FROM authorization INNER JOIN user ON authorization.id = user.authorization WHERE authorization.id=?', [auth_level[0][0]])79 if level_name[0][0] != 'proj_man':80 return jsonify('ERROR, project managers must be appropriate authorization level "2"')81 82 if not 'description' in new_project:83 run_query('INSERT INTO project (name, estimated_completion_date, project_manager) VALUES (?,?,?) ', \84 [new_project['name'], new_project['estimated_completion_date'], new_project['project_manager']])85 86 request_new_project = run_query('SELECT id, name, start_date, estimated_completion_date, description, project_manager, status_id FROM project WHERE name=?', [new_project['name']])87 88 list_new_project = []89 project = populate_project_dict(request_new_project[0])90 list_new_project.append(project)91 92 return jsonify('New Project Created,', list_new_project[0]), 20193 else:94 if 'description' in new_project:95 if not check_length(new_project['description'], 1, 200):96 return jsonify('ERROR, description must be between 1 and 200 characters')97 run_query('INSERT INTO project (name, estimated_completion_date, project_manager, description) VALUES (?,?,?,?) ', \98 [new_project['name'], new_project['estimated_completion_date'], new_project['project_manager'], new_project['description']])99 request_new_project = run_query('SELECT id, name, start_date, estimated_completion_date, description, project_manager, status_id FROM project WHERE name=?', [new_project['name']])100 list_new_project = []101 project = populate_project_dict(request_new_project[0])102 list_new_project.append(project)103 return jsonify('New Project Created,', list_new_project[0]), 201104 105 106@app.patch('/api/project')107def project_patch():108 #only org_owners and proj managers can update projects109 #proj_managers have a select amount of updates they can run compared to org_owners110 #TODO: ADD updates for project information on both accounts111 112 token = request.headers.get('token')113 params = request.args114 data = request.json115 project_id = params.get('project_id')116 117 auth_level = get_authorization(token)118 if auth_level == 'invalid':119 return jsonify('ERROR, invalid token submitted'), 400120 if auth_level == 'org_owner':121 if len(data.keys()) >= 1 and len(data.keys()) <=2:122 update_project = new_dictionary_request(data)123 if 'status_id' in update_project:124 if str(update_project['status_id']).isdigit() == False:125 return jsonify('ERROR, status_id must be a digit between 1 and 3')126 127 if not 'project_manager' in update_project:128 run_query('UPDATE project SET status_id=? WHERE id=?', [update_project['status_id'], project_id])129 130 elif 'project_manager' in update_project:131 auth_level = run_query('SELECT authorization FROM user WHERE id=?', [update_project['project_manager']])132 level_name = run_query('SELECT level FROM authorization INNER JOIN user ON authorization.id = user.authorization WHERE authorization.id=?', [auth_level[0][0]])133 if level_name[0][0] != 'proj_man':134 return jsonify('ERROR, project managers must be appropriate authorization level "2"')135 run_query('UPDATE project SET status_id=?, project_manager=? WHERE id=?', [update_project['status_id'], update_project['project_manager'], project_id])136 137 elif auth_level == 'proj_man':138 139 update_project = new_dictionary_request(data)140 if 'status_id' in update_project:141 if str(update_project['status_id']).isdigit() == False:142 return jsonify('ERROR, status_id must be a digit between 1 and 3')143 run_query('UPDATE project SET status_id=? WHERE id=?', [update_project['status_id'], project_id])144 145 146 request_updated_project = run_query('SELECT id, name, start_date, estimated_completion_date, description, project_manager, status_id FROM project WHERE id=?', [project_id])147 148 list_updated_project = []149 project = populate_project_dict(request_updated_project[0])150 list_updated_project.append(project)...
urls.py
Source:urls.py
1from django.urls import path, include2from .views import projects, single_project, create_project, update_project, delete_project3urlpatterns = [4 5 path('',projects,name="projects"),6 path('single_project/<str:pk>/',single_project,name="single_project"),7 path('create-project/', create_project,name="create_project"),8 path('update_project/<str:pk>/', update_project, name="update_project"),9 path('delete_project/<str:pk>/', delete_project, name="delete_project")...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!