def restoreUserDataAfterReactivate(app_user): models_to_restore = list() archives_to_delete = list() for avatar_archive in AvatarArchive.all().filter("user ="******"user ="******"len(models_to_restore) %s %r", len(models_to_restore), models_to_restore) logging.info("len(archives_to_delete) %s %r", len(archives_to_delete), archives_to_delete) db.put(models_to_restore) db.delete(archives_to_delete) if not AuthorizedUser.all().filter(u"user =", app_user).get(): au = AuthorizedUser() au.user = app_user au.put() if friend_map_archive: from rogerthat.bizz.friends import makeFriends # Skip friends that unsubscribed in the meantime cleanup_friend_map = False friend_profile_infos = get_profile_infos(friend_map.friends, allow_none_in_results=True) for friend_user, friendProfileInfo in zip(friend_map.friends, friend_profile_infos): if friendProfileInfo: continue logging.debug('User %s must have been deactivated in the mean while. Not executing makeFriends.', friend_user.email()) friend_map.friends.remove(friend_user) friend_map.friendDetails.remove(friend_user.email()) cleanup_friend_map = True if cleanup_friend_map: friend_map.put() # MakeFriends of remaining users in friendMap for f in friend_map.friendDetails: connected_user = users.User(f.email) makeFriends(friend_map.user, connected_user, connected_user, servicetag=None, origin=None, notify_invitee=False, notify_invitor=False, user_data=None)
def create_default_timeframes_if_nessecary(cls, service_user, solution): has_timeframes = cls.all(keys_only=True).ancestor( parent_key(service_user, solution)).get() if not has_timeframes: to_put = list() for i in xrange(0, 5): to_put.append( cls(parent=parent_key(service_user, solution), day=i, time_from=SECONDS_IN_HOUR * 9, time_until=SECONDS_IN_HOUR * 17)) db.put(to_put)
def put_all_static_content(cursor=None): qry = SolutionStaticContent.all() qry.with_cursor(cursor) models = qry.fetch(200) new_models = list() if not models: return for m in models: tmp = m.key().name().split("x", 2) coords = [int(tmp[1]), int(tmp[2]), int(tmp[0])] new_model = SolutionStaticContent(parent=parent_key( m.service_user, SOLUTION_COMMON), coords=coords, old_coords=coords, sc_type=m.sc_type, icon_label=m.icon_label, icon_name=m.icon_name, text_color=m.text_color, background_color=m.background_color, html_content=m.html_content, branding_hash=m.branding_hash, visible=True, provisioned=True, deleted=False) new_models.append(new_model) def trans(): db.put(new_models) db.delete(models) run_in_transaction(trans, True) deferred.defer(put_all_static_content, qry.cursor(), _queue=MIGRATION_QUEUE)
def _create_branding(hash_, zip_content, description, service_user, branding_type, meta_properties, pokes): if pokes and not service_user: raise BrandingValidationException('Cannot create branding with one or more poke tags without a service user') filename = get_branding_cloudstorage_path(hash_, service_user) with cloudstorage.open(filename, 'w') as f: f.write(zip_content) blobstore_filename = '/gs' + filename blobstore_key = blobstore.create_gs_key(blobstore_filename) b = Branding(key_name=hash_) b.blob_key = blobstore_key.decode('utf-8') b.description = description b.timestamp = now() b.user = service_user b.pokes = list() b.type = branding_type color_scheme = meta_properties.get('color-scheme') or Branding.DEFAULT_COLOR_SCHEME b.menu_item_color = meta_properties.get('menu-item-color') or Branding.DEFAULT_MENU_ITEM_COLORS[color_scheme] b.content_type = meta_properties.get('content-type') or Branding.CONTENT_TYPE_HTML b.orientation = meta_properties.get('orientation') or Branding.DEFAULT_ORIENTATION puts = [b] for poke_hash, unicode_tag in pokes: ptm = PokeTagMap(key_name=poke_hash, parent=parent_key(service_user)) ptm.tag = unicode_tag b.pokes.append(ptm.key()) puts.append(ptm) db.put(puts) return b
def trans(): if static_content_id: sc = SolutionStaticContent.get( SolutionStaticContent.create_key( service_user, static_content_id)) if not sc: logging.error( u"Failed to update static content with id '%s' for user %s", static_content_id, service_user) return sln_settings if sc.old_coords != coords and sc.provisioned: sc.old_coords = sc.coords else: sc = SolutionStaticContent(parent=parent_key( sln_settings.service_user, SOLUTION_COMMON)) sc.old_coords = coords sc.icon_label = icon_label sc.icon_name = icon_name sc.text_color = None sc.background_color = None sc.html_content = None sc.sc_type = SolutionStaticContent.TYPE_PDF sc.visible = visible sc.coords = coords sc.provisioned = False sc.deleted = False if branding_hash: sc.branding_hash = branding_hash sln_settings.updates_pending = True put_and_invalidate_cache(sc, sln_settings) return sln_settings
def store_template(user, png_stream, description, color, key_name=None): # Validate color try: color = parse_color(color) except ValueError: raise InvalidQRCodeBodyColorException() # Validate description if not description or not description.strip(): raise InvalidQRDescriptionException() # Validate png png_value = png_stream if png_stream: png_stream = StringIO(png_stream) reader = png.Reader(file=png_stream) img = reader.read() width, height, _, _ = img if width < 343 or height < 343: raise InvalidQRTemplateSizeException() # Store template if user: parent = parent_key(user) else: parent = None if key_name: template = QRTemplate(key=db.Key.from_path(QRTemplate.kind(), key_name, parent=parent), description=description, body_color=list(color), timestamp=now()) else: template = QRTemplate(parent=parent, description=description, body_color=list(color), timestamp=now()) if png_value: template.blob = db.Blob(png_value) template.put() return template
def create_parent_key(cls, service_identity_user): service_user, service_identity = get_service_identity_tuple( service_identity_user) service_parent_key = parent_key(service_user, SOLUTION_COMMON) return db.Key.from_path(service_parent_key.kind(), service_identity, parent=service_parent_key)
def post(self): try: user = users.get_current_user() tb = TempBlob(parent=parent_key(user)) tb.timeout = now() + 24 * 60 * 60 image = self.request.get("newAvatar") image_type = imghdr.what(None, image) try: img = images.Image(image) img.horizontal_flip() img.horizontal_flip() orig_width = img.width orig_height = img.height orig_image = image size = min(100, 100 * 4000 / max(orig_width, orig_height)) # max 4000 wide/high while len(image) > (1024 * 1024 - 100 * 1024): size -= size / 10 img = images.Image(orig_image) img.resize(orig_width * size / 100, orig_height * size / 100) image = img.execute_transforms(images.JPEG if image_type == 'jpeg' else images.PNG) tb.blob = db.Blob(image) except images.NotImageError: logging.info("Sending failure to the logged on users (%s) channel" % user.email()) self.response.out.write(broadcast_via_iframe_result(u"rogerthat.profile.avatar_upload_failed", error=u"The uploaded file is not an image!")) return except IOError, e: logging.info("Sending failure to the logged on users (%s) channel" % user.email()) self.response.out.write(broadcast_via_iframe_result(u"rogerthat.profile.avatar_upload_failed", error=e.message)) return except BadRequestError, e: logging.info("Sending failure to the logged on users (%s) channel" % user.email()) self.response.out.write(broadcast_via_iframe_result(u"rogerthat.profile.avatar_upload_failed", error=e.message)) return
def get(self): user = users.get_current_user() key = self.request.GET['key'] tb = TempBlob.get(key) azzert(tb.parent_key() == parent_key(user)) self.response.headers['Content-Type'] = "image/png" self.response.out.write(tb.blob)
def trans_create(): rogerthat_profile = get_service_or_user_profile(users.User(email)) if rogerthat_profile and isinstance(rogerthat_profile, ServiceProfile): from rogerthat.bizz.service import AppFailedToCreateUserProfileWithExistingServiceException raise AppFailedToCreateUserProfileWithExistingServiceException(email) user_profile = get_user_profile(app_user, cached=False) is_new_profile = False if not user_profile: deactivated_user_profile = get_deactivated_user_profile(app_user) if deactivated_user_profile: deferred.defer(reactivate_user_profile, deactivated_user_profile, app_user, _transactional=True) ActivationLog(timestamp=now(), email=app_user.email(), mobile=None, description="Reactivate user account by registering a paper loyalty card").put() else: is_new_profile = True avatar, image = _create_new_avatar(app_user, add_trial_overlay=False) user_profile = UserProfile(parent=parent_key(app_user), key_name=app_user.email()) user_profile.name = name user_profile.language = language user_profile.avatarId = avatar.key().id() user_profile.app_id = app_id _calculateAndSetAvatarHash(user_profile, image) pp = ProfilePointer(key=db.Key.from_path(ProfilePointer.kind(), user_code)) pp.user = app_user pp.short_url_id = short_url_id if is_new_profile: put_and_invalidate_cache(user_profile, pp, ProfilePointer.create(app_user)) else: pp.put()
def trans_create(avatar, image, share_sid_key): azzert(not get_service_profile(service_user, cached=False)) azzert(not get_default_service_identity_not_cached(service_user)) profile = ServiceProfile(parent=parent_key(service_user), key_name=service_user.email()) profile.avatarId = avatar.key().id() _calculateAndSetAvatarHash(profile, image) service_identity_user = create_service_identity_user(service_user, ServiceIdentity.DEFAULT) service_identity = ServiceIdentity(key=ServiceIdentity.keyFromUser(service_identity_user)) service_identity.inheritanceFlags = 0 service_identity.name = name service_identity.description = "%s (%s)" % (name, service_user.email()) service_identity.shareSIDKey = share_sid_key service_identity.shareEnabled = False service_identity.creationTimestamp = now() service_identity.appIds = supported_app_ids update_result = update_func(profile, service_identity) if update_func else None put_and_invalidate_cache(profile, service_identity, ProfilePointer.create(service_user), ProfileHashIndex.create(service_user)) deferred.defer(create_default_qr_templates, service_user, _transactional=True) return profile, service_identity, update_result
def get(self): service_user = users.get_current_user() azzert(get_service_profile(service_user)) # must exist message_flow_designs = get_service_message_flow_designs(service_user) result = [] for wiring in message_flow_designs: if wiring.deleted or not wiring.definition: continue wiringDefinition = json.loads(wiring.definition) for k in wiringDefinition.iterkeys(): if k == u"modules": for m in wiringDefinition[k]: if m["value"]: m["config"]["formMessageDef"] = m["value"] result.append({"name": wiring.name, "language": wiring.language, "working": json.dumps(wiringDefinition)}) if not result: wiring_id = "Sample message flow" language = "MessageFlow" result.append({"name": wiring_id, "language": language, "working": SAMPLE_MF}) message_flow_design = MessageFlowDesign(parent=parent_key(service_user), key_name=wiring_id) message_flow_design.definition = SAMPLE_MF message_flow_design.name = wiring_id message_flow_design.language = language message_flow_design.design_timestamp = now() message_flow_design.status = MessageFlowDesign.STATUS_VALID message_flow_design.js_flow_definitions = JsFlowDefinitions() message_flow_design.put() self.response.out.write(json.dumps(result))
def trans(): sln_settings = get_solution_settings(service_user) new_coords = map(int, [ static_content.position.x, static_content.position.y, static_content.position.z ]) if static_content.id is MISSING or static_content.id is None: sc = SolutionStaticContent(parent=parent_key( service_user, SOLUTION_COMMON), deleted=False) sc.old_coords = new_coords else: sc = SolutionStaticContent.get( SolutionStaticContent.create_key(service_user, static_content.id)) if sc.old_coords != new_coords and sc.provisioned: sc.old_coords = sc.coords sc.icon_label = static_content.icon_label sc.icon_name = static_content.icon_name if static_content.sc_type == SolutionStaticContent.TYPE_OWN: sc.text_color = static_content.text_color sc.background_color = static_content.background_color sc.html_content = static_content.html_content sc.branding_hash = branding_hash elif static_content.sc_type == SolutionStaticContent.TYPE_WEBSITE: sc.website = static_content.website sc.sc_type = static_content.sc_type sc.visible = static_content.visible sc.provisioned = False sc.coords = new_coords sc.put() sln_settings.updates_pending = True put_and_invalidate_cache(sc, sln_settings) return sln_settings
def get_friend_service_identity_connections_keys_of_app_user_query(app_user): app_user_email = app_user.email() azzert('/' not in app_user_email, 'no slash expected in %s' % app_user_email) qry = db.GqlQuery("SELECT __key__ FROM FriendServiceIdentityConnection" " WHERE ANCESTOR IS :ancestor AND deleted = False") qry.bind(ancestor=parent_key(app_user)) return qry
def delete_service_role(service_user, role_id, cleanup_members=False): service_role = get_service_role_by_id(service_user, role_id) if not service_role: raise RoleNotFoundException(role_id) if bool(ServiceMenuDef.all().ancestor(parent_key(service_user)).filter('roles =', role_id).count(1)): raise DeleteRoleFailedHasSMDException(role_id) if cleanup_members: service_identity_user = create_service_identity_user(service_user) role = u'%s' % role_id for srg in get_service_role_grants(service_user, role_id): app_user = create_app_user_by_email(srg.user_email, srg.app_id) def trans(): user_profile = get_user_profile(app_user, False) user_profile.revoke_role(service_identity_user, role) user_profile.put() if db.is_in_transaction(): trans() else: db.run_in_transaction(trans) else: has_grants = any(get_service_role_grants(service_user, role_id)) if has_grants: raise DeleteRoleFailedHasMembersException(role_id) db.delete(service_role) _send_service_role_updates(service_user)
def solution_calendar_broadcast(service_user, email, method, params, tag, service_identity, user_details): from solutions.common.bizz.messaging import broadcast_send sln_settings = get_solution_settings(service_user) jsondata = json.loads(params) calendar_id = long(jsondata['calendarId']) message = jsondata['message'] sc = SolutionCalendar.get_by_id( calendar_id, parent_key(service_user, sln_settings.solution)) r = SendApiCallCallbackResultTO() if sc: broadcast_type = translate(sln_settings.main_language, SOLUTION_COMMON, u'calendar-broadcast-type', name=sc.name) broadcast_send(service_user, service_identity, broadcast_type, message, broadcast_on_facebook=False, broadcast_on_twitter=False, broadcast_to_all_locations=True) r.result = u"successfully broadcast" else: r.result = u"Calendar not found. Broadcast failed." r.error = None return r
def trans(): if new_event.id: event = get_event_by_id(service_user, sln_settings.solution, new_event.id) if new_event.new_picture: event.picture_version += 1 else: event = Event( parent=parent_key(service_user, sln_settings.solution)) event.picture_version = 0 event.title = new_event.title event.place = new_event.place event.organizer = new_event.organizer event.description = new_event.description start_dates = [] for start_date in new_event.start_dates: start_dates.append(start_date.toEpoch()) start_dates_sorted, end_dates_sorted = zip( *sorted(zip(start_dates, new_event.end_dates))) startDates = list(start_dates_sorted) new_event.end_dates = list(end_dates_sorted) event.last_start_date = max(startDates) event.start_dates = start_dates event.end_dates = new_event.end_dates event.first_start_date = event.get_first_event_date() event.url = new_event.external_link event.picture = picture event.calendar_id = new_event.calendar_id sln_settings.updates_pending = True put_and_invalidate_cache(event, sln_settings) return sln_settings
def _send_broadcast_to_test_persons(broadcast): testers_to_find = list(broadcast.test_persons) si_mapped_to_testers = dict() for si in ServiceIdentity.all().ancestor(parent_key(broadcast.service_user)).run(): keys = [FriendServiceIdentityConnection.createKey(u, si.user) for u in testers_to_find] for fsic in db.get(keys): if fsic and not fsic.deleted: if not si in si_mapped_to_testers: si_mapped_to_testers[si] = list() si_mapped_to_testers[si].append(fsic.friend) testers_to_find.remove(fsic.friend) if not testers_to_find: break if not testers_to_find: break bizz_check(not testers_to_find, u"Could not find a connected service identity for %s" % [x.email() for x in testers_to_find]) mfd = MessageFlowDesign.get(broadcast.message_flow) # Make sure all end modules are connected with a flush new_xml = _check_flow_end_modules(mfd) if new_xml: mfd = CustomMessageFlowDesign() mfd.xml = new_xml for si, testers in si_mapped_to_testers.iteritems(): deferred.defer(start_flow, si.user, message_parent_key=None, flow=mfd, members=testers, check_friends=False, result_callback=False, tag=json.dumps({Broadcast.TAG_MC_BROADCAST: unicode(broadcast.key()), '%s.tag' % MC_RESERVED_TAG_PREFIX: broadcast.tag}), _transactional=db.is_in_transaction(), broadcast_type=broadcast.type_)
def trans_reset_credentials(): sc = SolutionCalendar.get_by_id( calendar_id, parent_key(service_user, sln_settings.solution)) if sc and sc.google_credentials: sc.google_credentials = None sc.put()
def trans(): sc = SolutionCalendar.get_by_id( calendar_id, parent_key(service_user, sln_settings.solution)) if not sc: return None, None, None return str( sc.key()), sc.get_google_credentials(), sc.google_calendar_ids
def trans(): ui = UserInteraction.get_by_key_name(user.email(), parent=parent_key(user)) or UserInteraction( key_name=user.email(), parent=parent_key(user)) if ui.interactions & UserInteraction.INTERACTION_WELCOME != UserInteraction.INTERACTION_WELCOME: ui.interactions |= UserInteraction.INTERACTION_WELCOME db.put_async(ui) user_profile = get_user_profile(user) app, app_translations = get_app_and_translations_by_app_id(user_profile.app_id) if app.type in (App.APP_TYPE_ENTERPRISE, App.APP_TYPE_YSAAA, App.APP_TYPE_OSA_LOYALTY): return msg = app_translations.get_translation(user_profile.language, '_welcome_msg') if app_translations else None if not msg: msg = localize(user_profile.language, '_welcome_msg', app_name=app.name, contact_email_address=app.get_contact_email_address()) sendMessage(MC_DASHBOARD, [UserMemberTO(user, Message.ALERT_FLAG_VIBRATE)], Message.FLAG_ALLOW_DISMISS, 0, None, msg, [], None, get_app_by_user(user).core_branding_hash, None, is_mfr=False)
def get_one_friend_service_identity_connection_keys_query(service_identity_user, app_user): """Returns a query that results in o FriendServiceIdentityConnection between a service identity and a user.""" service_identity_email = service_identity_user.email() qry = db.GqlQuery("SELECT __key__ FROM FriendServiceIdentityConnection" " WHERE ANCESTOR is :ancestor AND service_identity_email = :service_identity_email") qry.bind(ancestor=parent_key(app_user), service_identity_email=service_identity_email) return qry
def get_profile_for_facebook_user(access_token, app_user, update=False, language=DEFAULT_LANGUAGE, app_id=App.APP_ID_ROGERTHAT): gapi = facebook.GraphAPI(access_token) fields = ["id", "first_name", "last_name", "name", "verified", "locale", "gender", "email", "birthday", "link"] fb_profile = gapi.get_object("me", fields=','.join(fields)) logging.debug("/me graph response: %s", fb_profile) if not app_user: if "email" in fb_profile: app_user = create_app_user(users.User(fb_profile["email"]), app_id) else: raise FailedToBuildFacebookProfileException( localize(language, 'There is no e-mail address configured in your facebook account. Please use the e-mail based login.')) # TODO we should validate app.user_regex # TODO we should check if email is not used for a service account couple_facebook_id_with_profile(app_user, access_token) profile = get_user_profile(app_user) if not profile or update: if not profile: profile = FacebookUserProfile(parent=parent_key(app_user), key_name=app_user.email()) profile.app_id = app_id avatar = Avatar(user=app_user) else: avatar = get_avatar_by_id(profile.avatarId) if not avatar: avatar = Avatar(user=app_user) if fb_profile.get("name"): profile.name = fb_profile["name"] else: profile.name = get_human_user_from_app_user(app_user).email().replace("@", " at ") if profile.birthdate is None and fb_profile.get("birthday"): birthday = fb_profile["birthday"].split("/") profile.birthdate = get_epoch_from_datetime( datetime.date(int(birthday[2]), int(birthday[0]), int(birthday[1]))) if profile.gender is None and fb_profile.get("gender"): gender = fb_profile["gender"] if gender == "male": profile.gender = UserProfile.GENDER_MALE elif gender == "female": profile.gender = UserProfile.GENDER_FEMALE else: profile.gender = UserProfile.GENDER_CUSTOM avatar = _get_and_save_facebook_avatar(app_user, fb_profile["id"], profile, avatar) profile.avatarId = avatar.key().id() profile.language = language profile.profile_url = fb_profile.get("link") profile.access_token = access_token profile.version = 1 put_and_invalidate_cache(profile, ProfilePointer.create(app_user)) update_friends(profile, [u"name", u"avatar"]) update_mobiles(app_user, profile) return profile
def capied(result_f, error_f, profile, *args, **kwargs): synchronous = kwargs.get(PERFORM_CALLBACK_SYNCHRONOUS, False) if not synchronous: _validate_api_callback(result_f, error_f, profile, function, f) cc = dict() cc["method"] = function default_arg_values = f.meta['fargs'].defaults if default_arg_values: args_with_defaults = f.meta['fargs'].args[-len(default_arg_values):] effective_kwargs = dict(zip(args_with_defaults, default_arg_values)) effective_kwargs.update(kwargs) else: effective_kwargs = kwargs target_mfr = TARGET_MFR in kwargs is_solution = bool(profile.solution) if code: # code 0 is test.test if target_mfr: if code == ServiceProfile.CALLBACK_MESSAGING_RECEIVED: return elif is_solution: if not solution_supports_api_callback(profile.solution, code) and profile.callBackURI == "mobidick": logging.debug('callback %s is not implemented for solution %s', code, profile.user) return else: if not profile.callbackEnabled(code): logging.debug('callback %s is not enabled for %s', code, profile.user) return cc["params"] = {arg: serialize_value(effective_kwargs[arg], *get_type_details(type_, effective_kwargs[arg])) for arg, type_ in f.meta["kwarg_types"].iteritems()} cc["callback_name"] = kwargs.get("callback_name", None) now_ = now() callId = unicode(uuid.uuid1()) cc["id"] = callId message = json.dumps(cc) timestamp = (now_ + SERVICE_API_CALLBACK_RETRY_UNIT) * 1 if profile.enabled else -1 service_api_callback = ServiceAPICallback(parent_key(profile.user), callId, call=message, timestamp=timestamp, resultFunction=result_f and result_f.meta[u"mapping"], errorFunction=error_f and error_f.meta[u"mapping"], targetMFR=target_mfr, monitor=bool(profile.monitor), code=code, is_solution=is_solution) if 'service_identity' in kwargs: azzert(kwargs['service_identity'], 'service_identity should not be None') service_api_callback.internal_service_identity = kwargs['service_identity'] if synchronous: return submit_service_api_callback(profile, service_api_callback, effective_kwargs, function, synchronous, call_dict=cc, response_type=f.meta[u'return_type']) if not DO_NOT_SAVE_RPCCALL_OBJECTS in kwargs: service_api_callback.put() if profile.enabled or profile.solution or target_mfr or function == "test.test": submit_service_api_callback(profile, service_api_callback, effective_kwargs, function, call_dict=cc, response_type=f.meta[u'return_type']) return service_api_callback
def get_current_tracker(app_user, service_identity_user): logging.info("Getting current location tracker for %s of %s", app_user, service_identity_user) slt = ServiceLocationTracker.all().ancestor(parent_key(app_user)).filter("service_identity_user ="******"until >=", now()).filter("enabled =", True).get() if slt: logging.info("Found location tracker") else: logging.info("No location tracker found") return slt
def _6000_migrate_ancestor_models(job_key): phase = MigrateServiceJob.PHASE_6000_MIGRATE_ANCESTOR_MODELS next_phase = MigrateServiceJob.PHASE_7000_MIGRATE_SOLUTION # Validate that the job still exists job = _get_job(job_key, phase) # Do the work _log_progress(job) logging.info("1/ Migrating ServiceTranslationSet") _migrate_ancestor_models(job, parent_key(job.from_service_user, u'mc-i18n')) logging.info("2/ Migrating all other ancestor models") _migrate_ancestor_models(job, parent_key(job.from_service_user)) # Set the next phase _set_job_in_next_phase(job_key, phase, next_phase)
def get(self): user = users.get_current_user() template_id = self.request.get("template_id") logging.info(template_id) template = QRTemplate.get_by_id(int(template_id[2:], 16), parent_key(user)) self.response.headers['Content-Type'] = "image/png" logging.info(map(int, template.body_color)) url = "%s/" % get_server_settings().baseUrl self.response.out.write(qrcode(url, template.blob, map(int, template.body_color), True))
def get_service_identity_based_keys(): keys = list() si_users = list() for si in get_service_identities(service_user): keys.append(ProfilePointer.create_key(si.service_identity_user)) si_users.append(si.service_identity_user) for qr in ServiceInteractionDef.all().ancestor(parent_key(service_user)): keys.append(db.Key.from_path(ShortURL.kind(), ServiceInteractionDef.shortUrl.get_value_for_datastore(qr).id())) return keys, si_users
def trans(): capi_calls = getLocation(get_location_response_handler, get_location_response_error_handler, friend, request=request, DO_NOT_SAVE_RPCCALL_OBJECTS=True) lr = LocationRequest(parent=parent_key(friend), key_name=app_user.email(), timestamp=now()) db.put_async(lr) for capi_call in capi_calls: capi_call.lr = lr.key() capi_call.target = target db.put(capi_calls) deferred.defer(_cancel_location_request, lr, None, target, None, _countdown=17 * 60, _transactional=True, _queue=SCHEDULED_QUEUE)
def trans(): sc = SolutionCalendar(parent=parent_key(sln_settings.service_user, sln_settings.solution), name="Default", deleted=False) sc.put() sln_settings.default_calendar = sc.calendar_id put_and_invalidate_cache(sln_settings) return sc
def assemble_qrcode_strings(service_user): button_caption_set = set() qry = ServiceInteractionDef.gql("WHERE ANCESTOR IS :ancestor AND deleted = FALSE AND multilanguage = TRUE") qry.bind(ancestor=parent_key(service_user)) for sid in qry.fetch(None): button_caption_set.add(sid.description) button_caption_set.discard(None) button_caption_set.discard("") return {ServiceTranslation.SID_BUTTON: button_caption_set}
def _update_qr_code_design(sp_key): sp = db.get(sp_key) for template in QRTemplate.gql("WHERE deleted = False AND ANCESTOR is :1", parent_key(sp.service_user)): if template.description == QR_TEMPLATE_BLUE_PACIFIC or template.description == QR_TEMPLATE_BROWN_BAG or \ template.description == QR_TEMPLATE_PINK_PANTHER or template.description == QR_TEMPLATE_BLACK_HAND: template.deleted = True template.put() create_default_qr_templates(sp.service_user)
def get(self): logging.debug("GetTmpBrandingEditorLogoHandler.get") user = users.get_current_user() tb = TempBlob.get(get_branding_editor_logo_key(user)) azzert(tb.parent_key() == parent_key(user)) self.response.headers['Content-Type'] = "image/png" self.response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" self.response.headers["Pragma"] = "no-cache" self.response.headers["Expires"] = "0" self.response.out.write(tb.blob)
def trans(): service_role = get_service_role_by_name(service_user, name) if service_role: raise RoleAlreadyExistsException(name) service_role = ServiceRole(parent=parent_key(service_user)) service_role.name = name service_role.creationTime = now() service_role.type = type_ service_role.put() return service_role
def cleanup(user): """ Removes all traces in the datastore of a specific user. """ user_keys = itertools.chain(*(db.Query(c, True).filter("user =", user) for c in (\ AuthorizedUser, Settings, MobileSettings))) db.delete(user_keys) for m in get_user_active_mobiles(user): unregister_mobile(user, m) db.delete(db.GqlQuery('SELECT __key__ WHERE ANCESTOR IS :1', parent_key(user)))
def delete_appointment_weekday_timeframe(service_user, appointment_id): sln_settings = get_solution_settings(service_user) sawt = SolutionAppointmentWeekdayTimeframe.get_by_id(appointment_id, parent_key(service_user, sln_settings.solution)) if sawt: sawt.delete() sln_settings = get_solution_settings(service_user) sln_settings.updates_pending = True put_and_invalidate_cache(sln_settings) broadcast_updates_pending(sln_settings) send_message(service_user, u"solutions.common.appointment.settings.timeframe.update")
def get_friend_service_identity_connections_keys_query(service_user, app_user): """Returns a query that results in all FriendServiceIdentityConnection between a service and a user.""" email = service_user.email() qry = db.GqlQuery("SELECT __key__ FROM FriendServiceIdentityConnection" " WHERE ANCESTOR is :ancestor" " AND service_identity_email >= :from_service_identity_email" " AND service_identity_email < :to_service_identity_email") qry.bind(ancestor=parent_key(app_user), from_service_identity_email=email + '/', to_service_identity_email=email + u"/\ufffd") return qry
def get_profile_for_google_user(email, language, name): user = users.User(email) user_profile = get_user_profile(user) if not user_profile: user_profile = UserProfile(parent=parent_key(user), key_name=user.email()) user_profile.name = name if name else user.email() user_profile.language = language user_profile.version = 1 put_and_invalidate_cache(user_profile, ProfilePointer.create(user)) update_friends(user_profile) return user_profile
def trans(): key_name = fb_friend_user.email() parent = parent_key(new_user) invite = FacebookDiscoveryInvite.get_by_key_name(key_name, parent) if invite: return db.put_async(FacebookDiscoveryInvite(key_name=key_name, parent=parent)) friend_map = get_friends_map(new_user) if fb_friend_user in friend_map.friends: return deferred.defer(_send_message_to_inform_user_about_a_new_join_step_2, fb_friend_user, new_user, _transactional=True)
def _delete_service_models(parent_service_user, service_user): key = parent_key(service_user) def trans(): keys = db.GqlQuery("SELECT __key__ WHERE ANCESTOR IS KEY('%s')" % str(key)).fetch(1000) if keys: db.delete(keys) return True return False while db.run_in_transaction(trans): pass _decrease_and_verify(parent_service_user, service_user)
def trans(): key_name = "log_analysis_instance" parent = parent_key(users.User(u"*****@*****.**")) la = LogAnalysis.get_by_key_name(key_name, parent) if not la: la = LogAnalysis(key_name=key_name, parent=parent, analyzed_until=now() - 10 * 60) start = la.analyzed_until end = now() la.analyzed_until = end db.put_async(la) deferred.defer(_analyze, start, end, _transactional=True)
def _worker(sls_key): def trans(): return db.get(sls_key) sls = db.run_in_transaction(trans) to_put = [] for visit in SolutionLoyaltyVisitStamps.all().ancestor(parent_key(sls.service_user, SOLUTION_COMMON)): visit.x_stamps = sls.x_stamps to_put.append(visit) if to_put: db.put(to_put)
def get_service_interaction_defs(service_user, identifier, cursor, include_deleted=False): if identifier is None: if include_deleted: qry_string = "WHERE ANCESTOR IS :ancestor ORDER BY timestamp DESC" else: qry_string = "WHERE ANCESTOR IS :ancestor AND deleted = FALSE ORDER BY timestamp DESC" qry = ServiceInteractionDef.gql(qry_string) qry.bind(ancestor=parent_key(service_user)) else: if include_deleted: qry_string = "WHERE ANCESTOR IS :ancestor AND service_identity = :identifier ORDER BY timestamp DESC" else: qry_string = "WHERE ANCESTOR IS :ancestor AND service_identity = :identifier AND deleted = FALSE ORDER BY timestamp DESC" qry = ServiceInteractionDef.gql(qry_string) qry.bind(identifier=identifier, ancestor=parent_key(service_user)) qry.with_cursor(cursor if cursor else None, None) defs = qry.fetch(10) return { "defs": defs, "cursor": qry.cursor() if len(defs) > 0 else "" }
def get_solution_calendars(service_user, solution, filter_broadcast_disabled=False): if filter_broadcast_disabled: qry = SolutionCalendar.gql( "WHERE ANCESTOR IS :ancestor AND deleted=False AND broadcast_enabled=True" ) else: qry = SolutionCalendar.gql( "WHERE ANCESTOR IS :ancestor AND deleted=False") qry.bind(ancestor=parent_key(service_user, solution)) return generator(qry.run())
def _set_deleted_status(sln_settings): to_put = [] for sc in SolutionOrder.all().ancestor(parent_key(sln_settings.service_user, SOLUTION_COMMON)): if not sc.deleted: if sc.status == SolutionOrder.STATUS_COMPLETED: sc.deleted = True else: sc.deleted = False to_put.append(sc) logging.info("updated '%s' order for user %s", len(to_put), sln_settings.service_user) db.put(to_put)
def get_solution_calendars_for_user(service_user, solution, app_user, filter_deleted=True): calendars = [] for calendar in db.get([ c.calendar_key for c in SolutionCalendarAdmin.all().ancestor( parent_key(service_user, solution)).filter( "app_user =", app_user) ]): if not (filter_deleted and calendar.deleted): calendars.append(calendar) return calendars
def _gather_events_for_customer(customer_key, cap_key, organization_type): customer = Customer.get(customer_key) if not customer.service_email: logging.debug('This customer has no service yet: %s', db.to_dict(customer)) return if cap_key.parent().name() == customer.service_email: # do not gather own events return sln_settings = get_solution_settings(customer.service_user) if SolutionModule.AGENDA not in sln_settings.modules: return if sln_settings.default_calendar is None: logging.error( 'This customer has no default calendar!\n\nSolutionSettings: %s\n\nCustomer: %s', db.to_dict(sln_settings), db.to_dict(customer), _suppress=False) return sc = SolutionCalendar.get_by_id( sln_settings.default_calendar, parent_key(customer.service_user, sln_settings.solution)) if not sc: return event_items = [] for event in sc.events: event_item = EventItemTO.fromEventItemObject(event) event_item.calendar_id = organization_type event_items.append(event_item) if event_items: new_events = serialize_complex_value(event_items, EventItemTO, True) gather_events_key = u"%s" % organization_type def trans(): cap = CityAppProfile.get(cap_key) stream = cap.gather_events.get(gather_events_key) if stream: json_list = json.load(stream) else: json_list = list() json_list.extend(new_events) with closing(StringIO()) as stream: json.dump(json_list, stream) cap.gather_events[gather_events_key] = stream cap.put() db.run_in_transaction(trans) sln_settings.put_identity_pending = True sln_settings.put()
def trans(): beacon = Beacon.all().ancestor(parent_key(si.service_user)).get() if beacon: return beacon.uuid, beacon.name app = App.get(App.create_key(si.app_id)) app.beacon_last_minor = app.beacon_last_minor + 1 name = "%s|%s" % (app.beacon_major, app.beacon_last_minor) logging.info("add_new_beacon: %s", name) if not add_new_beacon(app.beacon_uuid, name, u'Autoconnect', si.service_identity_user): raise Exception("Beacon already exists") app.put() return app.beacon_uuid, name
def create_calendar_admin(calendar_id, app_user, service_user, solution): """Create calendar admin and assign create events role.""" from solutions.common.bizz.messaging import POKE_TAG_NEW_EVENT sc = SolutionCalendar.get_by_id(calendar_id, parent_key(service_user, solution)) if sc: key = SolutionCalendarAdmin.createKey(app_user, sc.key()) sca = db.get(key) if not sca: sca = SolutionCalendarAdmin(key=key) sca.app_user = app_user db.put(sca) assign_app_user_role(app_user, POKE_TAG_NEW_EVENT) return sca
def put_order_weekday_timeframe(service_user, timeframe_id, day, time_from, time_until): sln_settings = get_solution_settings(service_user) if time_from == time_until: raise BusinessException( common_translate(sln_settings.main_language, SOLUTION_COMMON, 'time-start-end-equal')) if time_from >= time_until: raise BusinessException( common_translate(sln_settings.main_language, SOLUTION_COMMON, 'time-start-end-smaller')) sln_settings = get_solution_settings(service_user) if timeframe_id: sawt = SolutionOrderWeekdayTimeframe.get_by_id( timeframe_id, parent_key(service_user, sln_settings.solution)) if not sawt: # Already deleted before channel update went through send_message(service_user, u"solutions.common.order.settings.timeframe.update") return sawt.day = day sawt.time_from = time_from sawt.time_until = time_until else: sawt = SolutionOrderWeekdayTimeframe.get_or_create( parent_key(service_user, sln_settings.solution), day, time_from, time_until) sawt.put() sln_settings = get_solution_settings(service_user) sln_settings.updates_pending = True put_and_invalidate_cache(sln_settings) broadcast_updates_pending(sln_settings) send_message(service_user, u"solutions.common.order.settings.timeframe.update")
def _process_event_reminder(reminder_key): reminder = EventReminder.get(reminder_key) service_user = reminder.service_user settings = get_solution_settings(service_user) event = Event.get_by_id(reminder.event_id, parent_key(service_user, settings.solution)) if event and reminder.event_start_epoch in event.start_dates: now_ = now() if (now_ + reminder.remind_before) > (reminder.event_start_epoch + timezone_offset(settings.timezone)): if event.place: place = "\n@ " + event.place + "\n" else: place = "" dt = datetime.datetime.fromtimestamp(reminder.event_start_epoch) language = settings.main_language or DEFAULT_LANGUAGE when = "%s, %s" % (format_date(dt, format='full', locale=language), format_time(dt, format='short', locale=language)) reminderMessage = "Reminder:\n\nTitle:\n%s\n\nWhen:\n%s\n%s\nDescription:\n%s" % ( event.title, when, place, event.description) main_branding = get_solution_main_branding(service_user) human_user = get_human_user_from_app_user(reminder.human_user) members = list() members.append(human_user.email()) users.set_user(reminder.service_user) try: messaging.send(parent_key=None, parent_message_key=None, message=reminderMessage, answers=[], flags=Message.FLAG_ALLOW_DISMISS, members=members, branding=main_branding.branding_key, tag=None, service_identity=reminder.service_identity) finally: users.clear_user() reminder.status = EventReminder.STATUS_REMINDED reminder.put() else: reminder.status = EventReminder.STATUS_REMINDED reminder.put()
def put_appointment_weekday_timeframe(service_user, appointment_id, day, time_from, time_until): sln_settings = get_solution_settings(service_user) if time_from == time_until: raise BusinessException(common_translate(sln_settings.main_language, SOLUTION_COMMON, 'time-start-end-equal')) if time_from >= time_until: raise BusinessException(common_translate(sln_settings.main_language, SOLUTION_COMMON, 'time-start-end-smaller')) sln_settings = get_solution_settings(service_user) if appointment_id: sawt = SolutionAppointmentWeekdayTimeframe.get_by_id(appointment_id, parent_key(service_user, sln_settings.solution)) sawt.day = day sawt.time_from = time_from sawt.time_until = time_until else: sawt = SolutionAppointmentWeekdayTimeframe.get_or_create(parent_key(service_user, sln_settings.solution), day, time_from, time_until) sawt.put() sln_settings = get_solution_settings(service_user) sln_settings.updates_pending = True put_and_invalidate_cache(sln_settings) broadcast_updates_pending(sln_settings) send_message(service_user, u"solutions.common.appointment.settings.timeframe.update")
def _put_default_event(service_user, translate, solution): event = Event(parent=parent_key(service_user, solution)) now = time.mktime(date.timetuple(date.today())) event.title = u"Title" event.place = u"Place" event.description = u"Description" event.last_start_date = long(now + 7 * 24 * 60 * 60 + 20 * 60 * 60) event.start_dates = [event.last_start_date] event.end_dates = [long(4 * 60 * 60)] event.first_start_date = event.get_first_event_date() event.put() event2 = Event(parent=parent_key(service_user, solution)) event2.title = u"Title2" event2.place = u"Place2" event2.description = u"Description2" event2.last_start_date = long(now + 14 * 24 * 60 * 60 + 20 * 60 * 60) event2.start_dates = [event2.last_start_date] event2.end_dates = [long(4 * 60 * 60)] event2.first_start_date = event2.get_first_event_date() event2.put() return [event, event2]
def get_admins_of_solution_calendars(service_user, solution): parent = parent_key(service_user, solution) deleted_calendar_keys = [ c for c in SolutionCalendar.all( keys_only=True).ancestor(parent).filter("deleted =", True) ] calendar_admins = SolutionCalendarAdmin.all().ancestor(parent) admins = [] for admin in calendar_admins: if admin.calendar_key not in deleted_calendar_keys: admins.append(admin) return admins
def events_with_cursor(self, cursor, count): qry = Event.all().with_cursor(cursor).ancestor( parent_key(self.service_user, self.solution)).filter( "calendar_id =", self.calendar_id).filter("deleted =", False).order("first_start_date") events = qry.fetch(count) cursor_ = qry.cursor() has_more = False if len(events) != 0: qry.with_cursor(cursor_) if len(qry.fetch(1)) > 0: has_more = True return unicode(cursor_), events, has_more
def trans(): sgc_key = SolutionGoogleCredentials.createKey(user_info['id']) sgc = SolutionGoogleCredentials(key=sgc_key) sgc.email = user_info.get('email', None) sgc.name = user_info.get('name', None) sgc.gender = user_info.get('gender', None) sgc.picture = user_info.get('picture', None) sgc.credentials = credentials sgc.put( ) # we still need to save this because other calendars can be using the same credential sc = SolutionCalendar.get_by_id( calendar_id, parent_key(service_user, sln_settings.solution)) if not sc: return sc.google_credentials = user_info['id'] sc.put()
def trans(): scgs = SolutionCalendarGoogleSync.get_by_key_name( calendar.service_user.email()) if not scgs: scgs = SolutionCalendarGoogleSync( key_name=calendar.service_user.email()) scgs.google_calendar_keys = map( str, SolutionCalendar.all(keys_only=True).ancestor( parent_key(calendar.service_user, calendar.solution)).filter( "google_sync_events =", True)) scgs.put() deferred.defer(update_events_from_google, calendar.service_user, calendar.calendar_id, _transactional=True)
def delete_calendar_admin(calendar_id, app_user, service_user, solution): """Delete a calendar admin and revoke create events role.""" from solutions.common.bizz.messaging import POKE_TAG_NEW_EVENT sc = SolutionCalendar.get_by_id(calendar_id, parent_key(service_user, solution)) if sc: key = SolutionCalendarAdmin.createKey(app_user, sc.key()) sca = db.get(key) if sca: db.delete(sca) # check if the user is an admin of any other calendar # if not, just revoke the role other_calendars = get_solution_calendar_ids_for_user( service_user, solution, app_user) if not other_calendars: revoke_app_user_role(app_user, POKE_TAG_NEW_EVENT)
def trans(): sandwich_settings = SandwichSettings.get_settings( sln_settings.service_user, sln_settings.solution) solution_datetime = datetime.now(pytz.timezone(sln_settings.timezone)) if not sandwich_settings.can_broadcast_for_sandwiches_on( solution_datetime): logging.info("No email_reminders today for %s", sln_settings.service_user.email()) return last_broad_cast_day = SandwichLastBroadcastDay.get_last_broadcast_day( sln_settings.service_user, sln_settings.solution) if last_broad_cast_day and last_broad_cast_day.year == solution_datetime.year and \ last_broad_cast_day.month == solution_datetime.month and last_broad_cast_day.day == solution_datetime.day: logging.info("Sandwich broadcast already sent today for %s", sln_settings.service_user.email()) return seconds_before_broadcast = sandwich_settings.remind_at - ( (solution_datetime.hour * 3600) + (solution_datetime.minute * 60) + (solution_datetime.second)) if seconds_before_broadcast < 0: seconds_before_broadcast = 0 if not last_broad_cast_day: last_broad_cast_day = SandwichLastBroadcastDay( key_name=sln_settings.service_user.email(), parent=parent_key(sln_settings.service_user, sln_settings.solution)) last_broad_cast_day.year = solution_datetime.year last_broad_cast_day.month = solution_datetime.month last_broad_cast_day.day = solution_datetime.day last_broad_cast_day.put() deferred.defer(_broadcast, sln_settings_key, sandwich_settings.key(), _transactional=True, _countdown=seconds_before_broadcast, _queue=SCHEDULED_QUEUE)
def _populate_uit_events(sln_settings, uitdatabank_secret, uitdatabank_key, external_id, uitdatabank_actors, changed_since): logging.debug("process event with id: %s", external_id) detail_success, detail_result = _get_uitdatabank_events_detail( uitdatabank_secret, uitdatabank_key, external_id) if not detail_success: logging.warn("Failed to get detail for cdbid: %s\n%s" % (external_id, detail_result)) return None if uitdatabank_secret and changed_since: if detail_result["lastupdated"] < (changed_since * 100): return None if DEBUG: logging.warn("detail result: %s", detail_result) event_parent_key = parent_key(sln_settings.service_user, sln_settings.solution) event = Event.all().ancestor(event_parent_key).filter( "source =", Event.SOURCE_UITDATABANK_BE).filter("external_id =", external_id).get() if not event: event = Event(parent=event_parent_key, source=Event.SOURCE_UITDATABANK_BE, external_id=external_id) event.calendar_id = sln_settings.default_calendar events = [event] uitdatabank_created_by = detail_result.get("createdby", None) logging.debug("uitdatabank_created_by: %s", uitdatabank_created_by) uitdatabank_lastupdated_by = detail_result.get("lastupdatedby", None) logging.debug("uitdatabank_lastupdated_by: %s", uitdatabank_lastupdated_by) if uitdatabank_created_by or uitdatabank_lastupdated_by: if uitdatabank_created_by and uitdatabank_created_by not in uitdatabank_actors: uitdatabank_actors[uitdatabank_created_by] = [] if uitdatabank_lastupdated_by and uitdatabank_lastupdated_by not in uitdatabank_actors: uitdatabank_actors[uitdatabank_lastupdated_by] = [] origanizer_settings = [] if uitdatabank_created_by: origanizer_settings.extend( uitdatabank_actors[uitdatabank_created_by]) if uitdatabank_lastupdated_by and uitdatabank_created_by != uitdatabank_lastupdated_by: origanizer_settings.extend( uitdatabank_actors[uitdatabank_lastupdated_by]) logging.debug("len(origanizer_settings): %s", len(origanizer_settings)) for organizer_sln_settings in origanizer_settings: organizer_event_parent_key = parent_key( organizer_sln_settings.service_user, organizer_sln_settings.solution) organizer_event = Event.all().ancestor( organizer_event_parent_key).filter( "source =", Event.SOURCE_UITDATABANK_BE).filter( "external_id =", external_id).get() if not organizer_event: organizer_event = Event(parent=organizer_event_parent_key, source=Event.SOURCE_UITDATABANK_BE, external_id=external_id) organizer_event.calendar_id = organizer_sln_settings.default_calendar events.append(organizer_event) r_event_detail = detail_result["eventdetails"]["eventdetail"] if not r_event_detail: logging.warn('Missing eventdetail') return None if isinstance(r_event_detail, list): for x in r_event_detail: if x['lang'] == sln_settings.main_language: r_event_detail = x break else: r_event_detail = r_event_detail[0] event_title = r_event_detail["title"] event_description = r_event_detail.get( "shortdescription", r_event_detail.get("longdescription", u"")) if "physical" in detail_result["location"]["address"]: location = detail_result["location"]["address"]["physical"] if location.get("street", None): if uitdatabank_secret: event_place = "%s %s, %s %s" % ( location["street"]["value"], location.get("housenr", ""), location["zipcode"], location["city"]["value"]) else: event_place = "%s %s, %s %s" % ( location["street"], location.get( "housenr", ""), location["zipcode"], location["city"]) else: if uitdatabank_secret: event_place = "%s %s" % (location["zipcode"], location["city"]["value"]) else: event_place = "%s %s" % (location["zipcode"], location["city"]) else: event_place = detail_result["location"]["address"]["virtual"]["title"] r_organizer = detail_result.get('organiser') for k in ('actor', 'actordetails', 'actordetail', 'title'): if not r_organizer: break r_organizer = r_organizer.get(k) event_organizer = r_organizer r_timestamps = detail_result["calendar"].get("timestamps") if not r_timestamps: logging.debug( "skipping event because we could not determine starttime for: \n%s", pprint.pformat(detail_result)) return None event_start_dates, event_end_dates = get_event_start_and_end_dates( r_timestamps, v2=uitdatabank_secret) if not event_start_dates: logging.info( "Skipping event because it had no starttime (list) for:\n%s", pprint.pformat(detail_result)) return None for event in events: event.title = event_title event.description = event_description event.place = event_place event.organize = event_organizer event.last_start_date = max(event_start_dates) event.start_dates = event_start_dates event.end_dates = event_end_dates event.first_start_date = event.get_first_event_date() return events