-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement Search App, seeding updates bug fixes and new features.
- Loading branch information
Showing
17 changed files
with
247 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from django.contrib import admin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class SearchConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'apps.search' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from django import forms | ||
|
||
class SearchForm(forms.Form): | ||
q = forms.CharField(max_length=255, required=False) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path('', views.search, name='search'), | ||
path('search/', views.search_results, name='search_results'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from django.http import JsonResponse | ||
from django.db.models import Q | ||
from django.apps import apps | ||
from django.shortcuts import render, get_object_or_404 | ||
from apps.contact.models import Contact | ||
from apps.business_partner.models import BusinessPartner | ||
from apps.tasks.models import Task | ||
from crm.models import Lead | ||
from crm.models import Opportunity | ||
from .forms import SearchForm | ||
|
||
def search(request): | ||
form = SearchForm(request.GET) | ||
if form.is_valid(): | ||
query = form.cleaned_data['q'] | ||
contacts = Contact.objects.filter(Q(first_name__icontains=query) | Q(last_name__icontains=query)) | ||
business_partners = BusinessPartner.objects.filter(Q(name__icontains=query)) | ||
tasks = Task.objects.filter(Q(title__icontains=query)) | ||
leads = Lead.objects.filter(Q(name__icontains=query)) | ||
opportunities = Opportunity.objects.filter(Q(name__icontains=query)) | ||
|
||
return render(request, 'apps/search/search_results.html', { | ||
'contacts': contacts, | ||
'business_partners': business_partners, | ||
'tasks': tasks, | ||
'leads': leads, | ||
'opportunities': opportunities, | ||
'form': form | ||
}) | ||
else: | ||
return render(request, 'apps/search/search_results.html', {'form': form}) | ||
|
||
import logging | ||
|
||
def autocomplete(request): | ||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
query = request.GET.get('q', '') | ||
models = request.GET.getlist('models', []) | ||
logger.info(f"Autocomplete request: q={query}, models={models}") | ||
results = [] | ||
|
||
for model in models: | ||
model_class = apps.get_model(model) | ||
model_data = list(model_class.objects.filter(Q(name__icontains=query)).values('id', 'name')) | ||
results.extend(model_data) | ||
|
||
logger.info(f"Autocomplete results: {results}") | ||
return JsonResponse({'results': results}) | ||
|
||
def search_results(request): | ||
query = request.GET.get('q', '') | ||
model_name = request.GET.get('model', '') | ||
if model_name: | ||
model_class = apps.get_model(model_name) | ||
model_data = list(model_class.objects.filter(Q(name__icontains=query)).values('id', 'name')) | ||
return render(request, 'search_results.html', {model_name.lower(): model_data}) | ||
else: | ||
return render(request, 'apps/search/search_results.html', {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,3 +128,4 @@ | |
|
||
})); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
{% extends 'base.html' %} | ||
{% load i18n %} | ||
|
||
{% block content %} | ||
<div class="page-header"> | ||
<div class="container-xl"> | ||
<div class="row g-2 align-items-center"> | ||
<div class="col"> | ||
<h1>{% trans "Search Results" %}</h1> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="page-body"> | ||
<div class="container-xl"> | ||
{% if contacts or business_partners or tasks or leads or opportunities %} | ||
{% if contacts %} | ||
<div class="card"> | ||
<div class="card-header"> | ||
<h3 class="card-title">{% trans "Contacts" %}</h3> | ||
</div> | ||
<div class="card-body"> | ||
<div class="row row-cards"> | ||
{% for contact in contacts %} | ||
<div class="col-md-4 col-lg-3"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
<a href="{% url 'contact:contact_detail' contact.id %}">{{ contact.first_name }} {{ contact.last_name }}</a> | ||
</div> | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</div> | ||
</div> | ||
{% endif %} | ||
|
||
{% if business_partners %} | ||
<div class="card"> | ||
<div class="card-header"> | ||
<h3 class="card-title">{% trans "Business Partners" %}</h3> | ||
</div> | ||
<div class="card-body"> | ||
<div class="row row-cards"> | ||
{% for partner in business_partners %} | ||
<div class="col-md-4 col-lg-3"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
<a href="{% url 'business_partner:businesspartner_detail' partner.id %}">{{ partner.name }}</a> | ||
</div> | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</div> | ||
</div> | ||
{% endif %} | ||
|
||
{% if tasks %} | ||
<div class="card"> | ||
<div class="card-header"> | ||
<h3 class="card-title">{% trans "Tasks" %}</h3> | ||
</div> | ||
<div class="card-body"> | ||
<div class="row row-cards"> | ||
{% for task in tasks %} | ||
<div class="col-md-4 col-lg-3"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
<a href="{% url 'tasks:task_detail' task.id %}">{{ task.title }}</a> | ||
</div> | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</div> | ||
</div> | ||
{% endif %} | ||
|
||
{% if leads %} | ||
<div class="card"> | ||
<div class="card-header"> | ||
<h3 class="card-title">{% trans "Leads" %}</h3> | ||
</div> | ||
<div class="card-body"> | ||
<div class="row row-cards"> | ||
{% for lead in leads %} | ||
<div class="col-md-4 col-lg-3"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
<a href="{% url 'crm:lead_detail' lead.id %}">{{ lead.name }}</a> | ||
</div> | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</div> | ||
</div> | ||
{% endif %} | ||
|
||
{% if opportunities %} | ||
<div class="card"> | ||
<div class="card-header"> | ||
<h3 class="card-title">{% trans "Opportunities" %}</h3> | ||
</div> | ||
<div class="card-body"> | ||
<div class="row row-cards"> | ||
{% for opportunity in opportunities %} | ||
<div class="col-md-4 col-lg-3"> | ||
<div class="card"> | ||
<div class="card-body"> | ||
<a href="{% url 'crm:opportunity_detail' opportunity.id %}">{{ opportunity.name }}</a> | ||
</div> | ||
</div> | ||
</div> | ||
{% endfor %} | ||
</div> | ||
</div> | ||
</div> | ||
{% endif %} | ||
{% else %} | ||
<div class="row"> | ||
<div class="col-md-6"> | ||
<div class="alert alert-warning"> | ||
{% trans "No results found." %} | ||
</div> | ||
</div> | ||
</div> | ||
{% endif %} | ||
</div> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters