def ajax_popup(request, popup_id='', url='/'):
    "Handles pop up forms and requests, by extracting only the required content from response content"

    view, args, kwargs = resolve(url)

    if not request.user.username:
        return HttpResponseRedirect('/accounts/login')

    modules = Module.objects.all()
    active = None
    for module in modules:
        try:
            import_name = module.name + "." + \
                          settings.HARDTREE_MODULE_IDENTIFIER
            hmodule = __import__(import_name, fromlist=[str(module.name)])
            urls = hmodule.URL_PATTERNS
            for regexp in urls:
                if re.match(regexp, url):
                    active = module
        except ImportError:
            pass
        except AttributeError:
            pass

    response = None
    if active:
        if not request.user.profile.has_permission(active):
            response = user_denied(request, "You do not have access to the %s module" % unicode(active),
                                   response_format='ajax')

    if not response:
        if view == ajax_popup:
            raise Http404("OMG, I see myself!")

        kwargs['request'] = request
        kwargs['response_format'] = 'ajax'
        response = view(*args, **kwargs)

        # response = csrf().process_response(request, response)

    module_inner = ""
    regexp = r"<!-- module_content_inner -->(?P<module_inner>.*?)<!-- /module_content_inner -->"
    blocks = re.finditer(regexp, response.content, re.DOTALL)
    for block in blocks:
        module_inner += block.group('module_inner').strip()

    title = ""
    regexp = r"<div class=\\\"title\\\">(?P<title>.*?)</div>"
    blocks = re.finditer(regexp, response.content, re.DOTALL)
    for block in blocks:
        title += block.group('title').replace('\\n', '').strip()
    if not title:
        blocks = re.finditer(
            r"<title>(?P<title>.*?)</title>", response.content, re.DOTALL)
        for block in blocks:
            title += block.group('title').replace('\\n', '').strip()

    subtitle = ""
    regexp = r"<div class=\\\"subtitle-block\\\">(?P<subtitle>.*?)</div>"
    blocks = re.finditer(regexp, response.content, re.DOTALL)
    for block in blocks:
        subtitle += block.group('subtitle').replace('\\n', '').strip()

    context = {'content': module_inner, 'title': title, 'subtitle': subtitle, 'popup_id': popup_id, 'url': request.path}

    if settings.HARDTREE_RESPONSE_FORMATS['json'] in response.get('Content-Type', 'text/html'):
        new_response = render_to_response('core/ajax_popup', context,
                                          context_instance=RequestContext(request), response_format='json')
    else:
        new_response = HttpResponse(json.dumps({'popup': context}))

    new_response.mimetype = settings.HARDTREE_RESPONSE_FORMATS['json']
    try:
        jsonresponse = json.loads(response.content)
        if 'redirect' in jsonresponse:
            new_response.status_code = 302
    except Exception:
        new_response.status_code = response.status_code

    return new_response
dataset/ETHPy150Open treeio/treeio/treeio/core/views.py/ajax_popup/original