Пример #1
0
    def add_booking_code(self, user, business_id, start, end, user_with):
        if user_with:
            # Make sure they are allowed to propose to this user
            if not business.user_owns_business(user.user_id(), business_id):
                return {"error": "not your business bro"}
            user_with = CoUser.get(user_with)
            if not user_with:
                return {"error":"that guy does not exist"}

            interaction = InteractionRecord.gql("WHERE business_id = :1 AND user = :2", business_id, user_with).get()

            if not interaction:
                return {"error":"You have no reason to talk to that user"}
            user = user_with
            contractor = True
            client = False
        else:
            contractor = False
            client = True
            need_to_create_this = InteractionRecord.guaranteed_get(business_id, user)

        new_booking = Booking(
            user = user,
            business_id = business_id,
            start_time = start,
            end_time = end,
            approved_by_customer = client,
            approved_by_contractor = contractor
        )
        new_booking.put()

        send_emails.booking_created(user, business_id, start, end, user_with)

        return new_booking
Пример #2
0
 def delete_booking(self, user, booking_id):
     booking = Booking.get(booking_id)
     if not booking:
         return {"error": "No such booking"}
     if str(booking.user.key()) == str(user.key()):
         booking.delete()
         return {"success": True}
     elif business.user_owns_business(user.user_id(), booking.business_id):
         booking.delete()
         return {"success": True}
     else:
         return {"error": "insufficient permissions"}
Пример #3
0
 def approve_booking(self, user, booking_id):
     booking = Booking.get(booking_id)
     if not booking:
         return {"error": "No such booking"}
     if str(booking.user.key()) == str(user.key()):
         booking.approved_by_customer = True
         booking.put()
         return model_to_dict(booking)
     if business.user_owns_business(user.user_id(), booking.business_id):
         booking.approved_by_contractor = True
         booking.put()
         return model_to_dict(booking)
     return {"error": "Insufficient permissions"}
Пример #4
0
    def get_bookings(self, user, business_id, user_id=None):
        # My intuition tells me that this is bad design
        # My laziness told it to shut up
        if user_id:
            if not business.user_owns_business(user.user_id(), business_id):
                return {"error": "You don't own that business"}

            # At this point, permissions are good
            user = CoUser.get(user_id)

        if not user:
            return []

        bookings = Booking.gql("WHERE user = :1 AND business_id = :2", user, business_id)
        return gql_to_raw(bookings)
Пример #5
0
    def get_availability(self, business_id, starting=None, until=None):
        # TODO: add a minimum appt length
        if not starting:
            starting = js_time()
        if not until:
            until = starting + (7*24*60*60*1000)

        available = ScheduleAvailability.gql("WHERE business_id = :1", business_id)
        booked = Booking.gql("WHERE business_id = :1", business_id)

        el_booking = NextMunger()
        el_available = NextMunger()

        for availability in available:
            el_available.add(WeeklyEvent(availability))
        for booking in booked:
            el_booking.add(SingleEvent(booking))

        ret_arr = []
        def maybe_append(el):
            # Just to make sure that we don't send tiny openings.
            # This will be better fixed in the future with better data integrity TODO: do that - AH
            # pulled this number out of what the bugs look like
            if el["end"] - el["start"] > 100000\
            and el["start"] < until:
                ret_arr.append(el)


        cursor = starting
        while cursor < until and el_available.next(cursor):
            next_available = el_available.next(cursor)
            next_book = el_booking.next(cursor)
            if next_book and next_book["start"]<next_available["start"]:
                cursor = next_book["end"] + 1 # + 1 is an extra ms just in case something broke with equal values in the algorithm
            elif next_book and next_book["start"] < next_available["end"]:
                augment = next_available
                augment["end"] = next_book["start"]
                if augment["end"] != augment["start"]:
                    maybe_append(augment)
                cursor = next_book["end"] + 1
            else:
                if next_available["end"] != next_available["start"]:
                    maybe_append(next_available)
                cursor = next_available["end"] + 1

        return ret_arr
Пример #6
0
    def edit_booking(self,user, business_id, booking_id, start, end):
        booking = Booking.get(booking_id)
        that_biz = Business.get(business_id)
        if end < start:
            return {"error": "what are you doing?  You can't end before you start"}
        if not booking or not that_biz or not ( booking.user == user or that_biz.owner == user):
            return {"error": "not allowed"}

        if booking.user == user:
            booking.approved_by_customer = True
            booking.approved_by_contractor = False
        else:
            booking.approved_by_contractor = True
            booking.approved_by_customer = False

        booking.start_time = start
        booking.end_time = end
        booking.put()
        return {"success": db.to_dict()}