def post(self, author, plando): user = users.get_current_user() if user: dispname = user.email().partition("@")[0] if dispname == author: id = author + ":" + plando seedLines = self.request.POST["seed"] desc = self.request.POST["desc"] old_name = paramVal(self, "old_name") if old_name: old_seed = Seed.get_by_id("%s:%s" % (author, old_name)) else: old_seed = Seed.get_by_id("%s:%s" % (author, plando)) seed = Seed.from_plando(seedLines.split("!"), author, plando, desc) if old_seed: seed.hidden = old_seed.hidden res = seed.put() if res and old_name and old_name != plando: if not old_seed: log.error( "couldn't find old seed when trying to rename!") else: old_seed.key.delete() self.response.headers['Content-Type'] = 'text/plain' self.response.status = 200 self.response.out.write(res) else: log.error( "Auth failed, logged in as %s, trying to edit %s's seed" % (dispname, author)) self.response.status = 401 else: log.error("no auth D:") self.response.status = 401
def seed(request, topic_id): rspd = {} request_user = request.user if not request_user.is_authenticated(): rspd['msg'] = u'要收藏,先登录!' res = json.dumps(rspd) return HttpResponse(res) topic = get_object_or_404(Topic, id=topic_id) if topic.posted_by == request_user: rspd['msg'] = u'没必要收藏自己的主题哦!' res = json.dumps(rspd) return HttpResponse(res) seed_user = Seed.objects.filter(topic=topic, seed_by=request_user).select_related() if seed_user: rspd['msg'] = u'你已经收藏了,不能重复收藏!' res = json.dumps(rspd) return HttpResponse(res) else: seed = Seed(topic=topic, seed_by=request_user) seed.save() topic.num_seeds += 1 topic.save() rspd['msg'] = u'收藏成功!' res = json.dumps(rspd) return HttpResponse(res)
def get(self, author, plando): owner = False user = users.get_current_user() if user: dispname = user.email().partition("@")[0] owner = dispname == author id = author + ":" + plando seed = Seed.get_by_id(id) if seed: gid = paramVal(self, "gid") pid = paramVal(self, "pid") syncFlag = "Sync%s.%s" % (gid, pid) self.response.status = 200 self.response.headers[ 'Content-Type'] = 'application/x-gzip' if not debug else 'text/plain' self.response.headers[ 'Content-Disposition'] = 'attachment; filename=randomizer.dat' if not debug else "" seedlines = seed.to_lines(player=int(pid), extraFlags=[syncFlag]) rand = Random() rand.seed(seed.name) flagline = seedlines.pop(0) rand.shuffle(seedlines) seedlines.insert(0, flagline) self.response.out.write("\n".join(seedlines)) else: self.response.status = 404 self.response.headers['Content-Type'] = 'text/plain' self.response.out.write("seed not found")
def get(self, author, plando): path = os.path.join(os.path.dirname(__file__), 'map/build/index.html') template_values = { 'app': "plandoBuilder", 'title': "Plandomizer Editor " + PLANDO_VER, 'seed_name': plando } owner = False user = users.get_current_user() if user: dispname = user.email().partition("@")[0] owner = dispname == author id = author + ":" + plando if not owner: self.redirect('/login') else: seed = Seed.get_by_id(id) template_values['user'] = dispname template_values['authed'] = "True" if seed: template_values['seed_desc'] = seed.description template_values['seed_hidden'] = seed.hidden or False template_values['seed_data'] = "\n".join( seed.to_plando_lines()) self.response.out.write(template.render(path, template_values))
def get(self, author, plando): user = users.get_current_user() dispname = "Guest" if user: dispname = user.email().partition("@")[0] id = author + ":" + plando seed = Seed.get_by_id(id) if seed: hidden = seed.hidden or False if not (hidden and dispname != author): self.response.status = 200 self.response.headers['Content-Type'] = 'text/html' path = os.path.join(os.path.dirname(__file__), 'map/build/index.html') template_values = { 'app': "seedDisplay", 'title': "%s by %s" % (plando, author), 'players': seed.players, 'seed_data': seed.to_lines()[0], 'seed_name': plando, 'author': author, 'authed': True, 'seed_desc': seed.description, 'user': dispname, 'game_id': Game.get_open_gid() } if hidden: template_values['seed_hidden'] = True self.response.out.write(template.render(path, template_values)) return self.response.status = 404 self.response.headers['Content-Type'] = 'text/plain' self.response.out.write("seed not found")
def get(self, author, old_name, new_name): user = users.get_current_user() if user: dispname = user.email().partition("@")[0] if dispname == author: old_seed = Seed.get_by_id("%s:%s" % (author, old_name)) if not old_seed: log.error("couldn't find old seed when trying to rename!") self.response.status = 404 return new_seed = clone_entity(old_seed, id="%s:%s" % (author, new_name), name=new_name) if new_seed.put(): if not paramFlag(self, "cp"): old_seed.key.delete() self.redirect('/plando/%s/%s' % (author, new_name)) else: log.error("Failed to rename seed") self.response.status = 500 else: log.error( "Auth failed, logged in as %s, trying to edit %s's seed" % (dispname, author)) self.response.status = 401 else: log.error("no auth D:") self.response.status = 401
def get_results_for_seeds(seeds, max_depth): # Create a new fetch index. last_fetch_index = Seed.select(fn.Max(Seed.fetch_index)).scalar() or 0 fetch_index = last_fetch_index + 1 for seed_text in seeds: # Create a new seed record from the text seed = Seed.create( fetch_index=fetch_index, seed=seed_text, depth=0, ) # Fetch the autocomplete results! get_results(seed, max_depth)
def get(self): self.response.headers['Content-Type'] = 'text/html' seeds = Seed.query(Seed.hidden != True) out = '<html><head><title>All Plando Authors</title></head><body><h5>All Seeds</h5><ul style="list-style-type:none;padding:5px">' authors = Counter([seed.author for seed in seeds]) for author, cnt in authors.most_common(): if cnt > 0: url = "%s/plando/%s" % (base_site, author) out += '<li style="padding:2px"><a href="%s">%s</a> (%s plandos)</li>' % ( url, author, cnt) out += "</ul></body></html>" self.response.out.write(out)
def get(self, author, seed_name): user = users.get_current_user() if user: dispname = user.email().partition("@")[0] if dispname == author: seed = Seed.get_by_id("%s:%s" % (author, seed_name)) if seed: seed.key.delete() self.redirect('/plando/%s' % author) else: log.error("couldn't find seed!") self.response.status = 404 else: log.error( "Auth failed, logged in as %s, trying to edit %s's seed" % (dispname, author)) self.response.status = 401 else: log.error("no auth D:") self.response.status = 401
def get(self, author): self.response.headers['Content-Type'] = 'text/html' owner = False user = users.get_current_user() if user: dispname = user.email().partition("@")[0] owner = dispname == author query = Seed.query(Seed.author == author) if not owner: query = query.filter(Seed.hidden != True) seeds = query.fetch() if len(seeds): out = '<html><head><title>Seeds by %s</title></head><body><div>Seeds by %s:</div><ul style="list-style-type:none;padding:5px">' % ( author, author) for seed in seeds: url = "%s/plando/%s/%s" % (base_site, author, seed.name) flags = ",".join(seed.flags) out += '<li style="padding:2px"><a href="%s">%s</a>: %s (%s players, %s)' % ( url, seed.name, seed.description, seed.players, flags) if owner: out += ' <a href="%s/edit">Edit</a>' % url if seed.hidden: out += " (hidden)" out += "</li>" out += "</ul></body></html>" self.response.write(out) else: if owner: self.response.write( "<html><body>You haven't made any seeds yet! <a href='/plando/%s/newseed/edit'>Start a new seed</a></body></html>" % author) else: self.response.write( '<html><body>No seeds by user %s</body></html>' % author)
def get_results(seed, max_depth): fetch_index = seed.fetch_index # Request for autocomplete results params = DEFAULT_PARAMS.copy() params['q'] = seed.seed response = make_request(default_requests_session.get, URL, params=params) time.sleep(REQUEST_DELAY) # enforce a pause between each fetch to be respectful to API # Go no further if the call failed if not response: return [] # Store data from the fetched queries doc = ElementTree.fromstring(response.text.encode('utf-8')) num_results = 0 rank = 1 for comp_sugg in doc.iterfind('CompleteSuggestion'): for suggestion in comp_sugg.iterfind('suggestion'): # Create a new query and add to the database data = suggestion.attrib['data'] # In Fourney et al.'s implementation of CUTS, the returned queries were checked so that # they started with the exactly the seed. We relax this restriction here. # We note that in some autocomplete entries use valuable synonyms for our # queries, such as converting node -> js or rearranging the terms. These modified # prefixes yield interesting queries that we don't want to miss. Query.create( fetch_index=fetch_index, seed=seed, query=data, rank=rank, depth=seed.depth, ) num_results += 1 rank += 1 # Only expand this seed into new seeds if we got a full set of results and # we have not yet descended to the maximum depth. if num_results == MAX_RESULTS and seed.depth < max_depth: for char in ALPHABET: # The initial query should be followed by a space. if seed.depth == 0 and char != ' ': continue # There shouldn't be any sequence of two spaces. if char == ' ' and seed.seed.endswith(' '): continue # Create and store new seed new_seed_text = seed.seed + char new_seed = Seed.create( fetch_index=fetch_index, parent=seed, seed=new_seed_text, depth=seed.depth + 1, ) # Fetch results for the new seed. get_results(new_seed, max_depth)