views:

42

answers:

2

I need to detect other events in the same day. What I hope to be able to do, is find if an event of eventClass X exists in the same day as a dropped event of eventClass Y. If not it would warn the user, that an eventClass X does not exist, else allow the user to drop the event.

Is this possible?

A: 

Well after probing around in the docs and a little experimenting, I arrived at a solution using the clientEvents method:

eventDrop: function( event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view ) { // see if its a concept class event if (event.className == 'conceptclass'){ // create a new date object from the start of the event var eventDate = new Date(event.start); // zero its time eventDate.setHours(0); eventDate.setMinutes(0); eventDate.setSeconds(0); eventDate.setMilliseconds(0);

                // now find all the events currently displayed in the calendar
                var pulledEvents = $('#calendar').fullCalendar( 'clientEvents');
                var meetingDay = false; // until a meeting is found 

                for(var i = 0; i < pulledEvents.length; i++){
                    // if the pulled event is of the meeting class 
                    if(pulledEvents[i].className == 'meetingclass'){
                        // create a new date object from the start of the pulled event 
                        var testEventDate = new Date(pulledEvents[i].start);
                         // zero its time for comparison 
                        testEventDate.setHours(0);
                        testEventDate.setMinutes(0);
                        testEventDate.setSeconds(0);
                        testEventDate.setMilliseconds(0);

                        // if meeting event found in the day
                        // OK a little wierdness here, even though the dates were equal 
                        // they would not return a valid comparison.  So I get the time value.
                        if(testEventDate.getTime() == eventDate.getTime()){
                           meetingDay = true;
                           break;
                        }
                    }
            }
            if(!meetingDay){
                 alert("Tried to drop a Concept into Day without Scheduled Review Meeting!");
                 revertFunc(); // back to where it came!
            }       
      }
Tom
A: 

Another option is to solve this server side since you should be checking it's valid there too.

When the "concept" event is dragged onto the day, send the Ajax request to the server to update the "concept" event's date, if it's not valid, call revertFunc() to put the event back.

eventDrop: function(event, dayDelta, minuteDelta, allDay, 
                    revertFunc, jsEvent, ui, view) {
  $.post("/concep_update/", { id: event.id, delta: dayDelta },
         function(data) {
           if(data!="true") {
             revertFunc();
           }
         }, "text"});
}

On the server, check that there is a "meeting" event on the same day, and if so, update the "concept" event and return "true", otherwise, return "false".

@require_POST @login_required
def concept_update(request):
    concept = get_object_or_404(Concept.objects.all(), 
                              id=int(request.POST.get("id","0")))
    concept.date = concept.date + timedelta(days=int(request.POST.get("delta","0")))
    meetings = Meeting.objects.filter(date=concept.date)
    if meetings.count() > 0:
        concept.save()
        return HttpResponse("true", status=200, content_type="text/plain")
    else:
        return HttpResponse("false", status=200, content_type="text/plain")
Tom