Skip to content

Commit

Permalink
adding categories main page
Browse files Browse the repository at this point in the history
  • Loading branch information
caseybecking committed Nov 18, 2024
1 parent 9fc4ff0 commit a3a79b1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 19 deletions.
4 changes: 4 additions & 0 deletions api/categories/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class CategoriesModel(Base):
name = db.Column(db.String(255), nullable=False)
categories_group_id = db.Column('categories_group_id', db.Text, db.ForeignKey('categories_group.id'), nullable=False)
categories_type_id = db.Column('categories_type_id', db.Text, db.ForeignKey('categories_type.id'), nullable=False)
categories_group = db.relationship('CategoriesGroupModel', backref='categories')
categories_type = db.relationship('CategoriesTypeModel', backref='categories')

def __init__(self, user_id, categories_group_id, categories_type_id, name):
"""
Expand Down Expand Up @@ -54,6 +56,8 @@ def to_dict(self):
'user_id': self.user_id,
'categories_group_id': self.categories_group_id,
'categories_type_id': self.categories_type_id,
'categories_group': self.categories_group.to_dict(),
'categories_type': self.categories_type.to_dict(),
'name': self.name,
'created_at': self.created_at,
'updated_at': self.updated_at
Expand Down
4 changes: 3 additions & 1 deletion app/categories/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ def categories():
str: Rendered HTML template for the categories page.
"""
api_url = url_for('categories', _external=True)
_categories_group = requests.get(url_for('categories_group', _external=True),timeout=15).json().get('categories_group', [])
_categories_type = requests.get(url_for('categories_type', _external=True), timeout=15).json().get('categories_type', [])
response = requests.get(api_url, timeout=15)
_categories = response.json().get('categories', [])
user_id = session.get('_user_id')
return render_template('categories/index.html', categories=_categories, user_id=user_id)
return render_template('categories/index.html', categories=_categories, user_id=user_id, categories_group=_categories_group, categories_type=_categories_type)

@categories_blueprint.route('/categories/group')
@login_required
Expand Down
15 changes: 12 additions & 3 deletions app/institution_account/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@
from flask_login import login_required
import requests


institution_account_blueprint = Blueprint('institution_account', __name__)

@institution_account_blueprint.route('/account')
@login_required
def institution_account():
"""
Render the institution account page.
This view function fetches the institution accounts and institutions from external APIs
and renders the institution_account/index.html template with the fetched data and the user ID
from the session.
Returns:
str: Rendered HTML template for the institution account page.
"""
api_url = url_for('institution_account', _external=True)
response = requests.get(api_url)
response = requests.get(api_url, timeout=15)
accounts = response.json().get('accounts', [])
user_id = session.get('_user_id')
_istitutions = requests.get(url_for('institution', _external=True)).json().get('institutions', [])
_istitutions = requests.get(url_for('institution', _external=True), timeout=15).json().get('institutions', [])

return render_template('institution_account/index.html', accounts=accounts, user_id=user_id, institutions=_istitutions)
34 changes: 34 additions & 0 deletions app/static/js/categories/categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function categoriesFormSubmit(event) {
event.preventDefault();

const categoryName = document.getElementById('categoriesName').value;
const categoriesGroupId = document.getElementById('categoriesGroupId').value;
const categoriesTypeId = document.getElementById('categoriesTypeId').value;
const user_id = document.getElementById('user_id').value;


const data = {
name: categoryName,
categories_group_id: categoriesGroupId,
categories_type_id: categoriesTypeId,
user_id: user_id

};

fetch('/api/categories', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
// redirect to the categories page
window.location.href = '/categories';
})
.catch((error) => {
console.error('Error:', error);
});
}
49 changes: 34 additions & 15 deletions app/templates/categories/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,40 @@ <h5 class="modal-title" id="CategoriesModalgridLabel">Add
<div class="row g-3">
<div class="col-xxl-12">
<div>
<label for="institutionName" class="form-label">Institution Name</label>
<label for="categoriesName" class="form-label">Category Name</label>
<input type="text" class="form-control"
id="institutionName"
placeholder="Enter Institution Name">
id="categoriesName"
placeholder="Enter Category Name">
</div>
</div>
<div class="col-xxl-12">
<label for="institutionLocation"
class="form-label">Institution Location</label>
<input type="text" class="form-control"
id="institutionLocation" placeholder="Enter Institution Location">
<div class="row g-3">
<div class="col-xxl-12">
<div>
<label for="categoriesGroupName" class="form-label">Group Name</label>
<select class="form-select" id="categoriesGroupId">
<option selected>Choose...</option>
{% for group in categories_group %}
<option value="{{ group.id }}">{{ group.name }}</option>
{% endfor %}

</select>
</div>
</div>
</div>
<!--end col-->
<div class="col-xxl-12">
<label for="institutionDescription"
class="form-label">Institution Description</label>
<textarea class="form-control" id="institutionDescription" rows="3" placeholder="Enter Institution Description"></textarea>
<div class="row g-3">
<div class="col-xxl-12">
<div>
<label for="categoriesTypeName" class="form-label">Type Name</label>
<select class="form-select" id="categoriesTypeId">
<option selected>Choose...</option>
{% for type in categories_type %}
<option value="{{ type.id }}">{{ type.name }}</option>
{% endfor %}

</select>
</div>
</div>
</div>
<!--end col-->
<div class="col-lg-12">
<div class="hstack gap-2 justify-content-end">
<button type="button" class="btn btn-light"
Expand Down Expand Up @@ -114,6 +129,8 @@ <h5 class="modal-title" id="CategoriesModalgridLabel">Add
</div>
</th>
<th scope="col">Name</th>
<th scope="col">Group</th>
<th scope="col">Type</th>

<th scope="col" style="width: 150px;">Action</th>
</tr>
Expand All @@ -130,6 +147,8 @@ <h5 class="modal-title" id="CategoriesModalgridLabel">Add
</td>

<td>{{ category.name }}</td>
<td>{{ category.categories_group.name }}</td>
<td>{{ category.categories_type.name }}</td>
<td>
<button type="button" class="btn btn-sm btn-light">Details</button>
</td>
Expand All @@ -155,5 +174,5 @@ <h5 class="modal-title" id="CategoriesModalgridLabel">Add
<!-- end main content-->
{% endblock content %}
{% block extra_js %}
<script src="{{ url_for('static', filename='js/institution/institution.js') }}"></script>
<script src="{{ url_for('static', filename='js/categories/categories.js') }}"></script>
{% endblock extra_js %}

0 comments on commit a3a79b1

Please sign in to comment.