~launchpad-pqm/launchpad/devel

8687.15.18 by Karl Fogel
Add the copyright header block to files under lib/canonical/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
7675.61.3 by Bjorn Tillenius
Undo the commit that removed all db changes.
3
4
"""Various helper functions for using the librarian in testing.."""
5
6
__metaclass__ = type
7
__all__ = [
8
    'get_newest_librarian_file',
9
]
10
11
from storm.expr import Desc
12
from zope.component import getUtility
13
14606.2.8 by William Grant
format-imports
14
from lp.services.librarian.interfaces.client import ILibrarianClient
15
from lp.services.librarian.model import LibraryFileAlias
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
16
from lp.services.webapp.interfaces import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
17
    DEFAULT_FLAVOR,
18
    IStoreSelector,
19
    MAIN_STORE,
20
    )
7675.61.3 by Bjorn Tillenius
Undo the commit that removed all db changes.
21
22
23
def get_newest_librarian_file():
24
    """Return the file that was last stored in the librarian.
25
26
    Note that a transaction.commit() call is needed before a new file is
27
    readable from the librarian.
28
29
    :return: A file-like object of the file content.
30
    """
31
    store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
32
    alias = store.find(LibraryFileAlias).order_by(
33
        Desc(LibraryFileAlias.date_created)).first()
34
    return getUtility(ILibrarianClient).getFileByAlias(alias.id)