Функция для перемещения событий в файлеviews.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)
Код: Выделить всё
eventDrop: function (info) {
// Get the ID of the moved event
var eventId = info.event.id;
var eventYear = info.event.extendedProps.year;
// Get the new start date of the moved event
var newStartDate = info.event.start.toISOString().replace('T', ' ');
newStartDate = newStartDate.replace('Z', '');
// Update the delivery date on the server using AJAX
var csrftoken = Cookies.get('csrftoken');
$.ajax({
type: 'POST',
url: 'update_delivery_date/',
data: {
id: eventId,
new_start_date: newStartDate,
year: eventYear
},
beforeSend: function(xhr) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
},
success: function(response) {
// Successful update
},
error: function(xhr, status, error) {
if (xhr.status === 403) {
// Display a custom error message if the user does not have permission
alert('Error: You do not have permission to move this event.');
info.revert();
} else {
// In case of an error
console.error('Error updating the delivery date:', error);
}
}
});
}
обычное обновление
событие дублирования
событие дублирования
p>
Я пробовал много вещей, которые уже не помню, но последней попыткой была проверка в кодеviews.py, который должен был не добавлять событие, если numdoc + год уже существовал, но это не сработало.
Подробнее здесь: https://stackoverflow.com/questions/791 ... ed-to-anot