emile 3 weeks ago
parent 076ec0b467
commit 4c5d3d4cca

@ -0,0 +1,18 @@
# Generated by Django 5.2 on 2025-04-14 12:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('addressbook', '0006_comment_prospectinglist_prospectinglistitem'),
]
operations = [
migrations.AddField(
model_name='prospectinglistitem',
name='comments',
field=models.ManyToManyField(blank=True, to='addressbook.comment'),
),
]

@ -29,11 +29,18 @@ class AddressBook(models.Model):
group = models.ManyToManyField(Group, blank=True)
class Comment(models.Model):
content = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
added_by = models.ForeignKey(User, on_delete=models.CASCADE)
class ProspectingListItem(models.Model):
prospecting_list = models.ForeignKey(ProspectingList, on_delete=models.CASCADE)
addressbook = models.ForeignKey(AddressBook, on_delete=models.CASCADE)
date_added = models.DateTimeField(auto_now_add=True)
attention = models.BooleanField(default=False)
comments = models.ManyToManyField(Comment, blank=True)
class Contact(models.Model):
@ -61,9 +68,3 @@ class SocialMedia(models.Model):
username = models.CharField(max_length=50, blank=True)
link = models.URLField(blank=True)
addressbook = models.ForeignKey(AddressBook, on_delete=models.CASCADE, null=True)
class Comment(models.Model):
content = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
added_by = models.ForeignKey(User, on_delete=models.CASCADE)

Binary file not shown.

@ -1,4 +1,4 @@
from addressbook.models import AddressBook, Country, Contact, Group, ProspectingList, ProspectingListItem
from addressbook.models import AddressBook, Country, Contact, Group, ProspectingList, ProspectingListItem, Comment
from osinacore.models import *
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponseRedirect
@ -696,7 +696,6 @@ def edit_addressbook(request, addressbook_id):
"countries": countries,
"groups": groups,
})
@staff_login_required
def edit_prospecting_list(request, list_id):
prospecting_list = get_object_or_404(ProspectingList, id=list_id)
@ -704,26 +703,27 @@ def edit_prospecting_list(request, list_id):
list_items = prospecting_list.prospectinglistitem_set.all()
if request.method == 'POST':
# Update basic list fields
prospecting_list.name = request.POST.get('name')
prospecting_list.description = request.POST.get('description')
prospecting_list.save()
# --- Update existing items ---
# --- Update existing items (update "attention" checkbox) ---
existing_ids = request.POST.getlist('list_item_id[]')
for id in existing_ids:
for item_id in existing_ids:
try:
item = ProspectingListItem.objects.get(id=id, prospecting_list=prospecting_list)
# Checkbox will only be present in POST if checked.
item.attention = True if request.POST.get(f'attention_existing_{id}') == 'on' else False
item = ProspectingListItem.objects.get(id=item_id, prospecting_list=prospecting_list)
# Checkbox is only present if checked.
item.attention = True if request.POST.get(f'attention_existing_{item_id}') == 'on' else False
item.save()
except ProspectingListItem.DoesNotExist:
pass
# --- Add new inline item if provided ---
new_addressbook = request.POST.get('new_addressbook')
# Only process if user selected a value (it may be empty if no new item is desired)
new_addressbook = request.POST.get('new_addressbook') # can be an existing contact id or the string "new"
if new_addressbook:
if new_addressbook == 'new':
# Create new AddressBook entry from inline form fields.
# Create a new AddressBook entry from inline fields.
new_first_name = request.POST.get('new_first_name')
new_last_name = request.POST.get('new_last_name')
if new_first_name and new_last_name:
@ -732,7 +732,7 @@ def edit_prospecting_list(request, list_id):
last_name=new_last_name
)
else:
addressbook = None # Or handle errors as needed.
addressbook = None # Optionally, handle error here.
else:
try:
addressbook = AddressBook.objects.get(id=new_addressbook)
@ -741,11 +741,22 @@ def edit_prospecting_list(request, list_id):
if addressbook:
new_attention = True if request.POST.get('new_attention') == 'on' else False
ProspectingListItem.objects.create(
new_item = ProspectingListItem.objects.create(
prospecting_list=prospecting_list,
addressbook=addressbook,
attention=new_attention
)
# Process new comments if provided.
new_comments = request.POST.get('new_comments', '')
# Split comments on newline and create individual Comment objects.
for line in new_comments.splitlines():
comment_text = line.strip()
if comment_text:
comment = Comment.objects.create(
content=comment_text,
added_by=request.user # assuming current user is adding the comment
)
new_item.comments.add(comment)
return redirect('editprospecting-list', list_id=prospecting_list.id)

@ -8,21 +8,21 @@
Edit Prospecting List
</h1>
<!-- Inline table for updating existing items and adding a new one -->
<!-- Main form: Updates list details, updates existing items, and adds one new item inline -->
<form method="POST" action="{% url 'editprospecting-list' list.id %}">
{% csrf_token %}
<div class="w-full flex flex-col gap-5 justify-center items-center mt-5">
<!-- List Details -->
<div class="w-full">
<label class="text-gray-500">Name:</label>
<input name="name" type="text" value="{{ list.name }}"
class="w-full h-[50px] py-1 px-3 border border-gray-300 rounded-md mt-1" required>
<input name="name" type="text" value="{{ list.name }}" class="w-full h-[50px] py-1 px-3 border border-gray-300 rounded-md mt-1" required>
</div>
<div class="w-full">
<label class="text-gray-500">Description:</label>
<input name="description" type="text" value="{{ list.description }}"
class="w-full h-[50px] py-1 px-3 border border-gray-300 rounded-md mt-1">
<input name="description" type="text" value="{{ list.description }}" class="w-full h-[50px] py-1 px-3 border border-gray-300 rounded-md mt-1">
</div>
<!-- Inline Table of Items -->
<div class="w-full">
<label class="text-gray-500">Items:</label>
<table class="min-w-full border">
@ -30,34 +30,44 @@
<tr>
<th class="border px-4 py-2">Contact</th>
<th class="border px-4 py-2">Attention</th>
<th class="border px-4 py-2">Comments</th>
</tr>
</thead>
<tbody>
<!-- Existing ProspectingListItems -->
<!-- Existing items -->
{% for item in list_items %}
<tr>
<td class="border px-4 py-2">
{{ item.addressbook.first_name }} {{ item.addressbook.last_name }}
<!-- Hidden field to identify existing items -->
<input type="hidden" name="list_item_id[]" value="{{ item.id }}">
</td>
<td class="border px-4 py-2 text-center">
<input type="checkbox" name="attention_existing_{{ item.id }}"
{% if item.attention %} checked {% endif %}>
<input type="checkbox" name="attention_existing_{{ item.id }}" {% if item.attention %} checked {% endif %}>
</td>
<td class="border px-4 py-2">
{% if item.comments.all %}
<ul class="list-disc list-inside text-sm">
{% for comment in item.comments.all %}
<li>{{ comment.content }}</li>
{% endfor %}
</ul>
{% else %}
<span class="text-gray-400 italic text-sm">No comments</span>
{% endif %}
</td>
</tr>
{% empty %}
<tr>
<td colspan="2" class="border px-4 py-2 text-center text-gray-400 italic">
<td colspan="3" class="border px-4 py-2 text-center text-gray-400 italic">
No contacts yet.
</td>
</tr>
{% endfor %}
<!-- Blank row for adding a new item inline -->
<!-- New item row -->
<tr class="bg-gray-50">
<td class="border px-4 py-2">
<label class="text-gray-500 text-sm">Add New Contact:</label>
<label class="text-gray-500 text-sm block">Add New Contact:</label>
<select name="new_addressbook" id="new-addressbook-select" class="w-full border rounded-md py-1 px-2 mt-1">
<option value="">Select a contact...</option>
{% for contact in addresses %}
@ -65,21 +75,25 @@
{% endfor %}
<option value="new"> Add new contact</option>
</select>
<!-- Fields for adding a new address if "new" is selected -->
<!-- Inline fields for new contact if "new" is selected -->
<div id="new-contact-fields" class="hidden mt-2">
<input type="text" name="new_first_name" placeholder="First Name" class="w-full border rounded-md py-1 px-2 mb-1">
<input type="text" name="new_last_name" placeholder="Last Name" class="w-full border rounded-md py-1 px-2">
</div>
</td>
<td class="border px-4 py-2 text-center">
<label class="text-gray-500 text-sm">Attention?</label>
<br>
<label class="text-gray-500 text-sm block">Attention?</label>
<input type="checkbox" name="new_attention">
</td>
<td class="border px-4 py-2">
<label class="text-gray-500 text-sm block">Comments:</label>
<textarea name="new_comments" placeholder="Enter one comment per line" class="w-full border rounded-md py-1 px-2 mt-1" rows="3"></textarea>
</td>
</tr>
</tbody>
</table>
</div>
<div class="flex justify-center mt-4">
<button type="submit" class="bg-osiblue text-white px-5 py-2 rounded hover:bg-white hover:text-osiblue border border-osiblue duration-300">
Save Changes
@ -94,7 +108,6 @@
// Toggle the new-contact fields when "Add new contact" is selected.
const newSelect = document.getElementById('new-addressbook-select');
const newContactFields = document.getElementById('new-contact-fields');
newSelect.addEventListener('change', function() {
if (this.value === 'new') {
newContactFields.classList.remove('hidden');

Loading…
Cancel
Save