def authenticate(username, password): doc = None for x in db().view("user/auth", key=[username, password], include_docs=True): doc = x["doc"] break if doc is None: return macs = doc["macaddrs"] maxmacs = doc["macaddrs_max"] if macaddr != None: macs.append(macaddr) while len(macs) > maxmacs: iptables.delmac(macs.pop(0)) if len(macs) == 0: return iptables.addmac(macaddr) db().save_doc(doc) return x["id"]
def delete(uid, id_): user = db()[uid] entry = db()[id_] if entry["group"] != user["group"]: raise PermissionError() entry["deletedby"] = uid db().save_doc(entry)
def add(username, group, password): password = sha224(password).hexdigest() db().save_doc( { "type": "user", "group": group, "password": password, "username": username, "date_added": dateutils.nowtuple(), "has_changed_password": False, } )
def update(id, name, phone, email, year, rustur): doc = db()[id] for fieldname, fieldvalue in ( ("name", name), ("phone", phone), ("email", email), ("year", year), ("rustur", rustur), ): if fieldvalue == None and fieldname in doc: del doc[fieldname] else: doc[fieldname] = fieldvalue db().save(doc)
def create(username, email, macaddrs_max, password=u""): if password == u"": password = generatepassword() userid = getid(username) if userid != None: raise UserExist("User %s already exist with id %s" % (repr(username), repr(userid))) db().save_doc({ "type": "user", "username": username, "email": email, "macaddrs_max": macaddrs_max, "macaddrs": [], "password": password, })
def getgroup(uid): group = user.getgroup(uid) q = db().view("entry/groups", startkey=[group+u"\ufff0"], endkey=[group], include_docs=True, descending=True ) for x in q: doc = x["doc"] username = user.getname(doc["uid"]) deletedby = doc.get("deletedby") deletedby = deletedby and user.getname(deletedby) user.getname(doc["uid"]) debtors = doc["debtors"] debtors = dict((user.getname(x),y) for x,y in debtors) creditor = user.getname(doc["creditor"]) yield { "id": doc["_id"], "amount": doc["amount"], "creditor": creditor, "date": dateutils.fromtuple(doc["date"]), "description": doc["description"], "debtors": debtors, "username": username, "deletedby": deletedby, }
def get(uid): try: doc = db()[uid] except ResourceNotFound: raise UserDoesntExist("The user with id %s was not found" % (repr(uid),)) if doc.get("type") != "user": raise UserDoesntExist("Document id %s is not a user" % (repr(uid),)) return doc
def get(uid): try: doc = db()[uid] except ResourceNotFound: raise UserDoesntExist("The user with id %s was not found" % (repr(uid), )) if doc.get("type") != "user": raise UserDoesntExist("Document id %s is not a user" % (repr(uid), )) return doc
def getbalances(uid): group = user.getgroup(uid) q = db().view("entry/balances", startkey=[group], endkey=[group+u"\ufff0"], group=True, reduce=True, group_level=2 ) for x in q: yield user.getname(x["key"][1]), x["value"]
def update(uid, username=None, email=None, macaddrs_max=None, password=None): doc = get(uid) if username != None: if username != doc["username"]: userid = getid(username) if userid != None: raise UserExist("User %s already exist with id %s" % (repr(username), repr(userid))) doc["username"] = username if email != None: doc["email"] = email if macaddrs_max != None: doc["macaddrs_max"] = macaddrs_max if password != None: doc["password"] = password db().save_doc(doc)
def main(): views = set() for p in get_nodes(["views"]): if len(p) != 2: continue p = path.join(*p) if not path.isdir(p): continue couchdbkit.designer.push(p, db())
def add(uid, group, description, amount, creditor, debtors): date = dateutils.totuple(dateutils.now()) members = set(x[0] for x in user.getmembers(uid)) if not creditor in members: raise PermissionError() for debtor in debtors: if not debtor[0] in members: raise PermissionError() db().save_doc({ "type": "entry", "amount": amount, "creditor": creditor, "date": date, "debtors": debtors, "description": description, "group": group, "uid": uid, "deletedby":None, })
def info(id): doc = db()[id] name = doc.get("name") phone = doc.get("phone") email = doc.get("email") year = doc.get("year") rustur = doc.get("rustur") id = doc.id name = name and unicode(name) phone = phone and unicode(phone) email = email and unicode(email) year = year and unicode(year) rustur = rustur and unicode(rustur) return name, phone, email, year, rustur
def browse(page=0, perpage=10): skip = page * perpage result = db().view("rus/default", include_docs=True, skip=skip, limit=perpage, reduce=False) for row in result: doc = row.doc name = doc.get("name") phone = doc.get("phone") email = doc.get("email") year = doc.get("year") rustur = doc.get("rustur") id = doc.id name = name and unicode(name) phone = phone and unicode(phone) email = email and unicode(email) rustur = rustur and unicode(rustur) if year != None: year = unicode(year) yield id, name, phone, email, year, rustur
def getname(uid): return db()[uid]["username"]
def browse_total_pages(perpage=10): result = db().view("rus/default", reduce=True) for row in result: return ((row.value - 1) // perpage) + 1
def delete(uid): get(uid) # Make sure the user exist and display pretty error message if not db().delete_doc(uid)
def list_docs(): for x in db().view("user/auth", include_docs=True): yield x["doc"]
def save(self): if not self.changed: return self.id, self.doc["_rev"] = db().save(self.doc)
def load_session(self): try: self.doc = list(db().view("session/by_id", key=self.id, include_docs=True))[0].doc except IndexError: raise InvalidCookieException()
def save(self): if self.is_init: db().save_doc(self.doc) self.id = self.doc["_id"]
def get_doc(self, id_): for result in db().view("session/by_id", key=id_, include_docs=True): return result["doc"]
def getmacs(): for x in db().view("user/macaddr"): yield x["key"][0]
def getmembers(uid): group = getgroup(uid) q = db().view("user/group", startkey=[group], endkey=[group + u"\fff0"], include_docs=True) for x in q: yield x["id"], x["doc"]["username"]
def getname(uid): name = local.cache.get(("uid-name", uid)) if name is None: name = db()[uid]["username"] local.cache[("uid-name", uid)] = name return name
def delete(uid): get(uid ) # Make sure the user exist and display pretty error message if not db().delete_doc(uid)
def getid(username): for x in db().view("user/auth", startkey=[username, u""], endkey=[username, u"\ufff0"], limit=1): return x["id"]
def haschangedpasswd(uid): return db()[uid]["has_changed_password"]
def changepassword(uid, password): password = sha224(password).hexdigest() user = db()[uid] user["has_changed_password"] = True user["password"] = password db().save_doc(user)
def getgroup(uid): return db()[uid]["group"]
def authenticate(username, password): password = sha224(password).hexdigest() for x in db().view("user/auth", key=[username, password]): return x["id"]