0

I'm a novice with Django and I'm trying to figure out how to save a class instance for the current user, exactly what I'm trying to do is to log my user to Firebase, and then, retrieve data from the database on firebase and save that data on a local class which is only going to be available until the user logout, but all my attempts save those class instances make them global (so actually they are accesible for every user and will overwrite the previous one).

My first attempt was on the views.py of my app, and looked like this:

APPMANAGER = FirebaseManager()

def homepage(request: HttpRequest) -> HttpResponse:
    APPManager.loadData(request.POST.get("data"))
    return HttpResponse("Done")

But that APPMANAGER will be saved globally (and also will be erased if the server is rebooted).

I was checking also Django sessions (https://docs.djangoproject.com/en/2.2/topics/http/sessions/), which seem like what I want to do, but it just take data that is serializeable, and I want to save a custom class instance, what is the best approach to do this?

Ronald Petit
  • 474
  • 1
  • 4
  • 23
  • A bit of a novice in Django as well, so inquiring out of curiosity. When you say `save that data on a local class which is only going to be available until the user logout`, the first thought that comes to mind is that you can use the default [user auth](https://docs.djangoproject.com/en/2.2/topics/auth/), and invoke the user's information saved on server in the `views.py` via the `request.user` param. – felipe Nov 23 '19 at 22:16
  • @FelipeFaria Mainly because I don't want to store my Firebase users on the local database, I want to use the user auth from Django to supply a user system apart from firebase, resuming this, I'm creating an admin panel for Firebase, which should not be together the Users table of Django as the main web application will have their own users (the firebase admin panel will be an app and the web app will be another app) – Ronald Petit Nov 23 '19 at 22:21
  • Perhaps using the Firebase API (i.e. [Pyrebase](https://github.com/thisbejim/Pyrebase)) to log the user in? Check this post out: [Using Firebase Auth with Django](https://stackoverflow.com/questions/48232029/using-firebase-auth-with-django). [Here](https://firebase.google.com/docs/auth/admin/verify-id-tokens) is the Firebase documentation on the verification of ID tokens. – felipe Nov 23 '19 at 22:27
  • @FelipeFaria I'm actually doing that, but then I cannot save the user data as I don't know where to save it, following that path lead me to this question – Ronald Petit Nov 23 '19 at 22:28
  • You would set it in the `request.session`, from my understanding -- which is the user's cookies. – felipe Nov 23 '19 at 22:36
  • @FelipeFaria But I need to save the class instance, and seems like I cannot do that, or yes?, can you share an example?, over the documentation the examples only show serializable data – Ronald Petit Nov 23 '19 at 22:37
  • 1
    I really really wish I could be of more help, but I don't have much experience with `sessions` myself. Perhaps checking out some of these projects might help: [`drf-firebase-auth`](https://github.com/garyburgmann/drf-firebase-auth), [`drf-firebase-authentication`](https://github.com/DoctorJohn/drf-firebase-authentication), [`django-firebase-auth`](https://github.com/fcornelius/django-firebase-auth). They are all fairly small projects -- the first seems to save the Firebase ID in a new model with foreign-key to the Djago's user (as opposed to session). – felipe Nov 23 '19 at 22:44

1 Answers1

0

Thanks to the comments of @FelipeFaria I was able to achieve what I wanted.

I used Django sessions and the cache stored over the database seems to me like the correct way to do this, first, I configured the sessions installing the following django app (actually it was installed by default)

Settings.py

INSTALLED_APPS = [
    #otherApps
    'django.contrib.sessions',
    #OtherApps
]

Then I added the following to another line:

SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'

This will configure the sessions to save the cache over the database, documentation says that the performance is pretty good, but a simple cache system is the best as it disregard persistence (which I need).

After that, I stored the data I needed with dictionaries on the request.session dictionary provided by Django (the request is the HttpRequest sent on every Django view), and when needed, I created a new instance of my class and ran a method to load all the properties from the cache.

Example of views.py

def index(request: HttpRequest) -> HttpResponse:
    if 'logged in' in request.session:
        user = User()
        user.load_data(request.session['user_data'])
        return render(request, "main/index.html", context={"user": user})        
    return redirect("main:index")

A better explaination of the getters, setters, deleters and those is better explained in the documentation: https://docs.djangoproject.com/en/2.2/topics/http/sessions/#configuring-sessions

Ronald Petit
  • 474
  • 1
  • 4
  • 23