Exemple #1
0
    def restore(cls):
        session_data = session.get(cls.session_key)

        if session_data is None:
            return None
        else:
            return ECommerceOrder.get_instance(session_data)
Exemple #2
0
    def _get_edit_stack(self, stack_id, preserved_stacks=None):

        edit_stack = self.__stack_map.get(stack_id)

        if edit_stack is None:

            if preserved_stacks is None:
                preserved_stacks = session.get(self._session_key)

            if preserved_stacks:

                entry = preserved_stacks.get(stack_id)

                if entry is None:
                    raise WrongEditStackError(stack_id)

                last_update, stack_data = entry

                if self.expiration is not None:
                    preserved_stacks[stack_id] = (datetime.now(), stack_data)

                edit_stack = self._loads(stack_data)
                self.__stack_map[stack_id] = edit_stack

        self._remove_expired_edit_stacks(preserved_stacks)

        return edit_stack
Exemple #3
0
def pop_user_notifications(filter=None):
    """Retrieves pending notification messages that were stored through the
    L{notify_user} method.

    Retrieved messages are considered to be consumed, and therefore they
    are removed from the list of pending notifications.

    @param filter: If given, only those notifications matching the specified
        criteria will be retrieved. The criteria matches as follows:

            * If set to a string, it designates a single notification category
              to retrieve.
            * If set to a sequence (list, tuple, set), it designates a set of
              categories; notifications belonging to any of the specified
              categories will be retrieved.
            * If set to a callable, it will be used as filter function, taking
              a notification tuple as its single parameter, and returning True
              if the notification is to be retrieved, or False if it should be
              ignored.

    @return: The sequence of pending notification messages. Each message
        consists of a tuple with the message text, its category and wether
        or not it should be treated as a transient message.
    @rtype: sequence of (tuple of (unicode, unicode or None, bool))
    """
    notifications = session.get("notifications")

    if notifications is None:
        return []

    remaining = []

    if filter:
        matching = []

        if isinstance(filter, basestring):
            match = lambda notification: notification[1] == filter
        elif callable(filter):
            match = filter
        elif hasattr("__contains__", filter):
            match = lambda notification: notification[1] in filter
        else:
            raise TypeError(
                "The 'filter' parameter for the pop_user_notifications() "
                "function should be a string, a sequence or callable, got "
                "%s instead" % type(filter))

        for notification in notifications:
            (matching
             if match(notification) else remaining).append(notification)

        notifications = matching

    session["notifications"] = remaining
    return notifications
Exemple #4
0
    def create_edit_stack(self):
        """Creates a new edit stack.

        @return: The new edit stack.
        @rtype: L{EditStack}
        """
        edit_stack = resolve(self._edit_stack_class)()
        edit_stack.id = session.get(self._session_id_key, 0)
        edit_stack.root_url = cherrypy.request.params.get("root_url")
        session[self._session_id_key] = edit_stack.id + 1
        self.__stack_map[edit_stack.id] = edit_stack

        self._remove_expired_edit_stacks()

        return edit_stack
Exemple #5
0
    def edit_stacks(self):
        """Obtains a mapping containing all the edit stacks for the current
        browsing session.

        @return: The mapping of stacks, indexed by their numerical id.
        @rtype: mapping of int => L{EditStack}
        """
        preserved_stacks = session.get(self._session_key)
        edit_stacks = {}

        if preserved_stacks:
            for id, entry in preserved_stacks.iteritems():
                stack = self._get_edit_stack(id)
                edit_stacks[id] = stack

        return DictWrapper(edit_stacks)
Exemple #6
0
    def preserve_edit_stack(self, edit_stack):
        """Stores changes to the given edit stack inside the current HTTP
        session, so it can be retrieved by later requests.
        
        @param edit_stack: The edit stack to preserve.
        @type edit_stack: L{EditStack}
        """
        preserved_stacks = session.get(self._session_key)

        if preserved_stacks is None:
            preserved_stacks = {}
            session[self._session_key] = preserved_stacks

        preserved_stacks[edit_stack.id] = (datetime.now(),
                                           self._dumps(edit_stack))

        session.save()
    def step2(self):

        credentials = session.get(SESSION_PREFIX + "credentials")

        if not credentials or credentials.access_token_expired:
            redirect(self.step_url(1))

        http_auth = credentials.authorize(httplib2.Http())
        oauth2_service = discovery.build('oauth2', 'v2', http_auth)
        user_data = oauth2_service.userinfo().get().execute()

        if self.provider.debug_mode:
            print(styled("Google user profile:", "magenta"), user_data)

        self.provider.login(user_data)
        del session[SESSION_PREFIX + "credentials"]

        redirect(self.target_url)
Exemple #8
0
    def _remove_expired_edit_stacks(self,
                                    preserved_stacks=None,
                                    current_time=None):
        if self.expiration is None:
            return

        if preserved_stacks is None:
            preserved_stacks = session.get(self._session_key)

        if preserved_stacks is None:
            return

        if current_time is None:
            current_time = datetime.now()

        for stack_id, (last_update, stack_data) in preserved_stacks.items():
            if (current_time - last_update).seconds >= self.expiration:
                del preserved_stacks[stack_id]

        session.save()
Exemple #9
0
    def step2(self):
        credentials = session.get(SESSION_PREFIX + "credentials")

        if not credentials:
            redirect(self.step_url(1))

        fields = ['name', 'email']
        query = '{}{}{}{}'.format(
            'https://graph.facebook.com', '/me?', 'fields=' + ','.join(fields),
            '&access_token=' + credentials['access_token'])

        user_data_file = urllib.request.urlopen(query).readline()
        user_data = json.loads(user_data_file)

        self.check_step2_errors(user_data)

        if self.provider.debug_mode:
            print(styled("Facebook user profile:", "magenta"), user_data)

        self.provider.login(user_data)
        del session[SESSION_PREFIX + "credentials"]

        redirect(self.target_url)
Exemple #10
0
def notify_user(message, category=None, transient=True):
    """Creates a new notification for the current user.
    
    Notifications are stored until a proper page is served to the user. It
    is up to the views to decide how these messages should be displayed.

    @param message: The message that will be shown to the user.
    @type message: unicode

    @param category: An optional free form string identifier that qualifies
        the message. Standard values include 'success' and 'error'.
    @type category: unicode

    @param transient: Indicates if the message should be hidden after a
        short lapse (True), or if it should remain visible until explicitly
        closed by the user (False).
    @type transient: bool
    """
    notifications = session.get("notifications")

    if notifications is None:
        session["notifications"] = notifications = []

    notifications.append((message, category, transient))
Exemple #11
0
 def get_user_from_session(self):
     session_user_id = session.get(self.SESSION_KEY)
     if session_user_id:
         return User.get_instance(session_user_id)
 def __init__(self, provider):
     Controller.__init__(self)
     self.provider = provider
     self.target_url = (session.get(SESSION_PREFIX + "target_url")
                        or app.website.home.get_uri())
Exemple #13
0
def get_block_clipboard_contents():
    return session.get(BLOCK_CLIPBOARD_SESSION_KEY)
def catalog_current_state_uri():
    state = session.get(SESSION_KEY) or {}
    catalog = Publishable.require_instance(
        qname="woost.extensions.ecommerce.catalog_page")
    return catalog.get_uri(parameters=state)