Hello, I have a django project related to an online teaching system. There is a course with several chapters. The user can do, for example, two chapters a day. So I would like him to have access to just those two chapters. If on day X, he only did one of the two, then on the next he would do the pending chapter and another new one, from the other day, always remaining two chapters a day. If he does both on the same day, as he really should, then the system blocks it and he has to wait for the next day to continue the course.
So, basically, the logic would be like this, with, for example, three chapters per day:
I already tried to do this in several ways, using the DB, but I still couldn’t find the best solution. If you need any code, just say so, please.
First, what do you consider a “day”? Are you referring to a calendar day, or any arbitrary 24-hour period starting with the time that the first chapter is started?
If a “calendar day”, are you accounting for the individual’s time zone?
Do you already have code that tracks when a person starts a chapter?
Do you already have code that determines whether or not a person can start a chapter?
Yes, it would be helpful to see your models for the users, the chapters, and whatever models you’re using to relate those two models.
Does this mean that you already have working code, but it doesn’t “feel right” to you? If so, posting that code would help, too.
Yes, it’s a “calendar day”. After midnight, it’s considered another day.
I have three methods related to this, all returning an array:
one returns the number of chapters completed, another the number of chapters not completed, and the one that returns the number of total chapters, of course.
No, I don’t. From the moment she has the course, she can already make the chapters, but I would like to limit the number of daily chapters, with a number that I defined myself.
Which “calendar day”? Your’s? The person using the site? UTC? (Note, I don’t need to know the answer to this question, but you do.)
Side note 2: I’d change “chapters_per_day” to be an integer field. You can still create a select widget for it, but it is a numerical value that you will be performing math or comparisons with.
Basically, I’d be looking at it this way. Subtract the number of chapters started (or completed) “today” from the total number of chapters allowed to be started (or completed) “today”, and use that difference to enable the next “N” chapters.
Side note 3: You’re doing a lot of manual work with these querysets that you don’t need to do. For example, in your get_object method, you’re referring to qs, where you could use the first() function in your query to return the first element of the queryset. You may want to review all of the functions at QuerySet API reference | Django documentation | Django to get some ideas of what all is available to you. Likewise, you’re doing a lot of work with strings that should more appropriately be done with the proper Python objects.
It can be UTC-3, doesn’t matter at first, I can do some conversions after, if necessary. :))
In this case, that’s the problem. Using Ajax or not, doens’t matter at first, beacuse I’d need to limit the number of chapters per day. For example, when the user finishes the two available chapters, how can I lock my queue of available chapters so that it’s only available tomorrow? I tried to apply a timeout, but the page would be loading infinitely, until the timeout ended, so it’s unfeasible.
When you’re rendering the page, check to see if the person is eligible to open a new chapter. You count the number of chapters used today to the number of chapters they’re able to use in a day to determine what they see. There’s no “timeout” or “lock” necessary.