def get(self): user = auth.getCurrentUser() name = "Web" if user: try: user_data = models.getUser(user) except models.UserDoesNotExistError: user_data = models.UserData(user=user).save() try: device = models.getDevice("%s/%s" % (user.email(), name)) except models.DeviceDoesNotExistError: device = models.DeviceData(user=user_data, name=name).save() over_quota = False try: channel = channels.Channel(device.address, override_quota=user_data.immune()) channel_token = channel.token except channels.OverQuotaError: over_quota = True channel_token = 'overquota' template_values = { 'channel_id': channel_token, 'device': device.address, 'device_name': device.name, 'devices': user_data.getDevices(), 'over_quota': over_quota } path = os.path.join(os.path.dirname(__file__), 'devlinks_index.html') self.response.out.write(template.render(path, template_values)) else: self.redirect(users.create_login_url(self.request.uri))
def get(self, name=False): self.response.headers.add_header("Access-Control-Allow-Origin", "*") user = auth.getCurrentUser() response = {} if user: try: user_data = models.getUser(user) except models.UserDoesNotExistError: user_data = models.UserData(user=user).save() if not name: name = "Chrome" try: device = models.getDevice("%s/%s" % (user.email(), name)) except models.DeviceDoesNotExistError: device = models.DeviceData(user=user_data, name=name).save() try: channel = channels.Channel(device.address, override_quota=user_data.immune()) response['token'] = channel.token if channel.cached: response['code'] = 304 else: response['code'] = 200 except channels.OverQuotaError: response['code'] = 503 response['token'] = 'overquota' response['message'] = "Server is over quota." else: response['code'] = 401 response['message'] = 'Not logged in.' self.response.out.write(simplejson.dumps(response))
def post(self): self.response.headers.add_header("Access-Control-Allow-Origin", "*") user = auth.getCurrentUser() response = {} if user: try: user_data = models.getUser(user) except models.UserDoesNotExistError: user_data = models.UserData(user=user).save() name = self.request.get('name') if not name: name = "Chrome" try: device = models.getDevice("%s/%s" % (user.email(), name)) except models.DeviceDoesNotExistError: device = models.DeviceData(name=name, user=user_data).save() receiver = None if self.request.get("receiver"): try: receiver = models.getDevice( "%s/%s" % (user.email(), self.request.get("receiver"))) except models.DeviceDoesNotExistError: receiver = models.DeviceData( name=self.request.get("receiver"), user=user_data).save() if receiver == None: receiver = device link = models.LinkData(url=self.request.get('link'), sender=device, receiver=receiver).save() if models.getQuota().amount >= models.getStats( 'channels').count or user_data.immune(): channel = channels.Channel(receiver.address, False) channel.sendLink(link) response['code'] = 200 response['link'] = link.url else: response['code'] = 503 response['link'] = link.url else: response['code'] = 401 response['link'] = self.request.get('link') self.response.out.write(simplejson.dumps(response))
def get(self): user = users.User('*****@*****.**') try: user_data = models.getUser(user) except models.UserDoesNotExistError: user_data = models.UserData(user=user).save() try: device = models.getDevice("%s/%s" % (user.email(), "Web")) except models.DeviceDoesNotExistError: device = models.DeviceData(user=user_data, name="Web").save() channel = channels.Channel(device.address, override_quota=True) path = os.path.join(os.path.dirname(__file__), 'dashboard.html') stats_data = models.StatsData.all().order("-date").fetch(1000) template_values = {'channel_id': channel.token, 'stats': {}} stats = template_values['stats'] for datapoint in stats_data: datestamp = int( time.mktime(datapoint.date.date().timetuple()) * 1000) hour = datapoint.date.hour if datestamp not in stats: stats[datestamp] = { 'datestamp': datestamp, 'date': datapoint.date, 'datapoints': {} } if datapoint.datapoint not in stats[datestamp]['datapoints']: stats[datestamp]['datapoints'][datapoint.datapoint] = { 'datapoint': datapoint.datapoint, 'values': {} } point_id = "%s" % hour if datapoint.duration == "day": point_id = "total" dp = datapoint.datapoint stats[datestamp]['datapoints'][dp]['values'][point_id] = { 'datapoint': datapoint.datapoint, 'datestamp': datestamp, 'hour': hour, 'count': int(datapoint.count), 'duration': datapoint.duration } template_values['stats'] = stats logging.info(template_values) self.response.out.write(template.render(path, template_values))
def post(self, name="Chrome"): self.response.headers.add_header("Access-Control-Allow-Origin", "*") user = auth.getCurrentUser() device = None if user: try: user_data = models.getUser(user) except models.UserDoesNotExistError: user_data = models.UserData(user=user).save() try: device = models.getDevice("%s/%s" % (user.email(), name)) except: device = models.DeviceData(user=user_data, name=name).save() last_links = models.getUnreadLinks(device) channel = channels.Channel(device.address, False) for link in last_links: channel.queueLink(link) channel.send() stats.record("user_connected", simplejson.dumps({"user": user.email()})) self.response.out.write(device.address)