def run_batch(local_name, provider_name, method_name, tee_to_stdout=True): # This will force the loading of the utils app, attaching its log # handler lest the batch logs anything that needs e-mailing. app_by_application_name('baseutils') batch = Batch.objects.get( local_name=local_name, provider_name=provider_name, method_name=method_name) batch.run(tee_to_stdout) return batch.log
def unify_users(request): user = request.user conf = app_by_application_name('auth') users = set() for identifier in user.useridentifier_set.all(): if not identifier.namespace in conf.unify_identifiers: continue identifiers = UserIdentifier.objects.filter(namespace=identifier.namespace, value=identifier.value) users |= set(i.user for i in identifiers) token_namespaces = set(t.namespace for t in user.externalservicetoken_set.all()) identifier_namespaces = set(i.namespace for i in user.useridentifier_set.all()) root_user = min(users, key=lambda u:u.date_joined) # Also need to update favourites Favourite.objects.filter(user__in=users).update(user=root_user) # Need to do the root_user first, otherwise if it's after the current user, # tokens get assigned from the current user to the root user, and then # removed from the root user, because it's not the current user. # We accomplish this by sorting the set into a list, with a custom # comparison function which gives the root_user the lowest value (and so is # first) for u in sorted(users, cmp=lambda x, y:-1 if x == root_user else 1 if y == root_user else 0): u.usersession_set.all().update(user=root_user) for token in u.externalservicetoken_set.all(): if u != user and token.namespace in token_namespaces: token.delete() else: token.user = root_user token.save() for identifier in u.useridentifier_set.all(): if u != user and identifier.namespace in identifier_namespaces: identifier.delete() else: identifier.user = root_user identifier.save() if u != root_user: u.delete()
def geolocation(request): """ Provides location-based information to the template (i.e. lat/long, google placemark data, and whether we would like to request the device's location information. """ # Use the epoch in the place of -inf; a time it has been a while since. epoch = datetime(1970, 1, 1, 0, 0, 0) # Only request a location if our current location is older than one minute # and the user isn't updating their location manually. # The one minute timeout applies to the more recent of a request and an # update. location = request.session.get("geolocation:location") requested = request.session.get("geolocation:requested", epoch) updated = request.session.get("geolocation:updated", epoch) method = request.session.get("geolocation:method") period = getattr(app_by_application_name("geolocation"), "location_request_period", 180) if max(requested, updated) + timedelta(0, period) < datetime.now() and method in ( "html5", "gears", "html5request", None, ): require_location = True request.session["geolocation:requested"] = datetime.now() else: require_location = False return { "require_location": require_location, "geolocation": { "location": request.session.get("geolocation:location"), "name": request.session.get("geolocation:name"), "accuracy": request.session.get("geolocation:accuracy"), "history": request.session.get("geolocation:history"), "favourites": request.session.get("geolocation:favourites"), "method": request.session.get("geolocation:method"), }, "location_error": request.GET.get("location_error"), }