class Token(models.Model): link = models.ForeignKey(ApplicationLink) user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="%(app_label)s_%(class)s_user", help_text="User token authenticates as") url = models.TextField(help_text="Suburl this token is restricted to, " "relative e.g. (/my/single/service/entrypoint)", default="/") secret = models.CharField(max_length=320, help_text="Token Secret", unique=True) modified = models.DateTimeField(default=timezone.now, editable=False) timeout = models.IntegerField(default=600, help_text="Timeout token in " "seconds, 0 means never times out") class Meta: app_label = 'swingers' def save(self, *args, **kwargs): try: revision.unregister(self.__class__) except: pass super(Token, self).save(*args, **kwargs) def natural_key(self): return (self.secret, ) def get_by_natural_key(self, secret): return self.get(secret=secret) def __str__(self): return "{0} - {1}:{2}@{3}".format(self.pk, self.user, self.secret, self.link.client_name)[:320]
class TrafficControlDiagram(models.Model): """ """ name = models.CharField(max_length=64, unique=True) path = models.FileField(storage=trafficdiagram_storage, upload_to=".") display_order = models.IntegerField(default=1) objects = TrafficControlDiagramManager() @property def dimensions(self): """width, height = subprocess.check_output([ "identify", "-format", "%Wx%H,", self.document.path ]).split(",")[0].strip().split("x") return {"width": width, "height": height} """ dimensions_info = get_dimensions(self.document.path) logger.info( str({ "width": dimensions_info.width, "height": dimensions_info.height })) return { "width": dimensions_info.width, "height": dimensions_info.height } @property def document(self): return self.path @property def descriptor(self): return self.name @property def modified(self): return False @property def filename(self): return os.path.basename(self.path.path) def __str__(self): return self.name def natural_key(self): return (self.name, ) class Meta: ordering = ['id'] verbose_name = "Traffic Control Diagram" verbose_name_plural = "Traffic Control Diagrams"
class AnnualIndicativeBurnProgram(models.Model): objectid = models.IntegerField(primary_key=True) wkb_geometry = models.MultiPolygonField(srid=4326, blank=True, null=True) region = models.CharField(max_length=35, blank=True) district = models.CharField(max_length=35, blank=True) burnid = models.CharField(max_length=30, blank=True) fin_yr = models.CharField(verbose_name='Fin Year', max_length=9, blank=True, null=True) location = models.CharField(max_length=254, blank=True) status = models.CharField(max_length=254, blank=True) priority = models.DecimalField(max_digits=9, decimal_places=0, blank=True, null=True) #content = models.CharField(max_length=254, blank=True) #issues = models.CharField(max_length=254, blank=True) treatment = models.DecimalField(max_digits=9, decimal_places=0, blank=True, null=True) #purpose_1 = models.CharField(max_length=254, blank=True) #program = models.CharField(max_length=254, blank=True) #acb = models.CharField(max_length=254, blank=True) trtd_area = models.CharField(max_length=254, blank=True) #yslb = models.CharField(max_length=254, blank=True) area_ha = models.DecimalField(max_digits=19, decimal_places=11, blank=True, null=True) perim_km = models.DecimalField(max_digits=19, decimal_places=11, blank=True, null=True) longitude = models.DecimalField(max_digits=19, decimal_places=11, blank=True, null=True) latitude = models.DecimalField(max_digits=19, decimal_places=11, blank=True, null=True) objects = models.GeoManager() class Meta: managed = False
class ApplicationLink(models.Audit): AUTH_METHOD = Choices('basic', 'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') client_name = models.CharField( max_length=320, help_text="project/host of client, this app is {0}".format( settings.SITE_NAME)) server_name = models.CharField( max_length=320, help_text="project/host of server, this app is {0}".format( settings.SITE_NAME)) server_url = models.TextField( help_text="URL service backend requests should be made to") identifier = models.CharField( max_length=320, null=True, blank=True, help_text="IP or Hostname, optional for added security") secret = models.CharField(max_length=320, help_text="Application secret") timeout = models.IntegerField(default=600, help_text="Timeout of oauth tokens in " "seconds") auth_method = models.CharField(choices=AUTH_METHOD, default=AUTH_METHOD.sha256, max_length=20) class Meta(Audit.Meta): unique_together = ("client_name", "server_name") app_label = 'swingers' def natural_key(self): return (self.client_name, self.server_name) def get_by_natural_key(self, client_name, server_name): return self.get(client_name=client_name, server_name=server_name) def get_access_token(self, user_id, expires=600): """ Returns an access token for with the current user. Note: uses a hardcoded URL when determining where to send the request. """ url = self.server_url + "/api/swingers/v1/{0}/request_token" nonce = make_nonce() r = requests.get(url.format(self.server_name), params={ "user_id": user_id, "nonce": nonce, "client_secret": self.get_client_secret(user_id, nonce), "client_id": self.client_name, "expires": expires }) if r.ok: return r.content else: r.raise_for_status() def get_client_secret(self, user_id, nonce): """ Returns the client secret based on a user and a nonce. """ stringtohash = "{0}{1}{2}".format(self.secret, user_id, nonce) return getattr(hashlib, self.auth_method)(stringtohash).hexdigest()
class Counter(models.Model): num = models.IntegerField(default=0) def get_absolute_url(self): return "/"