Skip to content

Commit

Permalink
Merge branch 'session' of https://github.com/poke1024/Mathics into se…
Browse files Browse the repository at this point in the history
…ssion
  • Loading branch information
rocky committed Sep 16, 2020
2 parents a05fe2d + 1680dd7 commit a8263c3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
12 changes: 0 additions & 12 deletions mathics/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,10 @@ def evaluate():
return result

def get_stored_result(self, result):
from mathics.core.expression import Symbol

# Remove outer format
if result.has_form(FORMATS, 1):
result = result.leaves[0]

# Prevent too large results from being stored, as this can exceed the
# DB's max_allowed_packet size
max_stored_size = self.output.max_stored_size(settings)
if max_stored_size is not None:
try:
data = pickle.dumps(result)
if len(data) > max_stored_size:
return Symbol('Null')
except (ValueError, pickle.PicklingError):
return Symbol('Null')
return result

def stop(self) -> None:
Expand Down
28 changes: 28 additions & 0 deletions mathics/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import pre_delete
from django.contrib.sessions.models import Session

from mathics.core.definitions import Definitions
from mathics.core.evaluation import Evaluation, Output


class WebOutput(Output):
pass

_evaluations = {}


def get_session_evaluation(session):
evaluation = _evaluations.get(session.session_key)
if evaluation is None:
definitions = Definitions(add_builtin=True)
evaluation = Evaluation(
definitions, format='xml', output=WebOutput())
_evaluations[session.session_key] = evaluation
return evaluation


def end_session_evaluation(sender, **kwargs):
session_key = kwargs.get('instance').session_key
del _evaluations[session_key]

pre_delete.connect(end_session_evaluation, sender=Session)


class Query(models.Model):
Expand Down
15 changes: 2 additions & 13 deletions mathics/web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from mathics.core.definitions import Definitions
from mathics.core.evaluation import Evaluation, Message, Result, Output

from mathics.web.models import Query, Worksheet
from mathics.web.models import Query, Worksheet, get_session_evaluation
from mathics.web.forms import LoginForm, SaveForm
from mathics.doc import documentation
from mathics.doc.doc import DocPart, DocChapter, DocSection
Expand All @@ -39,10 +39,6 @@ def __init__(self, result={}):
super(JsonResponse, self).__init__(response, content_type=JSON_CONTENT_TYPE)


class WebOutput(Output):
pass


def require_ajax_login(func):
def new_func(request, *args, **kwargs):
if not request.user.is_authenticated():
Expand All @@ -53,7 +49,6 @@ def new_func(request, *args, **kwargs):
from mathics.settings import default_pymathics_modules
definitions = Definitions(add_builtin=True, extension_modules=default_pymathics_modules)


def require_ajax_login(f):
return f

Expand Down Expand Up @@ -100,9 +95,7 @@ def query(request):
)
query_log.save()

user_definitions = request.session.get('definitions')
definitions.set_user_definitions(user_definitions)
evaluation = Evaluation(definitions, format='xml', output=WebOutput())
evaluation = get_session_evaluation(request.session)
feeder = MultiLineFeeder(input, '<notebook>')
results = []
try:
Expand All @@ -126,7 +119,6 @@ def query(request):
result = {
'results': [result.get_data() for result in results],
}
request.session['definitions'] = definitions.get_user_definitions()

if settings.LOG_QUERIES:
query_log.timeout = evaluation.timeout
Expand Down Expand Up @@ -227,10 +219,7 @@ def login(request):


def logout(request):
# Remember user definitions
user_definitions = request.session.get('definitions', {})
auth.logout(request)
request.session['definitions'] = user_definitions
return JsonResponse()


Expand Down

0 comments on commit a8263c3

Please sign in to comment.