1

I have the following filter function on JavaScript and I need to do the same on Django.

This is my JavaScript function:

absentAttendees() {     
      return this.meeting.attendees.filter((element) => this.timeAttendees(element.user.id) <= 0 || (element.absent && this.timeAttendees(element.user.id) <= 0));
    }

timeAttendees(user) {
      let timeUser = 0
      this.meeting.meeting_times.forEach((time) => {
        if (time.user.id === user) {
          timeUser = timeUser + time.time
        }
      })

      return timeUser
    }

Here is what I have so far on Django, but it tells me that the object I'm passing doesn't have the attribute 'user'

timeList = filter(timeAttendees, meeting.attendees.all())
def timeAttendees(item):
            timeUser = 0
            
            for time in time_list:
                if(time.user.id == item.user.id):
                    timeUser = timeUser + time.time
                
            return timeUser

meeting.attendees refers to the following model:

meeting = models.ForeignKey(
Meeting, related_name="attendees", on_delete=models.CASCADE
)
user = models.ForeignKey(
    User, related_name="attendee", on_delete=models.CASCADE, null=True, blank=True
)
idd = models.CharField(max_length=255)
organizer = models.BooleanField(default=False)
absent = models.BooleanField(default=True)
verified = models.BooleanField(default=False)
5
  • you forgot to save the value of meeting.attendees.all().filter(timeAttendees > 0) in time_list i think . if not what is time_list Commented Sep 16, 2021 at 17:14
  • Right forgot about that part, although my main problem is that item on the function is not carrying the properties from the model Commented Sep 16, 2021 at 17:17
  • What is item ? an user or a structure of user or another model instance? Commented Sep 16, 2021 at 17:21
  • item would be the attendees model, which has properties and a user model inside Commented Sep 16, 2021 at 17:23
  • 1
    Can you edit and include the definition of every variable that is being used? Commented Sep 16, 2021 at 17:24

1 Answer 1

1

Assuming meeting is some Meeting object, and item is valid object here is how you can replicate timeAttendees function:

meeting = <Meeting_object>
def get_time_attendees(item):
    global meeting
    return sum([interval.time for interval in meeting.meeting_times
                if interval.user.id == item.user.id])

Above function returns total time that user appeared in meeting_times. To get absent attendees, filter() and map() could do the job:

def get_absent_attendees(meeting):
    attendees = meeting.attendees.all()
    return list(filter(lambda x: x <= 0, list(map(get_time_attendees, attendees))))

list(map(get_time_attendees, attendees)) calls get_time_attendees function in every attendees object and converts the result into a list. Then with filter, you grab the ones with attendance time less than zero.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, does exactly what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.