list? queryet? joining together

38 views
Skip to first unread message

MikeKJ

unread,
Jan 9, 2017, 11:16:08 AM1/9/17
to Django users
Got a quantity of a type of tool to have hours booked, sometimes a tool is booked for all available hours and sometimes for only a few hours and sometimes more than 1 tool is booked to the job.  The problem I am having is adding a booking into usable hours, I think I need to determine if the new booking will slip into an available existing booking, so...

[code]
list = Booking.objects.all().filter(resource=2).filter(date=now).order_by("-resource_quantity_booked")  //resource is the type of tool
// break the list into the individual tools ( of which there are 5 available)
for x in list:
    plist[x] = {'id': x.id, 'list': list}
    plist = list(plist)
    try:
        pa = plist[0]
     except:
        pa = ""
    try:
        pb = plist[1]
    except:
        pb = ""
    try:
        pc = plist[2]
    except:
        pc = ""
    try:
        pd = plist[3]
    except:
        pd = ""
    try:
        pe = plist[4]
    except:
        pe = ""
//ok got all indivdual tools
// lets assume that tool pb and pc are fully utilised for all available hours and that tool pa is booked for 4 hours from 10am (10:00 to 14:00)
// therefore there are 2 potential available hours before availble and four hours after the booking for tool pa
//lets assume that a booking is made for 16:00 (pd as being the 4th booking object)
// so I want to fold the booking for 16:00 into the previous booking for 10:00 to ustilise the available hours
//so I tried this
    if pd:
        if int(pd.start) < int(pa.start) and (pd.hours < (int(pa.start)-8)): //looking for prior availability
            pass  //wip
        if int(pd.start) > (pa.hours + int(pa.start)) and (pd.hours <= (18-pd.hours+int(pd.start))):  // this shows that there is availability
              from itertools import chain
                    #pa = list(chain(pa, pd))
                    #pa.extend(pd)
                    from django.db.models import Q
                    yy = Q(pa) | Q(pd)  //result  (OR: Booking object, Booking object)                   
                    papd = chain(pa, pd) //result <itertools.chain object at 0x7f5d7d37a9d0>                   
                    // need to empty pd as it is now part of pa?
[/code]
yy appears to be the result I want ( 2 objects (or more)) joined
How do I get at the individual objects within yy in a template?
Is there a better way to do this?





MikeKJ

unread,
Jan 10, 2017, 10:05:33 AM1/10/17
to Django users
May be another way of doing this?

5 tools @ 9 hours = 45 hours maximum possible utilisation
1 tool = 9 hours maximum possible utilisation

each tool is a list
pa = [] (tool1)
pb = [] (tool2)  etc

max = 9
need to know the count of objects
loop all the booking objects
use the max count as the index
test if all booked hours exceeds max if not then list pa is complete
else
add together object hours to not exceed max and make each list pa, pb etc
ending up with
pa = [(object, object, object)]
pb = [(object, object, object, object)]   etc

then unpack the tuples in the template with if statements to position as to time and duration

Would that work better?
What code would achieve it?
Appreciate any help here

       

   

Andrew Beales

unread,
Jan 12, 2017, 2:52:28 PM1/12/17
to Django users
Hi, just a couple of follow-up questions as having trouble following precisely.

Can you re-state the exact queries you’re looking for?

- Are you looking to check if there are tools of a given type available at a given time?
- And whether a tool of a given type can be booked in a given time window?
- When you said you wanted to “fold” 2 bookings into the same time window - intuitively at first glance a booking app would want to do the opposite, ie. find empty time windows - could you elaborate on these booking use cases? Is this for a single customer who wants to book multiple tools at a time?
- Sharing the Tool and Booking models would be useful.

This may be too basic, but for time queries if you’re not already aware there are the keyword lookups gt, gte (greater than, greater than or equal to) and lt, lte, for example:

now = timezone.now()
active_bookings_for_tool_type = Booking.objects.filter(tool__resource=2, start_time__lte=now, finish_time__gte=now)

and timedelta from the datetime module is useful for handling time intervals.

Also there are some open source django booking apps out there which look pretty good, for example django-booking: https://github.com/bitlabstudio/django-booking - could be worth looking into?

Mike Jones

unread,
Jan 13, 2017, 6:39:45 AM1/13/17
to django...@googlegroups.com

Thanks for response I’ll try to answer as below in red

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Andrew Beales
Sent: 12 January 2017 19:52
To: Django users
Subject: Re: list? queryet? joining together

 

Hi, just a couple of follow-up questions as having trouble following precisely.

Can you re-state the exact queries you’re looking for?

Your comment about ‘folding’ model objects makes sense so,

I thought some more about it and am thinking about creating a dict of all bookings for a tool for the day Booking.objects.all().filter(resource=1).filter(date=now).order_by('start').values() and now considering how to manipulate that.

Example:

[{'name': u'Test', 'resource_id': 1L, 'hours': 6L, 'start': u'10', 'date': datetime.date(2017, 1, 13), 'id': 31L, 'resource_quantity_booked': 1L}, {'name': u'Me', 'resource_id': 1L, 'hours': 1L, 'start': u'8', 'date': datetime.date(2017, 1, 13), 'id': 33L, 'resource_quantity_booked': 1L}, {'name': u'Testing', 'resource_id': 1L, 'hours': 4L, 'start': u'9', 'date': datetime.date(2017, 1, 13), 'id': 32L, 'resource_quantity_booked': 1L}]

 

If it is possible to manipulate that dict (or object) to determine that tool1 can be expressed as a booking of 1 hour at 9 and then booked from 10 for 6 hours and therefore the next booking has to be applied to tool2

The wrinkle is that more than 1 tool can be booked to a job but the logic to determine tool2 is required to accommodate booking object 3 then similar logic should be able to determine that if all hours are filled on 2 objects therefore 2 tools are required.

Kinda hoping to use this manipulated dict in the view to send html to the template like:

 

<tr><td>8</td><td>9</td>…….etc</tr>

<tr><td>BOOKING 1 determined tool1 has a job here for 1 hour</td><td>BOOKING 2 determined that tool1 has a job here</td><td>tool1 still in use</td>…….etc until hours used then unbooked hours<td><a href=”book?time=x&resource=1&date={{now}}”>Book</a></td>… etc

 

And if there are no bookings for a tool on a day to just output rows of <td><a href=”book?time=x&resource=1&date={{now}}”>Book</a></td> for the currently unused tools just in case another tool is required.

 

Hope that makes better sense


- Are you looking to check if there are tools of a given type available at a given time? Yes, by outputting the bookings on the tools in a tabular format by time


- And whether a tool of a given type can be booked in a given time window? Yes


- When you said you wanted to “fold” 2 bookings into the same time window - intuitively at first glance a booking app would want to do the opposite, ie. find empty time windows - could you elaborate on these booking use cases? Is this for a single customer who wants to book multiple tools at a time?  I agree, kinda given up on that idea

 

‘Customer’ the shop can either use a single tool on a job for any number of hours in a day and could also require more than 1 tool for any number of hours


- Sharing the Tool and Booking models would be useful.

class Resource(models.Model):     // various tools

    resource = models.CharField(max_length=30)

    resource_capacity = models.IntegerField()

    seating = models.IntegerField()

 

    def __unicode__(self):

        return self.resource

 

 

class Booking(models.Model):

    resource = models.ForeignKey(Resource)

    resource_quantity_booked = models.IntegerField()

    start = models.CharField(max_length=5, help_text="whole number like 8, 9, 10, 11, 12, 13, 14, 15, 16, 17")

    hours = models.IntegerField(max_length=2, help_text="Number of hours as a whole number like 3")

    date = models.DateField()

 

    class Meta:

        ordering = ('date',)


This may be too basic, but for time queries if you’re not already aware there are the keyword lookups gt, gte (greater than, greater than or equal to) and lt, lte, for example:

now = timezone.now()
active_bookings_for_tool_type = Booking.objects.filter(tool__resource=2, start_time__lte=now, finish_time__gte=now)
and timedelta from the datetime module is useful for handling time intervals.

 

Aware of these keywords thanks

 


Also there are some open source django booking apps out there which look pretty good, for example django-booking: https://github.com/bitlabstudio/django-booking - could be worth looking into?

Unfortunately this is an old site written in 1.3.1 with a suite of custom tools developed for 1.3.1 and would have to be completely re-written entirely.. good thought though.

 

Another thought occurred that may or may not be practical as I it is just a thought, currently I have set up the models so a tool is assigned to a booking and am wondering about what the possibilities would be of reversing that and making a booking assigned to a tool?



On Tuesday, January 10, 2017 at 3:05:33 PM UTC, MikeKJ wrote:

May be another way of doing this?

5 tools @ 9 hours = 45 hours maximum possible utilisation
1 tool = 9 hours maximum possible utilisation

each tool is a list
pa = [] (tool1)
pb = [] (tool2)  etc

max = 9
need to know the count of objects
loop all the booking objects
use the max count as the index
test if all booked hours exceeds max if not then list pa is complete
else
add together object hours to not exceed max and make each list pa, pb etc
ending up with
pa = [(object, object, object)]
pb = [(object, object, object, object)]   etc

then unpack the tuples in the template with if statements to position as to time and duration

Would that work better?
What code would ach
ieve it?
Appreciate any help here

       

   

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/2VkJ-c2VgfI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/141e1af8-86c2-498e-b6ce-0a39a4fc3ac2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

MikeKJ

unread,
Jan 13, 2017, 6:42:02 AM1/13/17
to Django users

Thanks for response I’ll try to answer as below in red

 


Subject: Re: list? queryet? joining together

 

Hi, just a couple of follow-up questions as having trouble following precisely.



Can you re-state the exact queries you’re looking for?

Your comment about ‘folding’ model objects makes sense so,

 

I thought some more about it and am thinking about creating a dict of all bookings for a tool for the day Booking.objects.all().filter(resource=1).filter(date=now).order_by('start').values() and now considering how to manipulate that.

Example:

[{'name': u'Test', 'resource_id': 1L, 'hours': 6L, 'start': u'10', 'date': datetime.date(2017, 1, 13), 'id': 31L, 'resource_quantity_booked': 1L}, {'name': u'Me', 'resource_id': 1L, 'hours': 1L, 'start': u'8', 'date': datetime.date(2017, 1, 13), 'id': 33L, 'resource_quantity_booked': 1L}, {'name': u'Testing', 'resource_id': 1L, 'hours': 4L, 'start': u'9', 'date': datetime.date(2017, 1, 13), 'id': 32L, 'resource_quantity_booked': 1L}]

 

If it is possible to manipulate that dict (or object) to determine that tool1 can be expressed as a booking of 1 hour at 9 and then booked from 10 for 6 hours and therefore the next booking has to be applied to tool2

The wrinkle is that more than 1 tool can be booked to a job but the logic to determine tool2 is required to accommodate booking object 3 then similar logic should be able to determine that if all hours are filled on 2 objects therefore 2 tools are required.

Kinda hoping to use this manipulated dict in the view to send html to the template like:

 

<tr><td>8</td><td>9</td>…….etc</tr>

<tr><td>BOOKING 1 determined tool1 has a job here for 1 hour</td><td>BOOKING 2 determined that tool1 has a job here</td><td>tool1 still in use</td>…….etc until hours used then unbooked hours<td><a href=”book?time=x&resource=1&date={{now}}”>Book</a></td>… etc

 

And if there are no bookings for a tool on a day to just output rows of <td><a href=”book?time=x&resource=1&date={{now}}”>Book</a></td> for the currently unused tools just in case another tool is required.

 

Hope that makes better sense


- Are you looking to check if there are tools of a given type available at a given time? Yes, by outputting the bookings on the tools in a tabular format by time


- And whether a tool of a given type can be booked in a given time window? Yes


- When you said you wanted to “fold” 2 bookings into the same time window - intuitively at first glance a booking app would want to do the opposite, ie. find empty time windows - could you elaborate on these booking use cases? Is this for a single customer who wants to book multiple tools at a time?  I agree, kinda given up on that idea

 

‘Customer’ the shop can either use a single tool on a job for any number of hours in a day and could also require more than 1 tool for any number of hours

- Sharing the Tool and Booking models would be useful.

class Resource(models.Model):     // various tools

    resource = models.CharField(max_length=30)

    resource_capacity = models.IntegerField()

    seating = models.IntegerField()

 

    def __unicode__(self):

        return self.resource

 

 

class Booking(models.Model):

    resource = models.ForeignKey(Resource)

    resource_quantity_booked = models.IntegerField()

    start = models.CharField(max_length=5, help_text="whole number like 8, 9, 10, 11, 12, 13, 14, 15, 16, 17")

    hours = models.IntegerField(max_length=2, help_text="Number of hours as a whole number like 3")

    date = models.DateField()

 

    class Meta:

        ordering = ('date',)

This may be too basic, but for time queries if you’re not already aware there are the keyword lookups gt, gte (greater than, greater than or equal to) and lt, lte, for example:

now = timezone.now()
active_bookings_for_tool_type = Booking.objects.filter(tool__resource=2, start_time__lte=now, finish_time__gte=now)
and timedelta from the datetime module is useful for handling time intervals.

 

Aware of these keywords thanks

 


Also there are some open source django booking apps out there which look pretty good, for example django-booking: https://github.com/bitlabstudio/django-booking - could be worth looking into?

Unfortunately this is an old site written in 1.3.1 with a suite of custom tools developed for 1.3.1 and would have to be completely re-written entirely.. good thought though.

Reply all
Reply to author
Forward
0 new messages