1
# Copyright 2010 Canonical Ltd.
3
# This file is part of desktopcouch.
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.
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.
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/>.
17
# Authors: Chad Miller <chad.miller@canonical.com>
19
from record import CONTACT_RECORD_TYPE
21
__all__ = [ "find_contacts_exact", "find_contacts_starting", ]
24
view_map_prefixed_fields = """
26
function emit_dict(prefix, dict) {
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
40
emit_dict(prefix+k, v[subk])
46
if (doc['application_annotations']['Ubuntu One']
47
['private_application_annotations']['deleted'])
53
if (doc['record_type'] != '%(CONTACT_RECORD_TYPE)s')
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")
68
viewdata = db.execute_view(name, "contacts", include_docs=include_docs)
71
def find_contacts_exact(db, include_docs=False, **kwargs):
73
raise ValueError("expected exactly one keyword")
74
pair = kwargs.popitem()
75
return _cur_find_contacts(db, include_docs)["%s:%s" % pair]
77
def find_contacts_starting(db, include_docs=False, **kwargs):
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]