Skip to content

Commit

Permalink
models: renaming of foreign keys
Browse files Browse the repository at this point in the history
* Renames foreign keys from `id_*` to `*_id`.

Signed-off-by: Alizee Pace <alizee.pace@gmail.com>
  • Loading branch information
alizeepace authored and jirikuncar committed Sep 28, 2016
1 parent 626baa2 commit b7e9e5d
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ Invenio module that adds support for communities.
- Sami Hiltunen <sami.mikael.hiltunen@cern.ch>
- Tibor Simko <tibor.simko@cern.ch>
- Yoan Blanc <yoan.blanc@cern.ch>
- Alizee Pace <alizee.pace@gmail.com>
6 changes: 3 additions & 3 deletions invenio_communities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ class InclusionRequestModelView(ModelView):
can_delete = True
can_view_details = True
column_list = (
'id_community',
'id_record',
'community_id',
'record_id',
'expires_at',
'id_user'
'user_id'
)


Expand Down
32 changes: 16 additions & 16 deletions invenio_communities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ class InclusionRequest(db.Model, Timestamp):

__tablename__ = 'communities_community_record'

id_community = db.Column(
community_id = db.Column(
db.String(100),
db.ForeignKey('communities_community.id'),
primary_key=True
)
"""Id of the community to which the record is applying."""

id_record = db.Column(
record_id = db.Column(
UUIDType,
db.ForeignKey(RecordMetadata.id),
primary_key=True
)
"""Id of the record applying to given community."""

id_user = db.Column(
user_id = db.Column(
db.Integer,
db.ForeignKey(User.id),
nullable=True,
Expand All @@ -88,20 +88,20 @@ class InclusionRequest(db.Model, Timestamp):
# Relationships
#
community = db.relationship(
'Community', backref='inclusion_requests', foreign_keys=[id_community])
'Community', backref='inclusion_requests', foreign_keys=[community_id])
"""Relation to the community to which the inclusion request is made."""

record = db.relationship(
RecordMetadata, backref='inclusion_requests', foreign_keys=[id_record])
RecordMetadata, backref='inclusion_requests', foreign_keys=[record_id])
"""Relation to the record which is requesting for community inclusion."""

user = db.relationship(
User, backref='inclusion_requests', foreign_keys=[id_user])
User, backref='inclusion_requests', foreign_keys=[user_id])
"""Relation to the User making the inclusion request."""

def get_record(self):
"""Return the API object for the Record."""
return Record.get_record(self.id_record)
return Record.get_record(self.record_id)

def delete(self):
"""Delete this request."""
Expand Down Expand Up @@ -129,8 +129,8 @@ def create(cls, community, record, user=None, expires_at=None,
# Create inclusion request
with db.session.begin_nested():
obj = cls(
id_community=community.id,
id_record=record.id,
community_id=community.id,
record_id=record.id,
user=user,
expires_at=expires_at
)
Expand All @@ -152,13 +152,13 @@ def create(cls, community, record, user=None, expires_at=None,
def get(cls, community_id, record_uuid):
"""Get an inclusion request."""
return cls.query.filter_by(
id_record=record_uuid, id_community=community_id
record_id=record_uuid, community_id=community_id
).one_or_none()

@classmethod
def get_by_record(cls, record_uuid):
"""Get inclusion requests for a given record."""
return cls.query.filter_by(id_record=record_uuid)
return cls.query.filter_by(record_id=record_uuid)


class Community(db.Model, Timestamp):
Expand All @@ -169,7 +169,7 @@ class Community(db.Model, Timestamp):
id = db.Column(db.String(100), primary_key=True)
"""Id of the community."""

id_user = db.Column(
user_id = db.Column(
db.Integer,
db.ForeignKey(User.id),
nullable=False
Expand Down Expand Up @@ -208,7 +208,7 @@ class Community(db.Model, Timestamp):
# Relationships
#
owner = db.relationship(User, backref='communities',
foreign_keys=[id_user])
foreign_keys=[user_id])
"""Relation to the owner (User) of the community."""

def __repr__(self):
Expand All @@ -219,7 +219,7 @@ def __repr__(self):
def create(cls, community_id, user_id, **data):
"""Get a community."""
with db.session.begin_nested():
obj = cls(id=community_id, id_user=user_id, **data)
obj = cls(id=community_id, user_id=user_id, **data)
db.session.add(obj)
return obj

Expand All @@ -243,7 +243,7 @@ def get(cls, community_id, with_deleted=False):
def get_by_user(cls, user_id, with_deleted=False):
"""Get a community."""
query = cls.query.filter_by(
id_user=user_id
user_id=user_id
)
if not with_deleted:
query = query.filter(cls.deleted_at.is_(None))
Expand Down Expand Up @@ -459,7 +459,7 @@ class FeaturedCommunity(db.Model, Timestamp):
id = db.Column(db.Integer, primary_key=True)
"""Id of the featured entry."""

id_community = db.Column(
community_id = db.Column(
db.String(100), db.ForeignKey(Community.id), nullable=False)
"""Id of the featured community."""

Expand Down
2 changes: 1 addition & 1 deletion invenio_communities/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, community, action):

def can(self):
"""Grant permission if owner or admin."""
return str(current_user.get_id()) == str(self.community.id_user) or \
return str(current_user.get_id()) == str(self.community.user_id) or \
DynamicPermission(ActionNeed('admin-access')).can()


Expand Down
2 changes: 1 addition & 1 deletion invenio_communities/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def inject_provisional_community(sender, json=None, record=None, index=None,
return

json['provisional_communities'] = list(sorted([
r.id_community for r in InclusionRequest.get_by_record(record.id)
r.community_id for r in InclusionRequest.get_by_record(record.id)
]))


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ <h4>
<div class="pull-right">
&nbsp;
<a href="{{url_for('.detail', community_id=obj.id)}}" class="btn btn-info">{{ _('View') }}</a>
{% if obj.id_user == current_user.id %}
{% if obj.user_id == current_user.id %}
<a href="{{url_for('.curate', community_id=obj.id)}}" class="btn btn-info">{{ _('Curate') }}</a>
{% endif %}
</div>
Expand Down
2 changes: 1 addition & 1 deletion invenio_communities/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def get(self, community_id):
Content-Length: 334
Content-Type: application/json
{
"id_user": 1,
"user_id": 1,
"description": "",
"title": "",
"created": "2016-04-05T14:56:37.051462",
Expand Down
2 changes: 1 addition & 1 deletion invenio_communities/views/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def generic_item(community, template, **extra_ctx):
# Check existence of community
ctx = mycommunities_ctx()
ctx.update({
'is_owner': community.id_user == current_user.get_id(),
'is_owner': community.user_id == current_user.get_id(),
'community': community,
'detail': True,
})
Expand Down
4 changes: 2 additions & 2 deletions tests/test_invenio_communities.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ def test_model_featured_community(app, db, communities):
t1 = datetime.now()

# Create two community featurings starting at different times
fc1 = FeaturedCommunity(id_community=comm1.id,
fc1 = FeaturedCommunity(community_id=comm1.id,
start_date=t1 + timedelta(days=1))
fc2 = FeaturedCommunity(id_community=comm2.id,
fc2 = FeaturedCommunity(community_id=comm2.id,
start_date=t1 + timedelta(days=3))
db_.session.add(fc1)
db_.session.add(fc2)
Expand Down

0 comments on commit b7e9e5d

Please sign in to comment.