def unremove_settlement(s_id): """ Un-sets 'removed' on the settlement and all of its survivors. We have to initialize the settlement to do this and we have to initialize whatever survivors need to be un-removed as well, so this one is here (and not exposed via API) because it's never going to be efficient/pretty. """ S = settlements.Settlement(_id=s_id) if not S.settlement.get("removed", False): print( " Settlement '%s' [%s] has not been removed!\n Aborting unremove request...\n" % (S.settlement['name'], s_id)) return False proceed = raw_input( " \x1b[1;33;40m Unremove this settlement (including all removed survivors)? [N/y] \x1b[0m " ) if len(proceed) > 0 and proceed.upper()[0] == "Y": proceed = True else: proceed = False if not proceed: print("\n Aborting unremove request...\n") return False print("\n Unremoving settlement...") S.unremove(unremove_survivors=True) time.sleep(1) print " done!\n" time.sleep(1) dump_settlement_to_cli(s_id)
def latest_settlement(self): """ Get the latest settlement and punch it up with some additional info, since JSON consumers don't have MDB access and can't get it otherwise. """ # s = self.get_eligible_documents(collection="settlements", limit=1) try: s = utils.mdb.settlements.find( {"name": { "$nin": self.ineligible_names }}, sort=[("created_on", -1)])[0] except IndexError: self.logger.error( "No settlements in mdb match the 'latest_settlement' criteria! Returning None..." ) return None S = settlements_models.Settlement(_id=s["_id"]) s["campaign"] = S.get_campaign("name") s["expansions"] = S.get_expansions("pretty") s["player_count"] = S.get_players("count") s["age"] = utils.get_time_elapsed_since(s["created_on"], 'age') for k in [ 'timeline', ]: if k in s.keys(): s[k] = "REDACTED" return s
def get_settlement_data(): """ Returns JSON about recently updated settlements. Also serializes those settlements and gets their event_log. """ recent_cutoff = datetime.now() - timedelta( hours=settings.get("application", "recent_user_horizon")) s_info = [] ids = utils.mdb.settlements.find({ 'last_accessed': { '$gte': recent_cutoff } }).distinct('_id') sorting_hat = {} for s_id in ids: last_updated = utils.mdb.settlement_events.find({ 'settlement_id': s_id }).limit(1).sort("created_on", -1)[0]['created_on'] sorting_hat[last_updated] = s_id sorted_ids = [] for timestamp in sorted(sorting_hat.keys(), reverse=True): sorted_ids.append(sorting_hat[timestamp]) for s_id in sorted_ids: S = settlements.Settlement(_id=s_id, normalize_on_init=False) s_dict = copy(S.serialize('dashboard')) s_info.append(s_dict) return "[" + ",".join(s_info) + "]"
def initialize_settlement_timeline(s_id): """ Initializes a settlement's timeline. Cannot be undone. """ S = settlements.Settlement(_id=s_id) if check_with_user("Initialize this settlement's timeline?"): S.initialize_timeline() print("\n Timeline initialized!\n") else: print("\n Aborted.\n")
def get_user_asset(collection=None, asset_id=None): """ Tries to return an initialized asset from one of our collections. Returns a (bad) HTTP response if it cannot. """ R = badResponse() try: if collection == "settlement": return settlements.Settlement(_id=asset_id) elif collection == "survivor": return survivors.Survivor(_id=asset_id) elif collection == "user": return users.User(_id=asset_id) else: return R except Exception as e: return R.send_bad_response(e)
def new_user_asset(asset_type=None): """ Hands off a new asset creation request and returns the result of the request. Like all brokerage methods, this method always returns an HTTP response. The idea is to call this in request-processing workflow when you know that you've got an asset creation request. This brokerage method expects a few things: 1.) you've added a logger to the request 2.) you've also added a models.users.User object to the request 3.) you're passing in JSON params that can be processed when fulfilling your request If you are NOT doing all of that, do NOT pass off a request to this method, unless you want to throw a 500 back at the user. """ # logger.debug("%s is requesting a new '%s' asset!" % (request.User, asset_type)) if asset_type == "settlement": S = settlements.Settlement() return S.serialize() elif asset_type == "survivor": S = survivors.Survivor() return S.serialize() elif asset_type == "survivors": output = survivors.add_many_survivors(dict(request.get_json())) return Response(response=json.dumps(output, default=json_util.default), status=200, mimetype="application/json") else: return Response( response= "Creating '%s' user assets via request_broker() is not supported!" % asset_type, status=422, mimetype="text/plain", ) return utils.http_400