Giter Club home page Giter Club logo

Comments (5)

r4fek avatar r4fek commented on July 20, 2024

Thanks for the report @EzequielAdrianM. For now I would recommend using regular relational database for storing users and cassandra as secondary backend.

Unfortunately authentication does not work yet with django_cassandra_engine backend, but this is on my TODO list.

from django-cassandra-engine.

EzequielAdrianM avatar EzequielAdrianM commented on July 20, 2024

Hi @r4fek I have managed to make my Django project work with Cassandra as the unique database backend. But my solution is temporal since I still ignore "request.user.is_authenticated()". I had to write my own SessionBackend for Cassandra and It works excellent ! The bad thing is that I am NOT using Django Authentication System but thanks to the "Modularity" of Django I use my custom SessionBackend to obtain and decode the SessionData and, check, whether a user is Authenticated or not.

from django-cassandra-engine.

r4fek avatar r4fek commented on July 20, 2024

@EzequielAdrianM and now session backend is included in latest dce! You can try it by yourself. We're one step closer to have working auth system based on Cassandra.

from django-cassandra-engine.

EzequielAdrianM avatar EzequielAdrianM commented on July 20, 2024

Hi, I have seen your update, but now I have migrated completely to Cassandra. Including the User tables. So I wanted to share with you my Fast Fix implementation. And what do you think about it.

from django.conf import settings
from django.utils.encoding import force_unicode
from django.contrib.sessions.backends.base import SessionBase, CreateError
from datetime import datetime
from datetime import timedelta
import cass

class SessionStore(SessionBase):
    """
    A Cassandra based session store.
    """
    def load(self):
        try:
            #GET SESSIONDATA BASED ON SESSIONKEY
            data = cass.get_session(self.session_key)
            #NOW
            a = datetime.now()
            #EXPIRE-DATE - NOW = TIME-LEFT
            c = data['expire_date'] - a
            if c.total_seconds() > 0:
                return self.decode(force_unicode(data['session_data']))
            #Manually remove session from DB
            cass.remove_session(session_key)
            self._session_key = None
            return {}
        except cass.DatabaseError:
            self._session_key = None
            return {}

    def create(self):
        while True:
            self._session_key = self._get_new_session_key()
            try:
                # Save immediately to ensure we have a unique entry in the database
                self.save(must_create=True)
            except CreateError:
                # Key wasn't unique. Try again.
                continue
            self.modified = True
            return

    def save(self, must_create=False):
        if self.session_key is None:
            return self.create()

        if must_create and self.exists(self.session_key):
            raise CreateError
        else:
            #DJANGO GENERATES NEW SESSION_DATA
            session_data = self.encode(self._get_session(no_load=must_create))
            #NOW + EXPIRY SECONDS = EXPIRE-DATE
            expire_date = datetime.now() + timedelta(seconds=self.get_expiry_age())
            #STRIP MICROSECONDS
            expire_date = expire_date.strftime("%Y-%m-%d %H:%M:%S")
            #ADD TIMEZONE
            expire_date = expire_date + '+0000'

            cass.create_session(self.session_key, session_data, expire_date)

    def exists(self, session_key):
        #CHECK IF SESSION_KEY EXISTS...
        return cass.check_session_existence(session_key)

    def delete(self, session_key=None):
        if session_key is None:
            if self.session_key is None:
                return
            session_key = self.session_key
        cass.remove_session(session_key)

And cass.py (Partially Taken from Twissandra Project, which is now deprecated)

from django.db import connection
cursor = connection.cursor()

# EXCEPTIONS
class DatabaseError(Exception):
    """
    The base error that functions in this module will raise when things go
    wrong.
    """
    pass
class NotFound(DatabaseError): pass
class InvalidDictionary(DatabaseError): pass

#COOKIES SESSION OPERATIONS
def create_session(session_key, session_data, expire_date):
    cursor.execute("INSERT INTO sessions (session_key, session_data, expire_date) VALUES ('%s', '%s', '%s')" % (session_key, session_data, expire_date));
    return True

def check_session_existence(session_key):
    result = cursor.execute("SELECT session_key FROM sessions WHERE session_key = '%s'" % session_key);
    if not result:
        return False
    return True

def remove_session(session_key):
    cursor.execute("DELETE FROM sessions WHERE session_key = '%s'" % session_key);
    return True

def get_session(session_key):
    result = cursor.execute("SELECT session_data, expire_date FROM sessions WHERE session_key = '%s'" % session_key);
    if not result:
        raise DatabaseError()
    return result[0]

from django-cassandra-engine.

SoumiBera avatar SoumiBera commented on July 20, 2024

@EzequielAdrianM Can you please provide the entire solution? I have one cassandra User table. And i am trying to create session-key for that particular user.

from django-cassandra-engine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.