Frage

I am creating a django app where users answer different questions in a survey, but if they have not logged in, their votes will be removed after three days.

The way I do it at the moment is as follows:

Once an anonymous user tries to answer a question, I create a new user, log them in and assign the answers. Once the user wants to create an account, I'll have them change their details.

The annoying side effect of this approach is: When I clandestinely create an account for an anonymous user to allow the voting, I have to pretend towards the user that they are not logged in. This is annoying with using the standard admin views and templates in general as I have to check an additional property, that tells me if I have automatically created the user or if the user themselves did it.

Is there a better way?

War es hilfreich?

Lösung

A simpler solution would be not tying the survey answers to the user ID. Give the survey response a separate, completely independent ID. When a real user is logged in, create a one-sided association from user to survey response. When the anonymous user registers, add this association as well. (That is, the "owner" of a survey will be optional.) Afterwards, periodically delete old survey responses that have no owner, and/or ignore outdated ones in queries.

This shifts most of the complexity from the admin app to user registration only. (I.e. when an anonymous user answers a survey, store the response ID in the session. When a new user registers, check the session for response IDs, and then assign it to the user ID.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top