def upcoming_birthdays(login_user): """Returns a dictionary with names, nicknames and birthdays of the login_user's contacts""" res = [] daterange_from = datetime.today() - timedelta(days=5) daterange_to = datetime.today() + timedelta(days=14) # Convert to fuzzydate and then to int (that's how it is stored in the db). # Year is least important fuzzydate_from = FuzzyDate(day=daterange_from.day, month=daterange_from.month).to_int() fuzzydate_to = FuzzyDate(day=daterange_to.day, month=daterange_to.month).to_int() if fuzzydate_from > fuzzydate_to: # end-of-year turnover fuzzydate_to_1 = 12310000 fuzzydate_from_1 = 1010000 else: fuzzydate_from_1 = fuzzydate_from fuzzydate_to_1 = fuzzydate_to logging.debug("Birthday search from: %d to %d OR %d to %d" % (fuzzydate_from,fuzzydate_to_1,fuzzydate_from_1,fuzzydate_to)) # now find the ones with birthdays in the range for con in Contact.all().filter("owned_by =", login_user): # skip companies if con.class_name() != "Person": continue if ((con.birthday.to_int() > fuzzydate_from and con.birthday.to_int() <= fuzzydate_to_1) or (con.birthday.to_int() > fuzzydate_from_1 and con.birthday.to_int() <= fuzzydate_to)): jubilee = {} # change birthday encoding from yyyy-mm-dd to dd Month jubilee['birthday'] = "%d %s" % (con.birthday.get_day(), calendar.month_name[con.birthday.get_month()]) jubilee['name'] = con.name jubilee['nickname'] = con.nickname if con.nickname else "" res.append(jubilee) return res
def get(self): login_user = get_login_user() format = self.request.get("format", "JSON") if format not in ['JSON','yaml']: logging.Critical("Unknown format for export: %s" % (format)) self.error(500) return # not logged in if not login_user: self.redirect('/login') return if self.request.get('attic',"") == 'True': attic = True else: attic = False # shall a specific dataset be exported? key = self.request.get("key", None) logging.info("export format: attic: %d user: %s admin: %d" % (attic,user.nickname(),users.is_current_user_admin())) self.response.headers['Content-Type'] = 'text/plain' # Administrator exports everything contacts = [] if users.is_current_user_admin(): if key: con = Contact.get(key) if con: contacts.append(encode_contact(con, include_attic=attic, signed_in=True, is_admin=True)) else: q_con = Contact.all() for con in q_con: contacts.append(encode_contact(con, include_attic=attic, signed_in=True, is_admin=True)) else: login_user = get_login_user(user) if key: con = Contact.get(key) if con: contacts.append(encode_contact(con, include_attic=attic, signed_in=True, is_admin=True)) else: # export everything this user can see for ckey in visible_contacts(login_user, include_attic=attic): con = Contact.get(ckey) contacts.append(encode_contact(con, include_attic=attic, me=login_user.me)) self.response.headers['Content-Disposition'] = "attachment; filename=address_export.json" if format == 'JSON': self.response.headers['Content-Type'] = "text/plain" self.response.out.write(json.dumps(contacts,indent=2)) else: self.response.headers['Content-Type'] = "text/yaml" self.response.out.write(yaml.dump(contacts,))
def get(self): if not users.is_current_user_admin(): logging.critical("UpdateIndex called by non-admin") self.error(500) return fix = True if self.request.get("fix", "False") == "True" else False # look for LoginUser with invalid Person attached logging.info("Check LoginUser") err = False for obj in LoginUser.all(): try: if not obj.me: logging.critical("LoginUser %d has no Person attached" % ((obj.key().id()))) err = True except db.ReferencePropertyResolveError: logging.critical("LoginUser %d has invalid Person reference" % ((obj.key().id()))) err = True if err: # check for dependent datasets count = Contact.all().filter("owned_by =", obj).count() logging.critical("LoginUser %d has %d dependant datasets" % (obj.key().id(),count)) if fix: obj.delete() logging.info("%d deleted" % obj.key().id()) err = False logging.info("Check Contact") err = False for obj in Contact.all(): try: if not obj.owned_by: logging.critical("Contact '%s' %d has no reference to owner" % (obj.name,obj.key().id())) err = True except db.ReferencePropertyResolveError: logging.critical("Contact '%s' %d has invalid reference to owner" % (obj.name,obj.key().id())) count = LoginUser.all().filter("me =", obj).count() if count: logging.critical("... but owner has reference!") err = True if err: # check for dependent datasets count = Take2.all().filter("contact_ref =", obj).count() logging.critical("Contact '%s' has %d dependent datasets" % (obj.name, count)) if fix: obj.delete() logging.info("%d deleted" % obj.key().id()) err = False logging.info("Check Take2") err = False for obj in Take2.all(): try: if not obj.contact_ref: logging.critical("Take2 has no reference to owner %s" % (obj.key().id())) err = True except db.ReferencePropertyResolveError: logging.critical("Take2 has invalid reference to owner %s" % (obj.key().id())) err = True if err: if fix: obj.delete() logging.info("%d deleted" % obj.key().id()) # location in address shall be set to default if obj.class_name() == 'Address' and not obj.location: logging.error("Address has null location %s. Fixed." % (obj.key().id())) obj.location=db.GeoPt(lon=0.0, lat=0.0) obj.put() err = False logging.info("Check SearchIndex") err = False for obj in SearchIndex.all(): try: if not obj.contact_ref: logging.critical("SearchIndex %d has no reference to owner" % (obj.key().id())) err = True except db.ReferencePropertyResolveError: logging.critical("SearchIndex %d has invalid reference to owner" % (obj.key().id())) err = True if err: if fix: obj.delete() logging.info("%d deleted" % obj.key().id()) err = False logging.info("Check GeoIndex") err = False for obj in GeoIndex.all(): try: if not obj.contact_ref: logging.critical("GeoIndex %d has no reference to owner" % (obj.key().id())) err = True except db.ReferencePropertyResolveError: logging.critical("GeoIndex %d has invalid reference to owner" % (obj.key().id())) err = True if err: if fix: obj.delete() logging.info("%d deleted" % obj.key().id()) err = False self.response.headers['Content-Type'] = "text/plain" self.response.out.write("/fix done.")