def update_list(self): self.pagelist.clear() qb = midgard.query_builder('midgard_page') qb.add_order('title', 'ASC') res = qb.execute() for r in res: self.pagelist.insertItem(r.id, r.title)
def __init__(self, nickname, login = False): gobject.GObject.__init__(self) if login is True: self.init_midgard_session(nickname) # Every adventurer has a ttoa_user record, check if it already exists qb = midgard.query_builder('ttoa_user') qb.add_constraint('username', '=', nickname) if qb.count() is 0: # No user yet in database, create it self.user = midgard.mgdschema.ttoa_user() self.user.username = nickname self.user.create() # Default colour for new adventurers self.set_colour('grey') else: users = qb.execute() for user in users: self.user = user self.colour = self.user.get_parameter('adventuretablet', 'colour') if self.colour is None: self.set_colour('grey') if login is True: self.apikey = self.user.get_parameter('adventuretablet', 'apikey') self.nick = nickname self.location = point.point(self.user.latitude, self.user.longitude)
def test_inherited_querybuilder(self): inherited = inherited_object() inherited.name = 'tests'; inherited.title = 'tests'; inherited.create() if (inherited.id > 0): assert True else: assert False qb = midgard.query_builder('inherited_object') qb.add_constraint('name', '=', 'tests') if len(qb.execute) == 1: assert True
def open_page(self, item=False): item = self.pagelist.currentItem() qb = midgard.query_builder('midgard_page') title = ("%s" % item.text()) qb.add_constraint('title', '=', title) res = qb.execute() if len(res) == 0: return False self.mgd_page = res[0] self.texteditor.setText(res[0].content) self.pagetitle.setText(res[0].title) return True
def log(self, button): wido= hildon.StackableWindow() vbox= gtk.VBox(False, 0) wido.add(vbox) wido.set_title("the Log") qb = midgard.query_builder('ttoa_log') qb.add_constraint('mission', '=', self.current_adventure.mission.id) qb.add_order('metadata.created', 'DESC') logs = qb.execute() players = {} sw = hildon.PannableArea() logview = hildon.TextView() logview.set_editable(False) logview.set_wrap_mode(gtk.WRAP_WORD) logbuffer = logview.get_buffer() sw.add(logview) vbox.pack_start(sw, expand = True) log_string = '' for log in logs: # Now we have an individual log entry with coordinates in log.latitude etc, and text in log.comment # Match log to the author of it if log.author not in players: player = midgard.mgdschema.ttoa_user() if player.get_by_id(log.author): players[log.author] = player.username else: players[log.author] = 'anon #%s' % (log.author) log_string = log_string + "%s: %s\n" % (players[log.author], log.comment) logbuffer.set_text(log_string) if self.current_adventure.qaikuid is not None: self.qaiku_message = hildon.Entry(gtk.HILDON_SIZE_AUTO) self.qaiku_message.set_placeholder("I'm finding myself in deep trouble..") log_b = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL, "Post to Qaiku") log_b.connect("clicked", self.log_button) vbox.pack_end(log_b, expand = False) vbox.pack_end(self.qaiku_message, expand = False) wido.show_all()
def adventure_from_mission(self, mission, player): target = point.point(mission.latitude, mission.longitude) mission_adventure = adventure.adventure(target, mission.text, mission) # Check for adventurers adventurers = [] qb = midgard.query_builder('ttoa_log') qb.add_constraint('mission', '=', mission.id) logs = qb.execute() for log in logs: if log.author not in adventurers: adventurers.append(log.author) for participant in adventurers: user = midgard.mgdschema.ttoa_user() user.get_by_id(participant) if user.username == player.nick: # We don't add the current player to adventures, they do so manually continue else: mission_adventure.add_adventurer(adventurer.adventurer(user.username), True) return mission_adventure
def refresh_adventures(self, adventurer): # Clear old list of adventures self.adventures = [] # Fetch currently valid adventures from Midgard location = adventurer.location today = datetime.datetime.today() today = today.replace(microsecond=0) qb = midgard.query_builder('ttoa_mission') qb.add_constraint('validDate', '>=', today.isoformat(' ')) geohash_found = False missions = qb.execute() for mission in missions: if (mission.type is 1) and (int(math.floor(mission.latitude)) == int(math.floor(location.lat))) and (int(math.floor(mission.longitude)) == int(math.floor(location.lon))): # We have a geohash for today and current graticule geohash_found = True self.adventures.append(self.adventure_from_mission(mission, adventurer)) if geohash_found is False: # We didn't have a GeoHash for today yet, generate one geohash = self.adventure_from_geohash(adventurer, today) if geohash is not None: self.adventures.append(geohash)
import configuration import cache_midgard import sys if len(sys.argv) != 4: print "Usage: python midcom.py configuration sitegroup host" sys.exit() cnc = midgard.connection() cnc.open(sys.argv[1]) if cnc.set_sitegroup(sys.argv[2]) == False: print ("Sitegroup %s not found" % (sys.argv[2])) sys.exit() qb = midgard.query_builder('midgard_host') qb.add_constraint('name', '=', sys.argv[3]) res = qb.execute() if len(res) == 0: print ("Host %s not found" % (sys.argv[3])) sys.exit() host = res[0] # Testing cache cache = cache_midgard.cache_midgard(cnc, host) cache.delete_all('test') cache.delete('test_domain', 'test') # Testing configuration conf = configuration.configuration() conf.load_component_configuration("midgardmvc_core")
return except IOError, e: print "adventures_from_qaiku: Connection failed" return messages = simplejson.loads(req.read()) for message in messages: if message['in_reply_to_status_id']: # This is a log entry or comment, we're only interested in adventures continue if message['data'] == '': # No QaikuData found, we need this for our adventure continue qb = midgard.query_builder('ttoa_mission') qb.add_constraint('parameter.value', '=', message['id']) if qb.count() != 0: # We already have this adventure continue mission = midgard.mgdschema.ttoa_mission() mission.type = 2 mission.text = message['text'] mission.pubDate = timestamp mission.validDate = timestamp.replace(hour=23, minute=59, second=59) qaikudata = message['data'].split(',') if len(qaikudata) != 2: # Invalid mission coordinates, skip continue mission.latitude = float(qaikudata[0])
print "logs_from_qaiku for %s: Connection failed, error %s" % (self.name, e.message) return True except IOError, e: print "logs_from_qaiku for %s: Connection failed" % (self.name) return True latest_update = 0 messages = simplejson.loads(req.read()) messages.reverse() for message in messages: created_at = int(time.mktime(time.strptime(message['created_at'], '%a %b %d %H:%M:%S +0000 %Y'))) if created_at > latest_update: # We use timestamp from the messages in order to avoid gaps due to Qaiku and local machine being in different time latest_update = created_at qb = midgard.query_builder('ttoa_log') qb.add_constraint('mission', '=', self.mission.id) qb.add_constraint('parameter.value', '=', message['id']) if qb.count() != 0: # We already have this log entry #print "Skipping comment '%s' from %s as we already have it" % (message['text'], message['user']['screen_name']) continue if isinstance(message['geo'], dict) is False: # Log without a location, place to Timbuktu #print "Comment %s from %s has no location, sending %s to Timbuktu" % (message['text'], message['user']['screen_name'], message['user']['screen_name']) message['geo']['coordinates'][1] = 16.775833 message['geo']['coordinates'][0] = -3.009444 continue # Parse QaikuData