# Path: multiseek/logic.py
# MULTISEEK_REPORT_TYPE = "_ms_report_type"
# 
# Path: multiseek/models.py
# class SearchForm(models.Model):
#     name = models.TextField(verbose_name=_("Name"), unique=True)
#     owner = models.ForeignKey(settings.AUTH_USER_MODEL,
#                               on_delete=models.CASCADE)
#     public = models.BooleanField(
#         verbose_name=_("Public"),
#         default=False, help_text=_(
#             "Make this search publicly available?"))
#     data = models.TextField(verbose_name=_("Form data (JSON)"))
# 
#     objects = SearchFormManager()
# 
#     class Meta:
#         ordering = ['name',]
# 
#     def __str__(self):
#         return self.name
# 
# Path: multiseek/logic.py
# AND = "and"
# 
# AUTOCOMPLETE = "autocomplete"
# 
# MULTISEEK_ORDERING_PREFIX = "order_"
# 
# OR = "or"
# 
# VALUE_LIST = "value-list"
# 
# class ParseError(Exception):
#     pass
# 
# class UnknownField(Exception):
#     pass
# 
# class UnknownOperation(Exception):
#     pass
# 
# def get_registry(registry):
#     """
#     :rtype: MultiseekRegistry
#     """
# 
#     if isinstance(registry, str):
#         if registry not in _cached_registry:
#             m = importlib.import_module(registry).registry
#             _cached_registry[registry] = m
#         return _cached_registry[registry]
# 
#     return registry


import json
from builtins import str as text
from django import http, shortcuts
from django.db import transaction
from django.http import HttpResponseForbidden, HttpResponseNotFound
from django.http.response import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.utils.translation import gettext_lazy
from django.utils.translation import gettext_lazy as _
from django.views.decorators.cache import never_cache
from django.views.generic import ListView, TemplateView
from multiseek.logic import MULTISEEK_REPORT_TYPE
from multiseek.models import SearchForm
from .logic import (
    AND,
    AUTOCOMPLETE,
    MULTISEEK_ORDERING_PREFIX,
    OR,
    VALUE_LIST,
    ParseError,
    UnknownField,
    UnknownOperation,
    get_registry,
)
    def get(self, request, *args, **kw):
        if not user_allowed_to_save_forms(request.user):
            return HttpResponseForbidden()
        return super(MultiseekSaveForm, self).get(request, *args, **kw)

    post = get

    @transaction.atomic
    def get_context_data(self):
        _json = self.request.POST.get("json")
        name = self.request.POST.get("name")
        public = self.request.POST.get("public") == "true"
        overwrite = self.request.POST.get("overwrite") == "true"

        if not _json:
            return dict(result=text(ERR_NO_FORM_DATA))

        try:
            json.loads(_json)
        except ValueError:
            return dict(result=text(ERR_PARSING_DATA))

        try:
            get_registry(self.registry).recreate_form(json.loads(_json))
        except (TypeError, UnknownField, ParseError, UnknownOperation):
            return dict(result=text(ERR_LOADING_DATA))

        if not name:
            return dict(result=text(ERR_FORM_NAME))


# Path: multiseek/logic.py
# MULTISEEK_REPORT_TYPE = "_ms_report_type"
# 
# Path: multiseek/models.py
# class SearchForm(models.Model):
#     name = models.TextField(verbose_name=_("Name"), unique=True)
#     owner = models.ForeignKey(settings.AUTH_USER_MODEL,
#                               on_delete=models.CASCADE)
#     public = models.BooleanField(
#         verbose_name=_("Public"),
#         default=False, help_text=_(
#             "Make this search publicly available?"))
#     data = models.TextField(verbose_name=_("Form data (JSON)"))
# 
#     objects = SearchFormManager()
# 
#     class Meta:
#         ordering = ['name',]
# 
#     def __str__(self):
#         return self.name
# 
# Path: multiseek/logic.py
# AND = "and"
# 
# AUTOCOMPLETE = "autocomplete"
# 
# MULTISEEK_ORDERING_PREFIX = "order_"
# 
# OR = "or"
# 
# VALUE_LIST = "value-list"
# 
# class ParseError(Exception):
#     pass
# 
# class UnknownField(Exception):
#     pass
# 
# class UnknownOperation(Exception):
#     pass
# 
# def get_registry(registry):
#     """
#     :rtype: MultiseekRegistry
#     """
# 
#     if isinstance(registry, str):
#         if registry not in _cached_registry:
#             m = importlib.import_module(registry).registry
#             _cached_registry[registry] = m
#         return _cached_registry[registry]
# 
#     return registry


# Path: multiseek/views.py
import json
from builtins import str as text
from django import http, shortcuts
from django.db import transaction
from django.http import HttpResponseForbidden, HttpResponseNotFound
from django.http.response import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.utils.translation import gettext_lazy
from django.utils.translation import gettext_lazy as _
from django.views.decorators.cache import never_cache
from django.views.generic import ListView, TemplateView
from multiseek.logic import MULTISEEK_REPORT_TYPE
from multiseek.models import SearchForm
from .logic import (
    AND,
    AUTOCOMPLETE,
    MULTISEEK_ORDERING_PREFIX,
    OR,
    VALUE_LIST,
    ParseError,
    UnknownField,
    UnknownOperation,
    get_registry,
)

    def get(self, request, *args, **kw):
        if not user_allowed_to_save_forms(request.user):
            return HttpResponseForbidden()
        return super(MultiseekSaveForm, self).get(request, *args, **kw)

    post = get

    @transaction.atomic
    def get_context_data(self):
        _json = self.request.POST.get("json")
        name = self.request.POST.get("name")
        public = self.request.POST.get("public") == "true"
        overwrite = self.request.POST.get("overwrite") == "true"

        if not _json:
            return dict(result=text(ERR_NO_FORM_DATA))

        try:
            json.loads(_json)
        except ValueError:
            return dict(result=text(ERR_PARSING_DATA))

        try:
            get_registry(self.registry).recreate_form(json.loads(_json))
        except (TypeError, UnknownField, ParseError, UnknownOperation):
            return dict(result=text(ERR_LOADING_DATA))

        if not name:
            return dict(result=text(ERR_FORM_NAME))


        if SearchForm.objects.filter(name=name).count():