Repository :
http://git.fedorahosted.org/cgit/copr.git
On branch : master
---------------------------------------------------------------
commit 096b627e27fe92e9f0651b5a83d717dc4fe58739
Author: Valentin Gologuzov <vgologuz(a)redhat.com>
Date: Tue Sep 1 15:04:13 2015 +0200
[frontend][api] imports cleanup
---------------------------------------------------------------
frontend/coprs_frontend/coprs/rest_api/common.py | 87 ++++++++++++++++++++
.../coprs/rest_api/resources/build.py | 4 +-
.../coprs/rest_api/resources/build_chroot.py | 19 ++---
.../coprs/rest_api/resources/common.py | 27 ------
.../coprs/rest_api/resources/mock_chroot.py | 11 +--
.../coprs/rest_api/resources/project.py | 74 +++--------------
.../coprs/rest_api/resources/project_chroot.py | 16 ++--
7 files changed, 116 insertions(+), 122 deletions(-)
diff --git a/frontend/coprs_frontend/coprs/rest_api/common.py
b/frontend/coprs_frontend/coprs/rest_api/common.py
new file mode 100644
index 0000000..da57710
--- /dev/null
+++ b/frontend/coprs_frontend/coprs/rest_api/common.py
@@ -0,0 +1,87 @@
+# coding: utf-8
+import base64
+import datetime
+import functools
+from logging import getLogger
+
+log = getLogger(__name__)
+
+from flask import url_for
+import flask
+
+from ..logic.users_logic import UsersLogic
+from .exceptions import AuthFailed
+from .schemas import CoprChrootSchema, BuildSchema, ProjectSchema
+from .util import mm_serialize_one
+
+
+def render_copr_chroot(chroot):
+ return {
+ "chroot": mm_serialize_one(CoprChrootSchema, chroot),
+ "_links": {
+ "project": {"href": url_for(".projectr",
project_id=chroot.copr.id)},
+ "self": {"href": url_for(".projectchrootr",
+ project_id=chroot.copr.id,
+ name=chroot.name)},
+ }
+ }
+
+
+def render_build(build):
+ return {
+ "build": BuildSchema().dump(build)[0],
+ "_links": {
+ "self": {"href": url_for(".buildr",
build_id=build.id)},
+ "project": {"href": url_for(".projectr",
project_id=build.copr_id)},
+ "chroots": {"href":
url_for(".buildchrootlistr", build_id=build.id)}
+ }
+ }
+
+
+def render_project(copr, self_params=None):
+ if self_params is None:
+ self_params = {}
+
+ return {
+ "project": mm_serialize_one(ProjectSchema, copr),
+ "_links": {
+ "self": {"href": url_for(".projectr",
project_id=copr.id, **self_params)},
+ "builds": {"href": url_for(".buildlistr",
project_id=copr.id)},
+ "chroots": {"href":
url_for(".projectchrootlistr", project_id=copr.id)}
+ },
+ }
+
+
+def rest_api_auth_required(f):
+ # todo: move to common.py and test this
+ @functools.wraps(f)
+ def decorated_function(*args, **kwargs):
+ token = None
+ api_login = None
+ try:
+ if "Authorization" in flask.request.headers:
+ base64string = flask.request.headers["Authorization"]
+ base64string = base64string.split()[1].strip()
+ userstring = base64.b64decode(base64string)
+ (api_login, token) = userstring.split(":")
+ except Exception:
+ log.exception("Failed to get auth token from headers")
+ api_login = token = None
+
+ token_auth = False
+ if token and api_login:
+ user = UsersLogic.get_by_api_login(api_login).first()
+ if (user and user.api_token == token and
+ user.api_token_expiration >= datetime.date.today()):
+
+ token_auth = True
+ flask.g.user = user
+ if not token_auth:
+ message = (
+ "Login invalid/expired. "
+ "Please visit
https://copr.fedoraproject.org/api "
+ "get or renew your API token.")
+
+ raise AuthFailed(message)
+ return f(*args, **kwargs)
+ return decorated_function
diff --git a/frontend/coprs_frontend/coprs/rest_api/resources/build.py
b/frontend/coprs_frontend/coprs/rest_api/resources/build.py
index 14f228a..b948a98 100644
--- a/frontend/coprs_frontend/coprs/rest_api/resources/build.py
+++ b/frontend/coprs_frontend/coprs/rest_api/resources/build.py
@@ -11,8 +11,8 @@ from coprs.logic.coprs_logic import CoprsLogic
from coprs.logic.builds_logic import BuildsLogic
from coprs.logic.users_logic import UsersLogic
from coprs.rest_api.exceptions import MalformedRequest, CannotProcessRequest,
AccessForbidden
-from coprs.rest_api.resources.common import render_build
-from coprs.rest_api.resources.project import rest_api_auth_required
+from ..common import render_build, rest_api_auth_required
+
from coprs.rest_api.schemas import BuildSchema, BuildCreateSchema,
BuildCreateFromUrlSchema
diff --git a/frontend/coprs_frontend/coprs/rest_api/resources/build_chroot.py
b/frontend/coprs_frontend/coprs/rest_api/resources/build_chroot.py
index 751ecaa..7d04edd 100644
--- a/frontend/coprs_frontend/coprs/rest_api/resources/build_chroot.py
+++ b/frontend/coprs_frontend/coprs/rest_api/resources/build_chroot.py
@@ -1,22 +1,15 @@
# coding: utf-8
-import json
-import flask
from flask import url_for
-from flask_restful import Resource, reqparse
-
-from marshmallow import Schema, fields, pprint
+from flask_restful import Resource
from ... import models
-from coprs.exceptions import MalformedArgumentException
-from coprs.logic.builds_logic import BuildsLogic, BuildChrootsLogic
-from coprs.rest_api.exceptions import MalformedRequest
-from coprs.rest_api.schemas import MockChrootSchema, CoprChrootSchema, BuildChrootSchema
-
-from coprs.views.misc import api_login_required
-from coprs.logic.coprs_logic import MockChrootsLogic, CoprChrootsLogic, CoprsLogic
+from ...exceptions import MalformedArgumentException
+from ...logic.builds_logic import BuildsLogic, BuildChrootsLogic
-from ..util import get_one_safe, json_loads_safe, mm_deserialize, mm_serialize_one
+from ..exceptions import MalformedRequest
+from ..schemas import BuildChrootSchema
+from ..util import get_one_safe, mm_serialize_one
def render_build_chroot(chroot):
diff --git a/frontend/coprs_frontend/coprs/rest_api/resources/common.py
b/frontend/coprs_frontend/coprs/rest_api/resources/common.py
deleted file mode 100644
index 85ccfd4..0000000
--- a/frontend/coprs_frontend/coprs/rest_api/resources/common.py
+++ /dev/null
@@ -1,27 +0,0 @@
-# coding: utf-8
-from flask import url_for
-from coprs.rest_api.schemas import CoprChrootSchema, BuildSchema
-from coprs.rest_api.util import mm_serialize_one
-
-
-def render_copr_chroot(chroot):
- return {
- "chroot": mm_serialize_one(CoprChrootSchema, chroot),
- "_links": {
- "project": {"href": url_for(".projectr",
project_id=chroot.copr.id)},
- "self": {"href": url_for(".projectchrootr",
- project_id=chroot.copr.id,
- name=chroot.name)},
- }
- }
-
-
-def render_build(build):
- return {
- "build": BuildSchema().dump(build)[0],
- "_links": {
- "self": {"href": url_for(".buildr",
build_id=build.id)},
- "project": {"href": url_for(".projectr",
project_id=build.copr_id)},
- "chroots": {"href":
url_for(".buildchrootlistr", build_id=build.id)}
- }
- }
diff --git a/frontend/coprs_frontend/coprs/rest_api/resources/mock_chroot.py
b/frontend/coprs_frontend/coprs/rest_api/resources/mock_chroot.py
index 0ee1d0d..2255d4b 100644
--- a/frontend/coprs_frontend/coprs/rest_api/resources/mock_chroot.py
+++ b/frontend/coprs_frontend/coprs/rest_api/resources/mock_chroot.py
@@ -1,17 +1,12 @@
# coding: utf-8
-import json
-import flask
from flask import url_for
from flask_restful import Resource, reqparse
-from marshmallow import Schema, fields, pprint
-from coprs.rest_api.schemas import MockChrootSchema
+from ...logic.coprs_logic import MockChrootsLogic
-from coprs.views.misc import api_login_required
-from coprs.logic.coprs_logic import MockChrootsLogic, CoprChrootsLogic, CoprsLogic
-
-from ..util import get_one_safe, json_loads_safe, mm_deserialize
+from ..schemas import MockChrootSchema
+from ..util import get_one_safe
def render_mock_chroot(chroot):
diff --git a/frontend/coprs_frontend/coprs/rest_api/resources/project.py
b/frontend/coprs_frontend/coprs/rest_api/resources/project.py
index 17dc94b..ede6fc2 100644
--- a/frontend/coprs_frontend/coprs/rest_api/resources/project.py
+++ b/frontend/coprs_frontend/coprs/rest_api/resources/project.py
@@ -1,76 +1,24 @@
-import base64
-import datetime
-import functools
from logging import getLogger
-from coprs.logic.builds_logic import BuildsLogic
-from coprs.rest_api.resources.common import render_copr_chroot, render_build
log = getLogger(__name__)
import flask
from flask import url_for, make_response
from flask_restful import Resource, reqparse
-from marshmallow import pprint
-
-from coprs import db
-from coprs.exceptions import DuplicateException
-from coprs.logic.complex_logic import ComplexLogic
-from coprs.logic.helpers import slice_query
-from coprs.logic.users_logic import UsersLogic
-from coprs.logic.coprs_logic import CoprsLogic
-from coprs.exceptions import ActionInProgressException, InsufficientRightsException
-from coprs.rest_api.schemas import ProjectSchema, ProjectCreateSchema
-from ..exceptions import ObjectAlreadyExists, AuthFailed, CannotProcessRequest,
AccessForbidden
-from ..util import get_one_safe, json_loads_safe, mm_deserialize, render_allowed_method,
mm_serialize_one
-
-
-def rest_api_auth_required(f):
- # todo: move to common.py and test this
- @functools.wraps(f)
- def decorated_function(*args, **kwargs):
- token = None
- api_login = None
- try:
- if "Authorization" in flask.request.headers:
- base64string = flask.request.headers["Authorization"]
- base64string = base64string.split()[1].strip()
- userstring = base64.b64decode(base64string)
- (api_login, token) = userstring.split(":")
- except Exception:
- log.exception("Failed to get auth token from headers")
- api_login = token = None
-
- token_auth = False
- if token and api_login:
- user = UsersLogic.get_by_api_login(api_login).first()
- if (user and user.api_token == token and
- user.api_token_expiration >= datetime.date.today()):
-
- token_auth = True
- flask.g.user = user
- if not token_auth:
- message = (
- "Login invalid/expired. "
- "Please visit
https://copr.fedoraproject.org/api "
- "get or renew your API token.")
-
- raise AuthFailed(message)
- return f(*args, **kwargs)
- return decorated_function
-
-
-def render_project(copr, self_params=None):
- if self_params is None:
- self_params = {}
- return {
- "project": mm_serialize_one(ProjectSchema, copr),
- "_links": {
- "self": {"href": url_for(".projectr",
project_id=copr.id, **self_params)},
- "builds": {"href": url_for(".buildlistr",
project_id=copr.id)},
- "chroots": {"href":
url_for(".projectchrootlistr", project_id=copr.id)}
- },
- }
+from ... import db
+from ...logic.builds_logic import BuildsLogic
+from ...logic.complex_logic import ComplexLogic
+from ...logic.helpers import slice_query
+from ...logic.coprs_logic import CoprsLogic
+from ...exceptions import ActionInProgressException, InsufficientRightsException
+
+from ...exceptions import DuplicateException
+
+from ..common import rest_api_auth_required, render_copr_chroot, render_build,
render_project
+from ..schemas import ProjectSchema, ProjectCreateSchema
+from ..exceptions import ObjectAlreadyExists, CannotProcessRequest, AccessForbidden
+from ..util import get_one_safe, mm_deserialize
class ProjectListR(Resource):
diff --git a/frontend/coprs_frontend/coprs/rest_api/resources/project_chroot.py
b/frontend/coprs_frontend/coprs/rest_api/resources/project_chroot.py
index 29f6114..d9043a7 100644
--- a/frontend/coprs_frontend/coprs/rest_api/resources/project_chroot.py
+++ b/frontend/coprs_frontend/coprs/rest_api/resources/project_chroot.py
@@ -2,8 +2,6 @@
import logging
-from coprs.rest_api.resources.common import render_copr_chroot
-
log = logging.getLogger(__name__)
import flask
@@ -11,16 +9,16 @@ from flask import url_for, make_response
from flask_restful import Resource
from sqlalchemy.exc import IntegrityError
-from coprs.exceptions import InsufficientRightsException, MalformedArgumentException
-from coprs.rest_api.exceptions import AccessForbidden, MalformedRequest,
ObjectAlreadyExists, \
- ServerError
-from coprs.rest_api.resources.project import rest_api_auth_required
-from coprs.rest_api.schemas import CoprChrootSchema, CoprChrootCreateSchema
-from coprs.logic.coprs_logic import MockChrootsLogic, CoprChrootsLogic, CoprsLogic
+from ... import db
+from ...logic.coprs_logic import MockChrootsLogic, CoprChrootsLogic, CoprsLogic
+from ...exceptions import InsufficientRightsException, MalformedArgumentException
+from ..exceptions import AccessForbidden, MalformedRequest, \
+ ObjectAlreadyExists, ServerError
+from ..common import rest_api_auth_required, render_copr_chroot
+from ..schemas import CoprChrootSchema, CoprChrootCreateSchema
from ..util import get_one_safe, mm_deserialize
-from ... import db
class ProjectChrootListR(Resource):