forked from Scifabric/pybossa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Scifabric#1925 from Scifabric/add-pages
Add pages to make PYBOSSA a headless CMS
- Loading branch information
Showing
17 changed files
with
1,516 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""add pages table | ||
Revision ID: 66ecf0b2aed5 | ||
Revises: c7476118715f | ||
Create Date: 2019-06-01 16:10:06.519049 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '66ecf0b2aed5' | ||
down_revision = 'c7476118715f' | ||
|
||
import datetime | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects.postgresql import JSON, TIMESTAMP | ||
|
||
|
||
def make_timestamp(): | ||
now = datetime.datetime.utcnow() | ||
return now.isoformat() | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
'pages', | ||
sa.Column('id', sa.Integer, primary_key=True), | ||
sa.Column('slug', sa.Unicode(length=255), nullable=False), | ||
sa.Column('project_id', | ||
sa.Integer, | ||
sa.ForeignKey('project.id', ondelete='CASCADE'), | ||
nullable=False), | ||
sa.Column('created', TIMESTAMP, default=make_timestamp), | ||
sa.Column('info', JSON, nullable=False), | ||
sa.Column('media_url', sa.Text), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table('pages') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf8 -*- | ||
# This file is part of PYBOSSA. | ||
# | ||
# Copyright (C) 2019 Scifabric LTD. | ||
# | ||
# PYBOSSA is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# PYBOSSA is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with PYBOSSA. If not, see <http://www.gnu.org/licenses/>. | ||
""" | ||
PYBOSSA api module for domain object Page via an API. | ||
This package adds GET, POST, PUT and DELETE methods for: | ||
* page | ||
""" | ||
from api_base import APIBase | ||
from pybossa.model.page import Page | ||
from flask_login import current_user | ||
from werkzeug.exceptions import BadRequest | ||
|
||
|
||
class PageAPI(APIBase): | ||
|
||
"""Class API for domain object Page.""" | ||
|
||
reserved_keys = set(['id', 'created']) | ||
|
||
__class__ = Page | ||
|
||
def _forbidden_attributes(self, data): | ||
for key in data.keys(): | ||
if key in self.reserved_keys: | ||
raise BadRequest("Reserved keys in payload") | ||
|
||
def _update_object(self, obj): | ||
if not current_user.is_anonymous(): | ||
obj.user_id = current_user.id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# -*- coding: utf8 -*- | ||
# This file is part of PYBOSSA. | ||
# | ||
# Copyright (C) 2019 Scifabric LTD. | ||
# | ||
# PYBOSSA is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# PYBOSSA is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with PYBOSSA. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
|
||
class PageAuth(object): | ||
_specific_actions = [] | ||
|
||
def __init__(self, project_repo): | ||
self.project_repo = project_repo | ||
|
||
@property | ||
def specific_actions(self): | ||
return self._specific_actions | ||
|
||
def can(self, user, action, page=None, project_id=None): | ||
action = ''.join(['_', action]) | ||
return getattr(self, action)(user, page, project_id) | ||
|
||
def _create(self, user, page=None, project_id=None): | ||
if user.is_anonymous() or (page is None and project_id is None): | ||
return False | ||
project = self._get_project(page, project_id) | ||
if page is None: | ||
return self._is_admin_or_owner(user, project) | ||
return self._is_admin_or_owner(user, project) | ||
|
||
def _read(self, user, page=None, project_id=None): | ||
if page or project_id: | ||
project = self._get_project(page, project_id) | ||
if project: | ||
return (project.published or | ||
self._is_admin_or_owner(user, project)) | ||
if user.is_anonymous() or (page is None and project_id is None): | ||
return False | ||
return self._is_admin_or_owner(user, project) | ||
else: | ||
return True | ||
|
||
def _update(self, user, page, project_id=None): | ||
project = self._get_project(page, project_id) | ||
if user.is_anonymous(): | ||
return False | ||
return self._is_admin_or_owner(user, project) | ||
|
||
def _delete(self, user, page, project_id=None): | ||
project = self._get_project(page, project_id) | ||
if user.is_anonymous(): | ||
return False | ||
return self._is_admin_or_owner(user, project) | ||
|
||
def _get_project(self, page, project_id): | ||
if page is not None: | ||
return self.project_repo.get(page.project_id) | ||
return self.project_repo.get(project_id) | ||
|
||
def _is_admin_or_owner(self, user, project): | ||
return (not user.is_anonymous() and | ||
(project.owner_id == user.id or user.admin)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf8 -*- | ||
# This file is part of PYBOSSA. | ||
# | ||
# Copyright (C) 2019 Scifabric LTD. | ||
# | ||
# PYBOSSA is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# PYBOSSA is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with PYBOSSA. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from sqlalchemy import Integer, Text, Float, Unicode | ||
from sqlalchemy.schema import Column, ForeignKey | ||
from sqlalchemy.dialects.postgresql import TIMESTAMP, JSONB | ||
from pybossa.core import db | ||
from pybossa.model import DomainObject, make_timestamp | ||
from sqlalchemy.ext.mutable import MutableDict | ||
|
||
|
||
class Page(db.Model, DomainObject): | ||
'''A Page objects to provide a headles CMS.''' | ||
|
||
__tablename__ = 'pages' | ||
|
||
#: Counter.ID | ||
id = Column(Integer, primary_key=True) | ||
#: UTC timestamp when the counter was created. | ||
created = Column(TIMESTAMP, default=make_timestamp) | ||
#: Slug the url for the page object | ||
slug = Column(Unicode(length=255), nullable=False) | ||
#: Project.ID that this counter is associated with. | ||
project_id = Column(Integer, ForeignKey('project.id', | ||
ondelete='CASCADE'), | ||
nullable=False) | ||
#: Info field where it can be stored anything related to it | ||
info = Column(MutableDict.as_mutable(JSONB), default=dict()) | ||
#: media URL to store a link to an image or file | ||
media_url = Column(Text) | ||
|
||
@classmethod | ||
def public_attributes(self): | ||
"""Return a list of public attributes.""" | ||
return ['created', 'id', 'info', 'media_url', 'slug'] | ||
|
||
@classmethod | ||
def public_info_keys(self): | ||
"""Return a list of public info keys.""" | ||
pass |
Oops, something went wrong.