Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specific files: JS, controllers, models, and views for contact #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
35 changes: 35 additions & 0 deletions asset/js/main.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,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 @@ -229,6 +230,40 @@ var app = app || {};
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);
$.each(this.items,function(index,item) {
var tbody_row = `
<tr>
<td>
<p><a href="/customer/`+item['customer_id']+`/dashboard">`+item['first_name']+``+item['last_name']+`</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>
<a href="/customer/`+item['customer_id']+`/contact/`+item['contact_id']+`/delete">
<i class="fa-solid fa-trash-can icon"></i>
</td>
</tr>
`;
tbody.append(tbody_row);
});
break;

default:
break;
}
Expand Down
100 changes: 98 additions & 2 deletions controller/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,111 @@
##

# 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)
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) #added line
self.var['contacts'] = contact().list_for_customer(customer_id) #added line
print self.var['customer']
return render_template('contact/create.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: # Added line
print(self.var['contact'])
else:
print("Contact data not found!")
self.var['contact'] = contact().get(customer_id, contact_id) # change line
self.var['customer'] = customer().get(customer_id)
return render_template('contact/update.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/' + customer_id + '/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], #added line
'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] #added line
}
data['contact_id'] = args[1]
if contact().update(data) is True:
self.var['contact'] = contact().get(args[0], args[1]) #added line
self.var['contacts'] = contact().list_for_customer(args[0])# added line
self.var['customer'] = customer().get(args[0]) #added line
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)
11 changes: 9 additions & 2 deletions controller/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# 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):
Expand Down Expand Up @@ -42,9 +42,16 @@ 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)
# self.var['customer'] = customer().get(customer_id)
customer_data = customer().get(customer_id)
if customer_data:# Fetch customer data
self.var['customer'] = customer_data
# Fetch contacts for this customer
contacts = contact().list_for_customer(customer_id)
self.var['contacts'] = contacts
return render_template('customer/dashboard.html',var=self.var)
else:

return render_template('error.html',var=self.var)

# HTTP POST method processor.
Expand Down
43 changes: 31 additions & 12 deletions init.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
# Import community modules.
import sys


# Append App specific Python paths.
sys.path.append('model')
sys.path.append('controller')
sys.path.append('helper')

# Import community modules.
import argparse
from flask import Flask,request
from flask import Flask,request, redirect, render_template

# Import custom modules.
from controller.user import user_web_controller
from controller.customer import customer_web_controller
from controller.contact import contact_web_controller

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

# Health web controller.
def health_web_controller():
Expand Down Expand Up @@ -68,26 +71,42 @@ def create_contact(arg_0):
else:
return None

# Update contact.
def update_contact(arg_0,arg_1):
if request.method=='GET':
return contact_web_controller(request).get(str(arg_0),str(arg_1))
elif request.method=='POST':
return contact_web_controller(request).post(str(arg_0),str(arg_1))
def update_contact(arg_0, arg_1):
print("Received Customer ID: {}, Contact ID: {}".format(arg_0, arg_1))
if request.method == 'GET':
return contact_web_controller(request).get(str(arg_0), str(arg_1))
elif request.method == 'POST':
return contact_web_controller(request).post(str(arg_0), str(arg_1))
else:
return None
return None

# List contacts.
def list_contacts():
def list_contacts(arg_0):
if request.method=='GET':
return contact_web_controller(request).get()
return contact_web_controller(request).get(str(arg_0))
else:
return None

# Delete contact route
def delete_contact(arg_0, arg_1):
if request.method == 'POST':
return contact_web_controller(request).post(str(arg_0),str(arg_1))
else:
return None


def customer_dashboard(arg_0):
if request.method == 'GET':
return customer_web_controller(request).get(str(arg_0))
else:
return None



# Initialize Flask app.
app = Flask('CRM app',template_folder='view')
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
# app.secret_key = 'sloopstash'

# App routes.
app.add_url_rule('/health',view_func=health_web_controller)
Expand All @@ -97,8 +116,8 @@ def list_contacts():
app.add_url_rule('/customers',view_func=list_customers)
app.add_url_rule('/customer/<int:arg_0>/contact/create',view_func=create_contact,methods=['GET','POST'])
app.add_url_rule('/customer/<int:arg_0>/contact/<int:arg_1>/update',view_func=update_contact,methods=['GET','POST'])
app.add_url_rule('/customer/<int:arg_0>/contacts',view_func=list_contacts)

app.add_url_rule('/customer/<int:arg_0>/contact/<int:arg_1>/delete', view_func=delete_contact, methods=['POST'])
app.add_url_rule('/customer/<int:arg_0>/dashboard', view_func=customer_dashboard, methods=['GET'])

if __name__=='__main__':
parser = argparse.ArgumentParser()
Expand Down
Loading