Python django.contrib.sessions.backends.base.SessionBase() Examples

The following are 6 code examples of django.contrib.sessions.backends.base.SessionBase(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.sessions.backends.base , or try the search function .
Example #1
Source File: manage_session.py    From ontask_b with MIT License 6 votes vote down vote up
def __init__(
        self,
        session: Optional[SessionBase] = None,
        initial_values: Optional[Dict] = None,
    ):
        """Initialize the store with the given arguments."""
        super().__init__()
        self.__store = {}
        if session:
            in_session = session.get(PAYLOAD_SESSION_DICTIONARY)
            if in_session:
                self.__store = in_session

        if initial_values:
            self.__store.update(initial_values)
        if session:
            self.store_in_session(session) 
Example #2
Source File: utils.py    From django-cas-ng with MIT License 5 votes vote down vote up
def get_user_from_session(session: SessionBase) -> Union[User, AnonymousUser]:
    """
    Get User object (or AnonymousUser() if not logged in) from session.
    """
    try:
        user_id = session[SESSION_KEY]
        backend_path = session[BACKEND_SESSION_KEY]
        backend = load_backend(backend_path)
        return backend.get_user(user_id) or AnonymousUser()
    except KeyError:
        return AnonymousUser() 
Example #3
Source File: conftest.py    From mrs with GNU Affero General Public License v3.0 5 votes vote down vote up
def generic(self, *args, **kwargs):
        request = super().generic(*args, **kwargs)
        request.session = SessionBase()
        request.user = self.user
        request._messages = default_storage(request)
        return request 
Example #4
Source File: manage_session.py    From ontask_b with MIT License 5 votes vote down vote up
def flush(cls, session: SessionBase):
        """Remove the table from the session."""
        session.pop(PAYLOAD_SESSION_DICTIONARY, None) 
Example #5
Source File: manage_session.py    From ontask_b with MIT License 5 votes vote down vote up
def store_in_session(self, session: SessionBase):
        """Set the payload in the current session.

        :param session: Session object
        """
        session[PAYLOAD_SESSION_DICTIONARY] = self.__store
        session.save() 
Example #6
Source File: zip.py    From ontask_b with MIT License 4 votes vote down vote up
def create_and_send_zip(
    session: SessionBase,
    action: models.Action,
    item_column: models.Column,
    user_fname_column: Optional[models.Column],
    payload: SessionPayload,
) -> http.HttpResponse:
    """Process the list of tuples in files and create the ZIP BytesIO object.

    :param session: Session object while creating a zip (need it to flush it)
    :param action: Action being used for ZIP
    :param item_column: Column used to itemize the zip
    :param user_fname_column: Optional column to create file name
    :param payload: Dictionary with additional parameters to create the ZIP
    :return: HttpResponse to send back with the ZIP download header
    """
    files = _create_eval_data_tuple(
        action,
        item_column,
        payload.get('exclude_values', []),
        user_fname_column)
    file_name_template = _create_filename_template(payload, user_fname_column)

    # Create the ZIP and return it for download
    sbuf = BytesIO()
    zip_file_obj = zipfile.ZipFile(sbuf, 'w')
    for user_fname, part_id, msg_body in files:
        if payload['zip_for_moodle']:
            # If a zip for Moodle, field is Participant [number]. Take the
            # number only
            part_id = part_id.split()[1]

        zip_file_obj.writestr(
            file_name_template.format(user_fname=user_fname, part_id=part_id),
            str(msg_body),
        )
    zip_file_obj.close()

    SessionPayload.flush(session)

    suffix = datetime.now().strftime('%y%m%d_%H%M%S')
    # Attach the compressed value to the response and send
    compressed_content = sbuf.getvalue()
    response = http.HttpResponse(compressed_content)
    response['Content-Type'] = 'application/x-zip-compressed'
    response['Content-Transfer-Encoding'] = 'binary'
    response['Content-Disposition'] = (
        'attachment; filename="ontask_zip_action_{0}.zip"'.format(suffix))
    response['Content-Length'] = str(len(compressed_content))

    return response