Функция для перемещения событий в view.py
Код: Выделить всё
@csrf_exempt
def update_delivery_date(request):
if request.method == 'POST':
event_id = request.POST.get('id')
new_start_date = request.POST.get('new_start_date')
year = request.POST.get('year')
# Check if the user has the allowed groups
if not (request.user.groups.filter(name__in=['Expedicao', 'Dir Logistica', 'Logistica']).exists() or request.user.is_superuser):
return JsonResponse({'success': False, 'error': 'User does not have permission to move events'}, status=403)
try:
with transaction.atomic():
# Find the specific event by DocNum and Year
event = EncomendasCalendar.objects.select_for_update().get(NumDoc=event_id, Ano=year)
# Check if there is already another event with the same DocNum and Year
if EncomendasCalendar.objects.filter(NumDoc=event_id, Ano=year).exclude(NumDoc=event_id).exists():
print("Duplicate")
return JsonResponse({'success': False, 'error': 'An event with the same DocNum and Year already exists'}, status=400)
# Check if the new date is the same as the current one
if event.DataEntrega == new_start_date:
print("same")
return JsonResponse({'success': False, 'error': 'The delivery date is already the same as the informed date.'}, status=400)
# Update the DeliveryDate with the new date
event.DataEntrega = new_start_date
event.UpdatedBy = request.user
# Save the updated event in the database
event.save()
except EncomendasCalendar.DoesNotExist:
return JsonResponse({'success': False, 'error': 'Event not found'}, status=404)
except Exception as e:
print('Error updating the delivery date:', e)
return JsonResponse({'success': False, 'error': 'Error updating the delivery date'})
return JsonResponse({'success': True})
else:
return JsonResponse({'success': False, 'error': 'Method not allowed'}, status=405)
Код: Выделить всё
function moveEvents() {
// Logic to move all events with status_id equal to 2
var startDate = $('#start_date').val();
var endDate = $('#end_date').val();
var csrftoken = Cookies.get('csrftoken');
$.ajax({
type: 'POST',
url: '/move_events/', // Replace with the correct URL
data: {
start_date: startDate,
end_date: endDate
},
beforeSend: function (xhr) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
},
success: function (response) {
$('#MoveModal').find('input[type="date"]').val('');
// Close the modal after the move and reload the events
$('#MoveModal').modal('hide');
calendar.refetchEvents();
},
error: function (xhr, status, error) {
console.error('Error moving events:', error);
}
});
}
Я пробовал много вещей, которые уже не помню, но последняя попытка была проверкой в кодеviews.py, который должен был не добавлять событие, если номер numdoc + год уже существовал, но это не сработало.
Подробнее здесь: https://stackoverflow.com/questions/791 ... ed-to-anot