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 from django.urls import reverse from django.http import HttpResponse from osinacore.decorators import * from customercore.models import * from support.models import * @staff_login_required def edit_customer(request, customer_id): customer = get_object_or_404(CustomerProfile, customer_id=customer_id) #Utilities references = Reference.objects.all() if request.method == 'POST': customer.user.first_name = request.POST.get('first_name') customer.user.last_name = request.POST.get('last_name') customer.user.email = request.POST.get('email') customer.user.username = request.POST.get('email') customer.user.save() customer.mobile_number = request.POST.get('mobile_number') customer.personal_website = request.POST.get('personal_website') customer.status = request.POST.get('status') customer_reference = request.POST.get('reference') reference = get_object_or_404(Reference, id=customer_reference) customer.reference = reference customer.save() return redirect('customerdetails', customer_id=customer.customer_id) context = { 'customer' : customer, 'references' : references, } return render(request, 'edit_templates/edit-customer.html', context) @staff_login_required def edit_customer_status_modal(request, customer_id): customer = get_object_or_404(CustomerProfile, id=customer_id) if request.method == 'POST': customer.status = request.POST.get('status') customer.save() return HttpResponse('') context = { 'customer': customer, } return render(request, 'edit_templates/edit-customer-status-modal.html', context) @staff_login_required def edit_business(request, business_id): business = get_object_or_404(Business, business_id=business_id) business_types = BusinessType.objects.all().order_by('name') customers = CustomerProfile.objects.all().order_by('-id') if request.method == 'POST': business.name= request.POST.get('name') business.financial_number = request.POST.get('financial_number') business.commercial_registration = request.POST.get('commercial_registration') if request.POST.get('vat'): business.vat = True else: business.vat = False business.phone_number = request.POST.get('phone_number') business.website = request.POST.get('website') if request.POST.get('business_type'): business_type = request.POST.get('business_type') type = get_object_or_404(BusinessType, id=business_type) business.type = type else: business.type = None new_logo = request.FILES.get('logo') if new_logo: business.logo = new_logo business.save() return redirect('businessdetails', business_id=business.business_id) context = { 'business' : business, 'business_types' : business_types, 'customers' : customers, } return render(request, 'edit_templates/edit-business.html', context) @staff_login_required def edit_staff(request, staff_id): staff = get_object_or_404(StaffProfile, staff_id=staff_id) if request.method == 'POST': staff.user.first_name = request.POST.get('first_name') staff.user.last_name = request.POST.get('last_name') staff.user.email = request.POST.get('email') active = True if request.POST.get('active') else False staff.user.is_active = active staff.user.save() staff.mobile_number = request.POST.get('mobile_number') new_image = request.FILES.get('image') if new_image: staff.image = new_image intern = True if request.POST.get('intern') else False staff.intern = intern staff.save() return redirect('userdetails', staff_id=staff.staff_id) context = { 'staff': staff, } return render(request, 'edit_templates/edit-staff.html', context) @staff_login_required def edit_project(request, project_id): project = get_object_or_404(Project, project_id=project_id) staffs = StaffProfile.objects.all().order_by('-id') current_manager = project.manager customers = CustomerProfile.objects.all().order_by('-id') current_client = project.customer types = ProjectType.objects.all().order_by('-id') if request.method == 'POST': project.name = request.POST.get('name') new_customer_id = request.POST.get('customer') customer = get_object_or_404(CustomerProfile, id=new_customer_id) project.customer = customer new_manager_id = request.POST.get('manager') manager = get_object_or_404(StaffProfile, id=new_manager_id) project.manager = manager members_ids = request.POST.getlist('members') members_profiles = StaffProfile.objects.filter(id__in=members_ids) project.members.set(members_profiles) type_ids = request.POST.getlist('types') types = ProjectType.objects.filter(id__in=type_ids) project.project_type.set(type_ids) project.details = request.POST.get('details') project.start_date = request.POST.get('start_date') project.end_date = request.POST.get('end_date') project.save() return redirect('detailed-project', project_id=project.project_id) context = { 'project' : project, 'staffs' : staffs, 'current_manager' : current_manager, 'customers' : customers, 'current_client' : current_client, 'types' : types, } return render(request, 'edit_templates/edit-project.html', context) @staff_login_required def edit_project_status_modal(request, project_id): project = get_object_or_404(Project, id=project_id) if request.method == 'POST': project_status = ProjectStatus( project = project, status = request.POST.get('status'), date = request.POST.get('date'), ) project_status.save() return redirect('detailed-project', project_id=project.project_id) context = { 'project': project, } return render(request, 'edit_templates/edit-project-status-modal.html', context) @staff_login_required def toggle_pin_project(request, project_id): project = get_object_or_404(Project, id=project_id) if PinnedProject.objects.filter(user=request.user, project=project): PinnedProject.objects.filter(user=request.user, project=project).delete() else: PinnedProject.objects.create(user=request.user, project=project) return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) @staff_login_required def edit_user_story_modal(request, story_id): story = get_object_or_404(UserStory, id=story_id) milestones = Milestone.objects.filter(project=story.project) if request.method == 'POST': story.milestone = get_object_or_404(Milestone, id=request.POST.get('milestone')) story.content = request.POST.get('content') story.completed = True if request.POST.get('completed') else False story.confirmed = True if request.POST.get('confirmed') else False story.save() response = HttpResponse( '') return response context = { 'milestones': milestones, 'story': story, } return render(request, 'edit_templates/edit-userstory-modal.html', context) @staff_login_required def edit_task(request, task_id): task = get_object_or_404(Task, task_id=task_id) projects = Project.objects.all().order_by('-id') staffs = StaffProfile.objects.all().order_by('-id') selected_project_id = request.POST.get('project', task.project.id) if request.method == 'POST' else task.project.id epics_of_my_project = Epic.objects.filter(project_id=selected_project_id) if request.method == 'POST': task.name = request.POST.get('name') project_id = request.POST.get('project') project = get_object_or_404(Project, id=project_id) task.project = project task.extra = request.POST.get('extra') epic_id = request.POST.get('epic') epic = get_object_or_404(Epic, id=epic_id) task.epic = epic task.userstory = request.POST.get('requirement') task.status = request.POST.get('status') # Convert assigned_to ID to a StaffProfile instance assigned_to_id = request.POST.get('assigned_to') assigned_to = get_object_or_404(StaffProfile, id=assigned_to_id) task.assigned_to = assigned_to task.description = request.POST.get('description') task.start_date = request.POST.get('start_date') task.end_date = request.POST.get('end_date') task.save() return redirect('detailed-task', task_id=task.task_id) context = { 'task': task, 'projects': projects, 'epics_of_my_project': epics_of_my_project, 'staffs': staffs, } return render(request, 'edit_templates/edit-task.html', context) @staff_login_required def edit_task_status_modal(request, *, task_id): task = get_object_or_404(Task, task_id=task_id) if request.method == 'POST': status = request.POST.get('status') task.status = status task.save() # Reload the parent page using JavaScript response = HttpResponse('') return response context = { 'task' : task, } return render(request, 'edit_templates/edit-taskstatus-modal.html', context) @staff_login_required def edit_epic(request, *args, **kwargs): context = { } return render(request, 'edit_templates/edit-epic.html', context) @staff_login_required def edit_department(request, department_id): department = get_object_or_404(Department, id=department_id) if request.method == 'POST': department.name = request.POST.get('name') department.save() return redirect('departments') return render(request, 'edit_templates/edit-department.html', {'department': department}) @staff_login_required def edit_project_type(request, projecttype_id): projecttype = get_object_or_404(ProjectType, id=projecttype_id) departments = Department.objects.all().order_by('name') if request.method == 'POST': projecttype.name = request.POST.get('name') projecttype.department = get_object_or_404(Department, id=request.POST.get('department')) projecttype.save() return redirect('projecttypes') return render(request, 'edit_templates/edit-project-type.html', {'projecttype': projecttype,'departments': departments}) @staff_login_required def edit_job_position(request, jobposition_id): jobposition = get_object_or_404(JobPosition, id=jobposition_id) departments = Department.objects.all().order_by('name') if request.method == 'POST': jobposition.name = request.POST.get('name') jobposition.department = get_object_or_404(Department, id=request.POST.get('department')) jobposition.save() return redirect('jobpositions') return render(request, 'edit_templates/edit-job-position.html', {'jobposition': jobposition,'departments': departments}) @staff_login_required def edit_business_type(request, businesstype_id): businesstype = get_object_or_404(BusinessType, id=businesstype_id) if request.method == 'POST': businesstype.name = request.POST.get('name') businesstype.save() return redirect('businesstypes') return render(request, 'edit_templates/edit-business-type.html', {'businesstype': businesstype}) @staff_login_required def edit_reference(request, reference_id): reference = get_object_or_404(Reference, id=reference_id) if request.method == 'POST': reference.name = request.POST.get('name') reference.date = request.POST.get('date') reference.save() return redirect('references') return render(request, 'edit_templates/edit-reference.html', {'reference': reference}) @staff_login_required def edit_tag(request, tag_id): tag = get_object_or_404(Tag, id=tag_id) if request.method == 'POST': tag.name = request.POST.get('name') tag.save() return redirect('tags') return render(request, 'edit_templates/edit-tag.html', {'tag': tag}) @staff_login_required def edit_ticket_status_modal(request, ticket_id): ticket = get_object_or_404(Ticket, id=ticket_id) if request.method == 'POST': ticket_status = TicketStatus( ticket = ticket, status = request.POST.get('status'), date_added = request.POST.get('date'), added_by = request.user ) ticket_status.save() return redirect('ticketroom', ticket_number=ticket.ticket_number) context = { 'ticket': ticket, } return render(request, 'edit_templates/edit-ticket-status-modal.html', context) #Mark points @staff_login_required def mark_point_working_on(request, point_id, task_id): task = get_object_or_404(Task, id=task_id) point = get_object_or_404(Point, id=point_id) point.status = 'Working On' point.save() activity = PointActivity( point = point, start_time = timezone.now(), ) activity.save() if task.status != 'Working On': task.status = 'Working On' task.status_date = timezone.now() task.save() if PointActivity.objects.filter(point=point).count() == 1: status_text = f'Started Working On: {point.text}' status = Status(text=status_text, date_time=timezone.now(), staff=request.user.staffprofile, type='Task', type_id=point.task.id) status.save() else: status_text = f'Resumed Working On: {point.text}' status = Status(text=status_text, date_time=timezone.now(), staff=request.user.staffprofile, type='Task', type_id=point.task.id) status.save() task_id_str = task.task_id showpoints_url = reverse('showpoints', args=[task_id_str]) return HttpResponseRedirect(showpoints_url) @staff_login_required def mark_point_working_on_task_page(request, point_id, task_id): task = get_object_or_404(Task, id=task_id) point = get_object_or_404(Point, id=point_id) point.status = 'Working On' point.save() activity = PointActivity( point = point, start_time = timezone.now(), ) activity.save() if task.status != 'Working On': task.status = 'Working On' task.status_date = timezone.now() task.save() if PointActivity.objects.filter(point=point).count() == 1: status_text = f'Started Working On: {point.text}' status = Status(text=status_text, date_time=timezone.now(), staff=request.user.staffprofile, type='Task', type_id=point.task.id) status.save() else: status_text = f'Resumed Working On: {point.text}' status = Status(text=status_text, date_time=timezone.now(), staff=request.user.staffprofile, type='Task', type_id=point.task.id) status.save() return redirect('detailed-task', task_id=task.task_id) @staff_login_required def mark_point_paused(request, point_id, task_id): task = get_object_or_404(Task, id=task_id) point = get_object_or_404(Point, id=point_id) point.status = 'Paused' current_datetime = timezone.now() point.save() last_activity = PointActivity.objects.filter(point=point).last() if last_activity: last_activity.end_time = current_datetime last_activity.save() if not Point.objects.filter(task=task, status='Working On').exists(): task.status = 'Open' task.status_date = timezone.now() task.save() status_text = f'{point.text} - Paused' status = Status(text=status_text, date_time=timezone.now(), staff=request.user.staffprofile, type='Task', type_id=point.task.id) status.save() task_id_str = task.task_id showpoints_url = reverse('showpoints', args=[task_id_str]) return HttpResponseRedirect(showpoints_url) @staff_login_required def mark_point_paused_task_page(request, point_id, task_id): task = get_object_or_404(Task, id=task_id) point = get_object_or_404(Point, id=point_id) point.status = 'Paused' current_datetime = timezone.now() point.save() last_activity = PointActivity.objects.filter(point=point).last() if last_activity: last_activity.end_time = current_datetime last_activity.save() if not Point.objects.filter(task=task, status='Working On').exists(): task.status = 'Open' task.status_date = timezone.now() task.save() status_text = f'{point.text} - Paused' status = Status(text=status_text, date_time=timezone.now(), staff=request.user.staffprofile, type='Task', type_id=point.task.id) status.save() return redirect('detailed-task', task_id=task.task_id) @staff_login_required def mark_point_completed(request, point_id, task_id): task = get_object_or_404(Task, id=task_id) point = get_object_or_404(Point, id=point_id) point.status = 'Completed' current_datetime = timezone.now() point.save() # Update the end time of the last activity to the current time last_activity = PointActivity.objects.filter(point=point).last() if last_activity: last_activity.end_time = current_datetime last_activity.save() if not Point.objects.filter(task=task, status='Working On').exists(): task.status = 'Open' task.status_date = timezone.now() task.save() total_time_hours, total_time_minutes, total_time_seconds = point.total_activity_time() formatted_time = "" if total_time_hours > 0: formatted_time += f"{total_time_hours}{'hr' if total_time_hours == 1 else 'hrs'}" if total_time_minutes > 0: formatted_time += f" {total_time_minutes}{'min' if total_time_minutes == 1 else 'mins'}" if total_time_seconds > 0: formatted_time += f" {total_time_seconds}{'sec' if total_time_seconds == 1 else 'secs'}" status_text = f'{point.text} - Completed' if formatted_time: status_text += f' in {formatted_time}' status = Status(text=status_text, date_time=timezone.now(), staff=request.user.staffprofile, type='Task', type_id=point.task.id) status.save() task_id_str = task.task_id showpoints_url = reverse('showpoints', args=[task_id_str]) return HttpResponseRedirect(showpoints_url) @staff_login_required def mark_point_completed_task_page(request, point_id, task_id): task = get_object_or_404(Task, id=task_id) point = get_object_or_404(Point, id=point_id) point.status = 'Completed' current_datetime = timezone.now() point.save() # Update the end time of the last activity to the current time last_activity = PointActivity.objects.filter(point=point).last() if last_activity: last_activity.end_time = current_datetime last_activity.save() if not Point.objects.filter(task=task, status='Working On').exists(): task.status = 'Open' task.status_date = timezone.now() task.save() total_time_hours, total_time_minutes, total_time_seconds = point.total_activity_time() formatted_time = "" if total_time_hours > 0: formatted_time += f"{total_time_hours}{'hr' if total_time_hours == 1 else 'hrs'}" if total_time_minutes > 0: formatted_time += f" {total_time_minutes}{'min' if total_time_minutes == 1 else 'mins'}" if total_time_seconds > 0: formatted_time += f" {total_time_seconds}{'sec' if total_time_seconds == 1 else 'secs'}" status_text = f'{point.text} - Completed' if formatted_time: status_text += f' in {formatted_time}' status = Status(text=status_text, date_time=timezone.now(), staff=request.user.staffprofile, type='Task', type_id=point.task.id) status.save() return redirect('detailed-task', task_id=task.task_id) @staff_login_required def edit_addressbook(request, addressbook_id): address = get_object_or_404(AddressBook, id=addressbook_id) countries = Country.objects.all() groups = Group.objects.all() if request.method == 'POST': address.first_name = request.POST.get('first_name') address.middle_name = request.POST.get('middle_name') address.last_name = request.POST.get('last_name') address.country_id = request.POST.get('country') address.save() selected_groups = request.POST.getlist('groups') selected_groups_ids = [int(gid) for gid in selected_groups if gid.isdigit()] address.group.set(selected_groups_ids) # Delete old contacts address.contact_set.all().delete() contact_types = request.POST.getlist('contact_type[]') contact_values = request.POST.getlist('contact_value[]') for type_, value in zip(contact_types, contact_values): if value: Contact.objects.create(type=type_, contact=value, addressbook=address) return redirect('addressbook') return render(request, 'edit_templates/edit-addressbook.html', { "address": address, "countries": countries, "groups": groups, }) @staff_login_required def edit_prospecting_list(request, list_id): prospecting_list = get_object_or_404(ProspectingList, id=list_id) addresses = AddressBook.objects.all().order_by('first_name') 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 "attention" checkbox) --- existing_ids = request.POST.getlist('list_item_id[]') for item_id in existing_ids: try: 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') # can be an existing contact id or the string "new" if new_addressbook: if new_addressbook == 'new': # 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: addressbook = AddressBook.objects.create( first_name=new_first_name, last_name=new_last_name ) else: addressbook = None # Optionally, handle error here. else: try: addressbook = AddressBook.objects.get(id=new_addressbook) except AddressBook.DoesNotExist: addressbook = None if addressbook: new_attention = True if request.POST.get('new_attention') == 'on' else False 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) context = { "list": prospecting_list, "addresses": addresses, "list_items": list_items, } return render(request, 'edit_templates/edit-prospecting-list.html', context)