Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions asset/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,26 @@ strong,table thead th {
#footer p {
margin:0px;
}
/* Ensure the form is displayed inline */
form {
display: inline;
}

/* Delete Button Styles */
.delete-button {
color: red;
background-color: transparent;
border: none;
cursor: pointer;
font-size: 18px;
padding: 0;
}

.delete-button i {
color: red;
font-size: 18px;
}

.delete-customer .icon {
color: red;
}
40 changes: 39 additions & 1 deletion asset/js/main.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var app = app || {};

// Initialize JQuery.
(function($) {

// Pager controller.
app.pager = function(params) {
this.name = params['name'];
Expand Down Expand Up @@ -166,6 +166,7 @@ var app = app || {};
crossDomain:false
}).done(function(data,status,request) {
output = data;
console.log('Fetched contacts:', output); // Log the fetched contacts
});
if(output!==null) {
switch(output['status']) {
Expand Down Expand Up @@ -211,6 +212,7 @@ var app = app || {};
thead.append(thead_row);
$.each(this.items,function(index,item) {
var tbody_row = `

<tr>
<td>
<p><a href="/customer/`+item['id']+`/dashboard">`+item['name']+`</a></p>
Expand All @@ -223,12 +225,48 @@ var app = app || {};
<a href="/customer/`+item['id']+`/update">
<i class="fa-solid fa-pen-clip icon"></i>
</a>
<a href="javascript:void(0);" class="delete-customer" data-id="`+item['id']+`">
<i class="fa-solid fa-trash-can icon"></i>
</a>
</td>
</tr>
`;
tbody.append(tbody_row);
});
break;

case 'contact':
var thead_row = `
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Email</th>
<th>Phonenumber</th>
<th>Actions</th>
</tr>
`;
thead.append(thead_row);
console.log(this.items); // Log the fetched contact data
$.each(this.items,function(index,item) {
var tbody_row = `
<tr>
<td>
<p><a href="/customer/`+item['customer_id']+`/dashboard">`+item['firstname']+ ' ' +item['lastname']+`</a></p>
<p class="paragraph">`+item['description']+`</p>
</td>
<td>`+item['email']+`</td>
<td>`+item['phonenumber']+`</td>
<td>
<a href="/customer/`+item['customer_id']+`/contact/`+item['contact_id']+`/update">
<i class="fa-solid fa-pen-clip icon"></i>
</a>
</td>
</tr>
`;
tbody.append(tbody_row);
});
break;

default:
break;
}
Expand Down
104 changes: 101 additions & 3 deletions controller/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,113 @@
##

# Import community modules.
from flask import render_template
import math
import logging
from decimal import Decimal
from flask import render_template, jsonify, redirect, request

# Import custom modules.
from controller import root_web_controller

from model.customer import customer
from model.contact import contact

# Contact web controller.
class contact_web_controller(root_web_controller):

# HTTP GET method processor.
def get(self,*args,**kwargs):
return render_template('main.html',var=self.var)
if self.request.path == '/contacts':
if self.request.args.get('count'):
contacts = contact().count()
if contacts>0:
limit = Decimal(5.0)
pages = math.ceil(contacts/limit)
return jsonify({'status': 'success', 'result': {'count': contacts, 'pages': int(pages)}})
else:
return jsonify({'status': 'failure', 'message': 'Not available.'})
else:
limit = 5
offset = (int(self.request.args.get('page'))*limit)-limit if self.request.args.get('page') else 0
contacts = contact().list(offset=offset,limit=limit)
return jsonify({'status':'success','result': {'items': contacts}})
elif self.request.path=='/customer/'+args[0]+'/dashboard':
customer_id = args[0]
self.var['customer'] = customer().get(customer_id)
self.var['contacts'] = contact().list_for_customer(customer_id)
print("Fetched contacts for customer {}: {}".format(customer_id, self.var['contacts'])) # Debugging line
return render_template('customer/dashboard.html', var=self.var)
elif self.request.path=='/customer/'+args[0]+'/contact/create':
customer_id = args[0]
self.var['customer'] = customer().get(customer_id)
self.var['contacts'] = contact().list_for_customer(customer_id)
print self.var['customer']
return render_template('contact/create.html',var=self.var)
elif len(args) < 2:
self.var['error_message'] = "Missing customer or contact ID."
return render_template('error.html', var=self.var)
elif self.request.path=='/customer/'+args[0]+'/contact/'+args[1]+'/update':
contact_id = args[1]
customer_id = args[0]
if 'contact' in self.var:
print(self.var['contact'])
else:
print("Contact data not found!")
self.var['contact'] = contact().get(customer_id, contact_id)
self.var['customer'] = customer().get(customer_id)
return render_template('contact/update.html', var=self.var)
elif len(args) < 2:
self.var['error_message'] = "Missing customer or contact ID."
return render_template('error.html', var=self.var)
elif self.request.path == '/customer/'+args[0] +'/contact/'+args[1]+'/delete':
customer_id = args[0]
contact_id = args[1]
if contact().delete(contact_id):
return redirect('/customer/'+args[0]+'/dashboard')
else:
self.var['error_message'] = "There was an issue deleting the contact."
return render_template('error.html', var=self.var)

# HTTP POST method processor.
def post(self, *args, **kwargs):
if self.request.path=='/customer/'+args[0]+'/contact/create':
data = {
'firstname': self.request.form.get('firstname'),
'lastname': self.request.form.get('lastname'),
'email': self.request.form.get('email'),
'phonenumber': self.request.form.get('phonenumber'),
'description': self.request.form.get('description'),
}
if contact().create(args[0], data) is True:
return redirect('/customer/'+ args[0] +'/dashboard')
else:
self.var['error_message'] = "There was an issue creating the contact."
return render_template('error.html', var=self.var)
elif self.request.path =='/customer/'+args[0]+'/contact/'+args[1]+'/update':
data = {
'contact_id': args[1],
'firstname': self.request.form.get('firstname'),
'lastname': self.request.form.get('lastname'),
'email': self.request.form.get('email'),
'phonenumber': self.request.form.get('phonenumber'),
'description': self.request.form.get('description'),
'customer_id': args[0]
}
data['contact_id'] = args[1]
if contact().update(data) is True:
self.var['contact'] = contact().get(args[0], args[1])
self.var['contacts'] = contact().list_for_customer(args[0])
self.var['customer'] = customer().get(args[0])
return redirect('/customer/'+args[0]+'/dashboard')
else:
return render_template('error.html', var=self.var)

elif self.request.path =='/customer/'+args[0]+'/contact/'+args[1]+'/delete':
customer_id = args[0]
contact_id = args[1]
if contact().delete(customer_id, contact_id):
return redirect('/customer/'+args[0]+'/dashboard')
else:
self.var['error_message'] = "There was an issue deleting the contact."
return render_template('error.html', var=self.var)
else:
return render_template('error.html', var=self.var)
44 changes: 39 additions & 5 deletions controller/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
import math
from decimal import Decimal
from flask import render_template,jsonify,redirect
from flask import session


# Import custom modules.
from controller import root_web_controller
from model.customer import customer

from model.contact import contact # Add this import

# Customer web controller.
class customer_web_controller(root_web_controller):

# HTTP GET method processor.
def get(self,*args,**kwargs):
user_id = self._get_authenticated_user_id()
if self.request.path=='/customers':
if self.request.args.get('count'):
customers = customer().count()
Expand All @@ -32,7 +35,7 @@ def get(self,*args,**kwargs):
else:
limit = 5
offset = (int(self.request.args.get('page'))*limit)-limit if self.request.args.get('page') else 0
customers = customer().list(offset=offset,limit=limit)
customers = customer().list_for_user(user_id, offset=offset, limit=limit)
return jsonify({'status':'success','result':{'items':customers}})
elif self.request.path=='/customer/create':
return render_template('customer/create.html',var=self.var)
Expand All @@ -42,8 +45,14 @@ def get(self,*args,**kwargs):
return render_template('customer/update.html',var=self.var)
elif self.request.path=='/customer/'+args[0]+'/dashboard':
customer_id = args[0]
self.var['customer'] = customer().get(customer_id)
return render_template('customer/dashboard.html',var=self.var)
customer_data = customer().get(customer_id)
if customer_data and customer_data['user_id'] == user_id:
self.var['customer'] = customer_data
contacts = contact().list_for_customer(customer_id)
self.var['contacts'] = contacts
return render_template('customer/dashboard.html',var=self.var)
else:
return redirect('/login')
else:
return render_template('error.html',var=self.var)

Expand All @@ -57,7 +66,9 @@ def post(self,*args,**kwargs):
'website':self.request.form.get('website'),
'description':self.request.form.get('description')
}
if customer().create(data) is True:
user_id = self._get_authenticated_user_id()
# if customer().create(data) is True:
if customer().create(data, user_id) is True:
return redirect('/dashboard')
else:
return render_template('error.html',var=self.var)
Expand All @@ -74,5 +85,28 @@ def post(self,*args,**kwargs):
return redirect('/dashboard')
else:
return render_template('error.html',var=self.var)

elif self.request.path == '/customer/' + args[0] + '/delete':
user_id = self._get_authenticated_user_id()
customer_id = args[0]
customer_data = customer().get(customer_id)
if customer_data and customer_data['user_id'] == user_id:
try:
if customer().delete(customer_id):
return jsonify({'status': 'success', 'message': 'Customer deleted successfully.', 'refresh': True})
else:
return jsonify({'status': 'failure', 'message': 'Failed to delete customer.'})
except Exception as e:
app.logger.error("Error occurred while deleting customer {}: {}".format(customer_id, str(e)))
return jsonify({'status': 'failure', 'message': "Error occurred while deleting customer: {}".format(str(e))})
else:
return jsonify({'status': 'failure', 'message': 'Customer not found or you do not have permission.'})
else:
return render_template('error.html',var=self.var)

def _get_authenticated_user_id(self):
user_id = session.get('user_id')
if not user_id:
return None
# raise Exception("User is not authenticated")
return user_id
Loading