~ubuntu-branches/ubuntu/trusty/desktopcouch/trusty

« back to all changes in this revision

Viewing changes to desktopcouch/contacts/view.py

  • Committer: Bazaar Package Importer
  • Author(s): Ken VanDine
  • Date: 2010-04-19 12:52:22 UTC
  • mfrom: (12.1.9 lucid)
  • Revision ID: james.westby@ubuntu.com-20100419125222-zsh9lrm15h84951j
* debian/patches/lp_522538.patch
  - Handle reconnects if the server isn't running (LP: #522538)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2010 Canonical Ltd.
2
 
#
3
 
# This file is part of desktopcouch.
4
 
#
5
 
#  desktopcouch is free software: you can redistribute it and/or modify
6
 
# it under the terms of the GNU Lesser General Public License version 3
7
 
# as published by the Free Software Foundation.
8
 
#
9
 
# desktopcouch is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU Lesser General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU Lesser General Public License
15
 
# along with desktopcouch.  If not, see <http://www.gnu.org/licenses/>.
16
 
#
17
 
# Authors: Chad Miller <chad.miller@canonical.com>
18
 
 
19
 
from record import CONTACT_RECORD_TYPE
20
 
 
21
 
__all__ = [ "find_contacts_exact", "find_contacts_starting", ]
22
 
 
23
 
 
24
 
view_map_prefixed_fields = """
25
 
    function(doc) {
26
 
        function emit_dict(prefix, dict) {
27
 
            for (var k in dict) {
28
 
                var v = dict[k];
29
 
                if (v == undefined) 
30
 
                    continue;
31
 
 
32
 
                if (v.substring) {
33
 
                    emit(prefix+k+":"+v, null);
34
 
                    switch (k) {   // Weird cases that may be useful.
35
 
                        case "birth_date": emit(prefix+k+":"+v.substring(v.indexOf("-")), null); break; // drop year
36
 
                    }
37
 
 
38
 
                } else {
39
 
                    for (subk in v)
40
 
                        emit_dict(prefix+k, v[subk])
41
 
                }
42
 
            }
43
 
        }
44
 
 
45
 
        try {
46
 
            if (doc['application_annotations']['Ubuntu One']
47
 
                    ['private_application_annotations']['deleted'])
48
 
                return;
49
 
        } catch (e) {
50
 
            // nothing
51
 
        }
52
 
 
53
 
        if (doc['record_type'] != '%(CONTACT_RECORD_TYPE)s')
54
 
        {
55
 
            return;
56
 
        }
57
 
 
58
 
        emit_dict("", doc);
59
 
    }
60
 
""" % locals()
61
 
 
62
 
 
63
 
def _cur_find_contacts(db, include_docs):
64
 
    name = "contacts_all_fields_prefixed"
65
 
    if not db.view_exists(name, "contacts"):
66
 
        db.add_view(name, view_map_prefixed_fields, None, "contacts")
67
 
 
68
 
    viewdata = db.execute_view(name, "contacts", include_docs=include_docs)
69
 
    return viewdata
70
 
 
71
 
def find_contacts_exact(db, include_docs=False, **kwargs):
72
 
    if len(kwargs) != 1:
73
 
        raise ValueError("expected exactly one keyword")
74
 
    pair = kwargs.popitem()
75
 
    return _cur_find_contacts(db, include_docs)["%s:%s" % pair]
76
 
 
77
 
def find_contacts_starting(db, include_docs=False, **kwargs):
78
 
    if len(kwargs) != 1:
79
 
        raise ValueError("expected exactly one keyword")
80
 
    pair = kwargs.popitem()
81
 
    return _cur_find_contacts(db, include_docs)[u"%s:%s" % pair : u"%s:%s\u9999\u9999\u9999\u9999" % pair]
82