def get_seeded_value(self, instance): """Generate a syncronised value.""" # Django model._meta is public API -> pylint: disable=W0212 return meteor_random_id( '/collection/%s' % instance._meta, self.max_length, )
def get_meteor_id(obj_or_model, obj_pk=None): """Return an Alea ID for the given object.""" if obj_or_model is None: return None # Django model._meta is now public API -> pylint: disable=W0212 meta = obj_or_model._meta model = meta.model if model is ObjectMapping: # this doesn't make sense - raise TypeError raise TypeError("Can't map ObjectMapping instances through self.") # try getting value of AleaIdField straight from instance if possible if isinstance(obj_or_model, model): # obj_or_model is an instance, not a model. if isinstance(meta.pk, AleaIdField): return obj_or_model.pk if obj_pk is None: # fall back to primary key, but coerce as string type for lookup. obj_pk = str(obj_or_model.pk) alea_unique_fields = [ field for field in meta.local_fields if isinstance(field, AleaIdField) and field.unique ] if len(alea_unique_fields) == 1: # found an AleaIdField with unique=True, assume it's got the value. aid = alea_unique_fields[0].attname if isinstance(obj_or_model, model): val = getattr(obj_or_model, aid) elif obj_pk is None: val = None else: val = model.objects.values_list(aid, flat=True).get( pk=obj_pk, ) if val: return val if obj_pk is None: # bail out if args are (model, pk) but pk is None. return None # fallback to using AleaIdField from ObjectMapping model. content_type = ContentType.objects.get_for_model(model) try: return ObjectMapping.objects.values_list( 'meteor_id', flat=True, ).get( content_type=content_type, object_id=obj_pk, ) except ObjectDoesNotExist: return ObjectMapping.objects.create( content_type=content_type, object_id=obj_pk, meteor_id=meteor_random_id('/collection/%s' % meta), ).meteor_id
def do_login(self, user): """Login a user.""" this.user_id = user.pk this.user_ddp_id = get_meteor_id(user) # silent subscription (sans sub/nosub msg) to LoggedInUser pub this.user_sub_id = meteor_random_id() API.do_sub(this.user_sub_id, 'LoggedInUser', silent=True) self.update_subs(user.pk) user_logged_in.send( sender=user.__class__, request=this.request, user=user, )
def emit(self, record): """Emit a formatted log record via DDP.""" if getattr(this, 'subs', {}).get('Logs', False): this.send({ 'msg': ADDED, 'collection': 'logs', 'id': meteor_random_id('/collection/logs'), 'fields': { # 'name': record.name, # 'levelno': record.levelno, 'levelname': record.levelname, # 'pathname': record.pathname, # 'lineno': record.lineno, 'msg': record.msg, 'args': record.args, # 'exc_info': record.exc_info, # 'funcName': record.funcName, }, })
def get_meteor_id(obj): """Return an Alea ID for the given object.""" # Django model._meta is now public API -> pylint: disable=W0212 meta = obj._meta obj_pk = str(obj.pk) content_type = ContentType.objects.get_for_model(meta.model) try: return ObjectMapping.objects.values_list( 'meteor_id', flat=True, ).get( content_type=content_type, object_id=obj_pk, ) except ObjectDoesNotExist: return ObjectMapping.objects.create( content_type=content_type, object_id=obj_pk, meteor_id=meteor_random_id('/collection/%s' % meta), ).meteor_id
def emit(self, record): """Emit a formatted log record via DDP.""" if getattr(this, 'subs', {}).get(LOGS_NAME, False): self.format(record) this.send({ 'msg': ADDED, 'collection': LOGS_NAME, 'id': meteor_random_id('/collection/%s' % LOGS_NAME), 'fields': { attr: { # typecasting methods for specific attributes 'args': lambda args: [repr(arg) for arg in args], 'created': datetime.datetime.fromtimestamp, 'exc_info': stacklines_or_none, }.get( attr, lambda val: val # default typecasting method )(getattr(record, attr, None)) for attr in ( 'args', 'asctime', 'created', 'exc_info', 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'module', 'msecs', 'message', 'name', 'pathname', 'process', 'processName', 'relativeCreated', 'thread', 'threadName', ) }, })