Skip to content

Commit

Permalink
Added: optional reciprocal name field for relationship types (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
signebedi committed Apr 14, 2024
1 parent 3612a75 commit ac79dfb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
19 changes: 15 additions & 4 deletions libreforms_fastapi/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2566,6 +2566,11 @@ async def api_admin_relationship_type(
description=escape(new_relationship_request.description),
exclusive=new_relationship_request.exclusive_relationship,
)

# Assign a reciprocal name / title if one was passed
if new_relationship_request.reciprocal_name:
new_relationship_type.reciprocal_name = new_relationship_request.reciprocal_name

session.add(new_relationship_type)
session.commit()

Expand Down Expand Up @@ -2602,7 +2607,7 @@ async def api_admin_get_relationship_types(
):

"""
Lists all relationship tyoes in the system for administrative purposes. Requires site admin permissions.
Lists all relationship types in the system for administrative purposes. Requires site admin permissions.
Logs the action for audit purposes.
"""

Expand Down Expand Up @@ -2658,14 +2663,15 @@ async def api_admin_update_relationship_type(
if not user or not user.site_admin:
raise HTTPException(status_code=404)

existing_relationship_type = session.query(RelationshipType).filter_by(name=relationship_request.name).first()
existing_relationship_type = session.query(RelationshipType).filter_by(id=id).first()
if not existing_relationship_type:
raise HTTPException(status_code=404, detail="Could not update relationship type. Does not exist.")

if all ([
existing_relationship_type.name == relationship_request.name,
existing_relationship_type.description == relationship_request.description,
existing_relationship_type.exclusive == relationship_request.exclusive_relationship,
existing_relationship_type.reciprocal_name == relationship_request.reciprocal_name,
]):
# If no change has been passed, return
return JSONResponse(
Expand All @@ -2675,11 +2681,16 @@ async def api_admin_update_relationship_type(
# raise HTTPException(status_code=304, detail=f"No change made to group with id {id}")


# Updating fields
# Updating fields
existing_relationship_type.name=relationship_request.name
existing_relationship_type.description=relationship_request.description
existing_relationship_type.exclusive=relationship_request.exclusive_relationship


# Assign a reciprocal name / title if one was passed
if relationship_request.reciprocal_name:
existing_relationship_type.reciprocal_name = relationship_request.reciprocal_name


session.add(existing_relationship_type)
session.commit()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
<input type="text" id="name" name="name" class="form-control" required>
</fieldset>

<fieldset style="padding-top: 10px;" class="form-check">
<label aria-labelledby="reciprocalNameHelpInline" for="reciprocalName" class="form-check-label">Reciprocal Name</label>
<span id="reciprocalNameHelpInline" class="form-text">
| Optionally, give a name to describe the reciprocal of this relationship. For example, if this field is called "managedBy", then a reciprocal name could be "managerOf".
</span>
<input type="text" id="reciprocalName" name="reciprocalName" class="form-control">
</fieldset>

<fieldset style="padding-top: 10px;" class="form-check">
<label aria-labelledby="descriptionHelpInline" for="description" class="form-check-label">Description</label>
<span id="descriptionHelpInline" class="form-text">
Expand Down Expand Up @@ -86,6 +94,7 @@
// Prepare form data
var formData = {
name: $('#name').val().trim(),
reciprocal_name: $('#reciprocalName').val().trim(),
description: $('#description').val().trim(),
exclusive_relationship: $('#exclusiveRelationship').is(':checked')
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
// Stash a flashed success message and redirect
setFlashMessage("Relationship created successfully!", "success");
window.location.href = '/ui/admin/manage_relationship_types';
window.location.href = '/ui/admin/manage_user_relationships';
},
error: function(xhr) {
// Handle errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ $(document).ready(function() {
function renderTable(relationshipTypes, tableName) {
var table = `<table class="table table-hover table-striped table-light" id="${tableName}"><thead><tr>`;
// Define the headers based on the required attributes
table += '<th>ID</th><th>Name</th><th>Description</th><th>Exclusive</th>';
table += '<th>ID</th><th>Name</th><th>Reciprocal Name</th><th>Description</th><th>Exclusive</th>';
table += '</tr></thead><tbody>';
// Iterate over the array of users
Expand All @@ -86,6 +86,7 @@ $(document).ready(function() {
</div>
</td>`;
table += '<td>' + relationshipType.reciprocal_name + '</td>';
table += '<td>' + relationshipType.description + '</td>';
table += `<td>
${relationshipType.exclusive == true ? '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check-square-fill" viewBox="0 0 16 16"><path d="M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zm10.03 4.97a.75.75 0 0 1 .011 1.05l-3.992 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.75.75 0 0 1 1.08-.022z"/></svg>' : '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-x-square" viewBox="0 0 16 16"><path d="M14 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2z"/><path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708"/></svg>'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<path d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"/>
</svg>
</sup>

{{relationship_details}}

</h1>
<hr>

Expand All @@ -29,6 +26,14 @@
<input type="text" id="name" name="name" value="{{ relationship_details.name }}" class="form-control" required>
</fieldset>

<fieldset style="padding-top: 10px;" class="form-check">
<label aria-labelledby="reciprocalNameHelpInline" for="reciprocalName" class="form-check-label">Reciprocal Name</label>
<span id="reciprocalNameHelpInline" class="form-text">
| Optionally, give a name to describe the reciprocal of this relationship. For example, if this field is called "managedBy", then a reciprocal name could be "managerOf".
</span>
<input type="text" id="reciprocalName" name="reciprocalName" value="{{ relationship_details.reciprocal_name }}" class="form-control">
</fieldset>

<fieldset style="padding-top: 10px;" class="form-check">
<label aria-labelledby="descriptionHelpInline" for="description" class="form-check-label">Description</label>
<span id="descriptionHelpInline" class="form-text">
Expand Down Expand Up @@ -96,6 +101,7 @@
var formData = {
name: $('#name').val().trim(),
description: $('#description').val().trim(),
reciprocal_name: $('#reciprocalName').val().trim(),
exclusive_relationship: $('#exclusiveRelationship').is(':checked')
};
Expand Down
1 change: 1 addition & 0 deletions libreforms_fastapi/utils/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ def check_colon_in_permission(cls, v):
class RelationshipTypeModel(BaseModel):
"""This model will be used for validating Relationship Types through the admin API"""
name: str = Field(...)
reciprocal_name: str = Field(None)
description: str = Field(...)
exclusive_relationship: bool = Field(...)

Expand Down
2 changes: 2 additions & 0 deletions libreforms_fastapi/utils/sqlalchemy_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ class RelationshipType(Base):
__tablename__ = 'relationship_types'
id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
reciprocal_name = Column(String, default="")
description = Column(String)
exclusive = Column(Boolean, default=False)

Expand All @@ -286,6 +287,7 @@ def to_dict(self):
relationship_dict = {
"id":self.id,
"name":self.name,
"reciprocal_name":self.reciprocal_name,
"description":self.description,
"exclusive":self.exclusive,
}
Expand Down

0 comments on commit ac79dfb

Please sign in to comment.