Exemple #1
0
    def resolve_path(self, path):
        """Determines the publishable item that matches the indicated path.

        This method identifies a matching publishable item by trying each
        url scheme defined by the site, in order. Once a scheme finds a
        matching item, the search concludes.
        
        See L{URLScheme.resolve_path} for more details on the resolution
        process.
 
        @param path: The path to evaluate; A list-like object describing a
            a path relative to the application's root.
        @type path: str list

        @return: A structure containing the matching item and its publication
            details. If no matching item can be found, None is returned
            instead.
        @rtype: L{PathResolution}
        """
        if not path:
            website = get_current_website()
            if website:
                return PathResolution(None, website.home)
        else:
            for url_scheme in self._url_schemes:
                resolution = url_scheme.resolve_path(path)
                if resolution is not None:
                    return resolution
Exemple #2
0
    def _ready(self):

        if self.root is None:
            website = get_current_website()
            self.root = website and website.home

        self.depends_on(self.root)

        if self.selection is None:
            self.selection = context["publishable"]

        TreeView._ready(self)
Exemple #3
0
    def _apply_https_policy(self, publishable):

        policy = Configuration.instance.get_setting("https_policy")
        website = get_current_website()

        if policy == "always":
            Location.require_https()
        elif policy == "never":
            Location.require_http()
        elif policy == "per_page":
            if publishable.requires_https or not get_current_user().anonymous:
                Location.require_https()
            elif not website.https_persistence:
                Location.require_http()
Exemple #4
0
    def get_payment_url(self, *args, **kwargs):

        website = get_current_website()
        location = Location()
        location.relative = False

        if website.https_policy != "never":
            location.scheme = "https"

        location.host = website.hosts[0]
        location.path_info = "payments/" + str(self.id)
        location.join_path(*args)
        location.query_string.update(kwargs)

        return unicode(location)
Exemple #5
0
    def _maintenance_check(self, publishable):

        if isinstance(publishable, File):
            return

        config = Configuration.instance
        website = get_current_website()

        if (config.down_for_maintenance
                or (website and website.down_for_maintenance)):
            headers = cherrypy.request.headers
            client_ip = headers.get("X-Forwarded-For") \
                     or headers.get("Remote-Addr")

            if client_ip not in config.maintenance_addresses:
                raise cherrypy.HTTPError(503, "Site down for maintenance")
Exemple #6
0
    def _resolve_path(self, path):

        unicode_path = [try_decode(step) for step in path]
        path_resolution = app.url_resolver.resolve_path(unicode_path)

        if path_resolution:
            publishable = path_resolution.item

            for step in path_resolution.matching_path:
                path.pop(0)

            self.canonical_redirection(path_resolution)
        else:
            website = get_current_website()
            publishable = website.home if website else None

        return publishable
Exemple #7
0
    def handle_exception_raised(cls, event):

        # Couldn't establish the active website: show a generic error
        if get_current_website() is None:
            return

        error = event.exception
        controller = event.source

        content_type = cherrypy.response.headers.get("Content-Type")
        pos = content_type.find(";")

        if pos != -1:
            content_type = content_type[:pos]

        if content_type in ("text/html", "text/xhtml"):

            error_page, status = event.source.get_error_page(error)
            response = cherrypy.response

            if status:
                response.status = status

            if error_page:
                event.handled = True

                # HTTP/HTTPS check
                controller._apply_https_policy(error_page)

                controller.context.update(
                    original_publishable=controller.context["publishable"],
                    publishable=error_page)

                error_controller = error_page.resolve_controller()

                # Instantiate class based controllers
                if isinstance(error_controller, type):
                    error_controller = error_controller()
                    error_controller._rendering_format = "html"

                response.body = error_controller()
Exemple #8
0
 def _apply_website_exclusiveness(self, publishable):
     if (publishable.websites
             and get_current_website() not in publishable.websites):
         raise cherrypy.HTTPRedirect(
             publishable.get_uri(host=publishable.websites[0].hosts[0]))
Exemple #9
0
 def __init__(self, html):
     self.soup = BeautifulSoup(html)
     self.absolute_url_prefix = \
         "http://" + get_current_website().hosts[0]
Exemple #10
0
    def resubscribe(self, email):
        extension = CampaignMonitorExtension.instance

        api = CampaignMonitorApi(extension.api_key, extension.client_id)

        lists = api.client_get_lists()

        resubscribed_lists = 0
        cm_context = {"email": email, "lists": []}
        for i, list in enumerate(lists):
            try:
                response = api.subscribers_get_single_subscriber(
                    list.get("ListID"), email.encode("utf-8"))
            except CampaignMonitorApi.CampaignMonitorApiException:
                continue
            else:
                subscriber = response.get("Subscribers.GetSingleSubscriber")
                name = subscriber[0].get("Name")
                date = subscriber[0].get("Date")
                state = subscriber[0].get("State")

                cm_context["lists"].append({
                    "list_id": list.get("ListID"),
                    "name": name,
                    "state": state,
                    "date": date
                })

                if state == "Unsubscribed":
                    date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
                    now = datetime.now()

                    diff = now - date

                    if date > now or diff.seconds < self.max_seconds:
                        custom_fields = subscriber[0].get("CustomFields")

                        # Encode custom fields
                        encoded_custom_fields = {}
                        for key, value in custom_fields.items():
                            encoded_key = (key.encode("utf-8") if isinstance(
                                key, unicode) else key)
                            encoded_value = (value.encode("utf-8")
                                             if isinstance(value, unicode) else
                                             value)
                            encoded_custom_fields[encoded_key] = encoded_value

                            cm_context["lists"][i].update(
                                **encoded_custom_fields)

                        try:
                            # Resubscribe
                            resubscribed_lists += 1
                            api.subscriber_add_and_resubscribe(
                                list.get("ListID"), email.encode("utf-8"),
                                name.encode("utf-8") if name else name,
                                encoded_custom_fields)
                        except CampaignMonitorApi.CampaignMonitorApiException:
                            self.campaign_monitor_errors = True

        uri = None

        if resubscribed_lists == 0:
            uri = self.get_subscription_uri(**cm_context)
        else:
            uri = self.get_pending_uri(**cm_context)

        if uri is None:
            uri = get_current_website().home.get_uri()

        raise cherrypy.HTTPRedirect(uri.encode("utf-8"))