From a6ea4c517c56dc476b6c66d853310eb637091ef3 Mon Sep 17 00:00:00 2001 From: SWETHA JAGADEESH Date: Fri, 24 Jan 2025 17:41:14 +0530 Subject: [PATCH] Add specific files: JS, controllers, models, and views for contact --- asset/js/main.js | 35 ++++++++++ controller/contact.py | 100 ++++++++++++++++++++++++++- controller/customer.py | 11 ++- init.py | 43 ++++++++---- model/contact.py | 129 +++++++++++++++++++++++++++++++++++ view/contact/create.html | 64 +++++++++++++++++ view/contact/update.html | 64 +++++++++++++++++ view/customer/dashboard.html | 108 +++++++++++++++++++++++++++++ 8 files changed, 538 insertions(+), 16 deletions(-) mode change 100644 => 100755 asset/js/main.js create mode 100644 view/customer/dashboard.html diff --git a/asset/js/main.js b/asset/js/main.js old mode 100644 new mode 100755 index d6cddf2..6df1778 --- a/asset/js/main.js +++ b/asset/js/main.js @@ -211,6 +211,7 @@ var app = app || {}; thead.append(thead_row); $.each(this.items,function(index,item) { var tbody_row = ` +

`+item['name']+`

@@ -229,6 +230,40 @@ var app = app || {}; tbody.append(tbody_row); }); break; + + case 'contact': + var thead_row = ` + + FirstName + LastName + Email + Phonenumber + Actions + + `; + thead.append(thead_row); + $.each(this.items,function(index,item) { + var tbody_row = ` + + +

`+item['first_name']+``+item['last_name']+`

+

`+item['description']+`

+ + `+item['email']+` + `+item['Phonenumber']+` + + + + + + + + + `; + tbody.append(tbody_row); + }); + break; + default: break; } diff --git a/controller/contact.py b/controller/contact.py index 4d19871..c98838b 100644 --- a/controller/contact.py +++ b/controller/contact.py @@ -6,10 +6,16 @@ ## # 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. @@ -17,4 +23,94 @@ 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) \ No newline at end of file diff --git a/controller/customer.py b/controller/customer.py index 1c03d01..0d67663 100644 --- a/controller/customer.py +++ b/controller/customer.py @@ -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): @@ -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. diff --git a/init.py b/init.py index 9a95ce6..9908636 100644 --- a/init.py +++ b/init.py @@ -8,6 +8,7 @@ # Import community modules. import sys + # Append App specific Python paths. sys.path.append('model') sys.path.append('controller') @@ -15,13 +16,15 @@ # 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(): @@ -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) @@ -97,8 +116,8 @@ def list_contacts(): app.add_url_rule('/customers',view_func=list_customers) app.add_url_rule('/customer//contact/create',view_func=create_contact,methods=['GET','POST']) app.add_url_rule('/customer//contact//update',view_func=update_contact,methods=['GET','POST']) -app.add_url_rule('/customer//contacts',view_func=list_contacts) - +app.add_url_rule('/customer//contact//delete', view_func=delete_contact, methods=['POST']) +app.add_url_rule('/customer//dashboard', view_func=customer_dashboard, methods=['GET']) if __name__=='__main__': parser = argparse.ArgumentParser() diff --git a/model/contact.py b/model/contact.py index e69de29..813d4bc 100644 --- a/model/contact.py +++ b/model/contact.py @@ -0,0 +1,129 @@ +## +# -*- coding: utf-8 -*- +## +## +# Customer model. +## + +# Import community modules. +import json +import logging + +# Import custom modules. +from database import redis + + +# Customer main model. +class contact(object): + + # Initializer. + def __init__(self): + self.redis = redis + + def create(self, customer_id, data): + try: + contact_id = self.redis.engine.incr( + self.redis.conf['key_prefix']['contact']['counter'] + ) + contact_key = "customer:{}:contact:{}".format(customer_id, contact_id) + data['id'] = contact_id + + for key, value in data.items(): + self.redis.engine.hset(contact_key, key, value) + + except Exception as error: + print("Error creating contact: {}".format(error)) + return False + else: + print("Contact created successfully with ID {}".format(contact_id)) + return True + + + # Update contact. + def update(self, data): + try: + contact_id = data['contact_id'] + customer_id = data['customer_id'] + contact_key = "customer:{}:contact:{}".format(customer_id, contact_id) + contact_data = self.redis.engine.hgetall(contact_key) + if not contact_data: + return False + for field, value in data.items(): + self.redis.engine.hset(contact_key, field, value) + except Exception as error: + return False + else: + return True + + # Get contact. + def get(self, customer_id, contact_id): + try: + key = "customer:{}:contact:{}".format(customer_id, contact_id) + data = self.redis.engine.hgetall(key) + if data: + item = { + 'id': contact_id + } + item.update(data) + return item + else: + return None + except redis.ConnectionError as e: + return None + except redis.RedisError as e: + return None + except Exception as e: + return None + + # Count of contact. + def count(self): + count = self.redis.engine.hlen( + self.redis.conf['key_prefix']['contact']['main'] + ) + return count + + # List customers. + def list(self,**kwargs): + offset = kwargs['offset'] if kwargs.has_key('offset') else 0 + limit = kwargs['limit'] if kwargs.has_key('limit') else 5 + data = self.redis.engine.hgetall( + self.redis.conf['key_prefix']['contact']['main'] + ) + items = [] + for key,value in data.items(): + item = { + 'id':key + } + item.update(json.loads(value)) + items.append(item) + return items + + # List for customers + def list_for_customer(self, customer_id): + try: + keys = self.redis.engine.keys("customer:{}:contact:*".format(customer_id)) + if not keys: + return [] + items = [] + for key in keys: + contact_data = self.redis.engine.hgetall(key) + contact_id = key.decode().split(":")[-1] + contact_data['id'] = contact_id + items.append(contact_data) + return items + except Exception as e: + return [] + + + # Delete contact. + def delete(self, customer_id, contact_id): + try: + contact_key = "customer:{}:contact:{}".format(customer_id, contact_id) + if not self.redis.engine.exists(contact_key): + return False + self.redis.engine.delete(contact_key) + return True + except Exception as error: + return False + + \ No newline at end of file diff --git a/view/contact/create.html b/view/contact/create.html index e69de29..5a167c7 100644 --- a/view/contact/create.html +++ b/view/contact/create.html @@ -0,0 +1,64 @@ +{% extends 'main.html' %} + +{% block title %}Create contact | SloopStash CRM{% endblock %} + +{% block content %} +
+
+
+
+
+{% endblock %} diff --git a/view/contact/update.html b/view/contact/update.html index e69de29..ec41075 100644 --- a/view/contact/update.html +++ b/view/contact/update.html @@ -0,0 +1,64 @@ +{% extends 'main.html' %} + +{% block title %}Update contact | SloopStash CRM{% endblock %} + +{% block content %} +
+
+
+
+
+
+

Update contact for + {{ var.customer.name if var.customer else 'Unknown Customer' }}

+
+
+
+
+
+
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + + +
+ +
+
+ + +
+
+ + Cancel + +
+ +
+
+
+
+{% endblock %} diff --git a/view/customer/dashboard.html b/view/customer/dashboard.html new file mode 100644 index 0000000..75db1a5 --- /dev/null +++ b/view/customer/dashboard.html @@ -0,0 +1,108 @@ +{% extends 'main.html' %} + +{% block title %}Dashboard | SloopStash CRM{% endblock %} + +{% block content %} +
+
+ +
+
+
+
+
+

+ + + + Contacts for {{ var.customer.name }} +

+
+ +
+ +
+ + + + + + + + + + + + + {% for contact in var.contacts %} + + + + + + + + + {% else %} + + + + {% endfor %} + +
FirstNameLastNameEmailPhonenumberDescriptionActions
{{ contact.firstname }}{{ contact.lastname }}{{ contact.email }}{{ contact.phonenumber }}{{ contact.description }} +
+ + + +
+ + +
+
+
No contacts found for this customer.
+
+
+ +
+
+
+
+{% endblock %} + +{% block js %} + +{% endblock %}