def create(request, room_pk): try: # room_pk = request.GET.get("pk", None) year = int(request.GET.get("year", None)) month = int(request.GET.get("month", None)) day = int(request.GET.get("day", None)) nights = int(request.GET.get("nights", None)) debug.info(f"{room_pk} {year} {month} {day} {nights}") date_obj = datetime.datetime(year, month, day) room = room_models.Room.objects.get(pk=room_pk) models.BookedDay.objects.get(day=date_obj, reservation__room=room) raise CreateError() except (room_models.Room.DoesNotExist, CreateError): messages.error(request, "Can't Reserve That Room") return redirect(reverse("core:home")) except models.BookedDay.DoesNotExist: reservation = models.Reservation.objects.create( guest=request.user, room=room, check_in=date_obj, check_out=date_obj + datetime.timedelta(days=nights), ) debug.info(reservation.pk) # return redirect(reverse("core:home")) # return redirect(reverse("reservations:review", kwargs={"pk": reservation.pk})) return redirect( reverse("reservations:detail", kwargs={"pk": reservation.pk}))
def get(self, *args, **kwargs): pk = kwargs.get("pk") """ Overriding get() by using a custom manager (CustomModelManager) in order to filter a request accessing a reservation which doen't exist """ debug.info(pk) # Move this code to CustomModelManager """ try: reservation = models.Reservation.objects.get(pk=pk) except models.Reservation.DoesNotExist: pass """ # and use .get_or_none() instead reservation = models.Reservation.objects.get_or_none(pk=pk) # raise Http404 # if the reservation pk does not exist # or user is neither a guest nor a host if not reservation or ( reservation.guest != self.request.user # not a guest and reservation.room.home != self.request.user # not a host ): raise Http404() # set reviews/form for reservations/template (detail.html) form = review_forms.CreateReviewForm() return render(self.request, "reservations/detail.html", { "reservation": reservation, "form": form })
def save(self, *args, **kwargs): """ Overriding save() To create new booked days only when new reservation is created. """ if self.pk is None: # for a new reservation # create new booked days start = self.check_in end = self.check_out difference = end - start # datetime difference! it's not an integer! existing_booked_day = ( BookedDay.objects.filter(reservation__room=(self.room)).filter( # In Django Model QuerySet Objects, Double Underscore ( __ ) Means "Field Lookups". # day__range: field-name__lookup-type-keywordm" # Simply, just replace and think "__" is "`s" in any situations in Django # like day__range=(start, end): day's range is between start and end day__range=( start, end ) # day__range: find BookedDay.day between start and end ).exists() ) # if BookedDay exists between start & end day or not if existing_booked_day is False: # First, Save a new Reservation super().save(*args, **kwargs) # Then, Create a new BookedDay for the Reservation for i in range(difference.days): day = start + datetime.timedelta(days=i) debug.info(f"{day} booked") # According to relation, BookedDay wouldn't be able to be created if reservation wasn't saved BookedDay.objects.create(day=day, reservation=self) else: print(f"booked day is existing between {start} and {end}") else: # for an existing reservation # do as usual return super().save(*args, **kwargs)
async def send_processing(ctx: commands.Context): msg: discord.Message = ctx.message user = discord.User = ctx.author debug.info( "command:\"{}\" id:{} user:{}:{}".format(msg.content, msg.id, user.name, user.id)) await msg.add_reaction("⏳")
async def send_done(ctx: commands.Context): msg: discord.Message = ctx.message debug.info('done') await msg.add_reaction("✅")
async def on_ready(): debug.info("Logged in as") debug.info(bot.user.name) debug.info(bot.user.id) debug.info("------")
async def on_message(msg: discord.Message): user: discord.User = msg.author if user.id == bot.user.id: return if is_private(msg) and has_permission(user): s_message: str = msg.content # debug.log(s_message == "") attachments: List[discord.Attachment] = msg.attachments if len(attachments) == 1: attachment: discord.Attachment = attachments[0] debug.info(attachment.filename) debug.info(attachment.id) debug.info(attachment.proxy_url) debug.info(attachment.size) debug.info(attachment.url) filename: str = attachment.filename if s_message == '' else s_message try: debug.info("download") await reaction.send_processing_msg(msg) await attachment.save(open("data/music/{0}".format(filename), "ab")) await reaction.send_done_msg(msg) except: await reaction.send_error_msg(msg) await bot.process_commands(msg)
await attachment.save(open("data/music/{0}".format(filename), "ab")) await reaction.send_done_msg(msg) except: await reaction.send_error_msg(msg) await bot.process_commands(msg) @bot.event async def on_command_error(ctx: commands.Context, error: commands.CommandError): await reaction.send_error(ctx, error) if __name__ == '__main__': args = sys.argv global token global guild_id global vc_id global admin_id global role_id token = args[1] debug.info("token:{}".format(token)) guild_id = int(args[2]) debug.info("guild_id:{}".format(guild_id)) vc_id = int(args[3]) debug.info("vc_id:{}".format(vc_id)) admin_id = int(args[4]) debug.info("admin_id:{}".format(admin_id)) role_id = int(args[5]) debug.info("role_id:{}".format(role_id)) bot.run(token)