from werkzeug.contrib.atom import AtomFeed


def build_query(q='*', start=0, size=10, sort=None):
    query = {
        'query': build_query_string(q),
        'from': start,
        'size': size,
    }

    if sort:
        query['sort'] = [
            {
                sort: 'desc'
            }
        ]

    return query


def build_query_string(q):
    return {
        'query_string': {
            'default_field': '_all',
            'query': q,
            'analyze_wildcard': True,
            'lenient': True  # TODO, may not want to do this
        }
    }


def create_atom_feed(name, data, query, size, start, url, to_atom):
    if query == '*':
        title_query = 'All'
    else:
        title_query = query

    title = '{name}: Atom Feed for query: "{title_query}"'.format(name=name, title_query=title_query)
    author = 'COS'

    prev_page = (start / size)

    if prev_page == 0:
        prev_page = (start / size) + 1

    links = [
        {'href': url, 'rel': 'first'},
        {'href': '{url}?page={page}'.format(url=url, page=(start / size) + 2), 'rel': 'next'},
        {'href': '{url}?page={page}'.format(url=url, page=prev_page), 'rel': 'previous'}
    ]

    feed = AtomFeed(
        title=title,
        feed_url=url,
        author=author,
        links=links
    )

    for doc in data:
        feed.add(**to_atom(doc))

    return feed

@@ -849,103 +849,8 @@
 S'%0A%0A
-    prev_page = (start / size)%0A%0A    if prev_page == 0:%0A        prev_page = (start / size) + 1%0A%0A
     
@@ -1055,17 +1055,22 @@
 age=
-prev_page
+(start / size)
 ), '
@@ -1090,24 +1090,82 @@
 us'%7D%0A    %5D%0A%0A
+    links = links%5B:-1%5D if (start / size) == 0 else links%0A%0A
     feed = A

Remove previous link if you're on the first page