def image_exists(filename): """Check if image filename exists in the database.""" # Read file from database storage = DatabaseStorage(options=ImageDraw.DBS_OPTIONS) # Check all namespaces check_single = storage.open('single/' + filename, 'rb') check_relation = storage.open('relation/' + filename, 'rb') check_schema = storage.open('schema/' + filename, 'rb') if check_single or check_relation or check_schema: return True return False
def image_view(request, filename): # Read file from database storage = DatabaseStorage(DBS_OPTIONS) image_file = storage.open(filename, 'rb') if not image_file: raise Http404 file_content = image_file.read() # Prepare response content_type, content_encoding = mimetypes.guess_type(filename) response = HttpResponse(content=file_content, mimetype=content_type) response['Content-Disposition'] = 'inline; filename=%s' % filename if content_encoding: response['Content-Encoding'] = content_encoding return response
def get(self, request, *args, **kwargs): DBS_OPTIONS = { 'table': 'places_images', 'base_url': '/storage_images/', } filename = self.kwargs['filename'] storage = DatabaseStorage(DBS_OPTIONS) image_file = storage.open(filename, 'rb') if not image_file: raise Http404 file_content = image_file.read() content_type, content_encoding = mimetypes.guess_type(filename) response = HttpResponse(content=file_content, mimetype=content_type) response['Content-Disposition'] = 'inline; filename=%s' % filename if content_encoding: response['Content-Encoding'] = content_encoding return response
class Varuosa(models.Model): varuosa_id = models.PositiveIntegerField(primary_key=True, default=next_varuosa_id) artikli_nr = models.CharField(max_length=50, unique=True, default=next_artikli_nr) tukihind = models.DecimalField(default=0.0, max_digits=10, decimal_places=2) kogus = models.PositiveSmallIntegerField(default=0) katalogi_nr = models.CharField(max_length=50, blank=True, null=True) monteerimis_koht = models.CharField(max_length=100, blank=True, null=True) mootmed = models.CharField(max_length=50, blank=True, null=True) kirjeldus = models.TextField(blank=True, null=True) varuosa_kategooria = models.ForeignKey(Varuosa_kategooria) varuosa_kulg = models.ForeignKey(Varuosa_kulg, blank=True, null=True) varuosa_seisund = models.ForeignKey(Varuosa_seisund, default=1) tootja = models.ForeignKey(Tootja) auto_modifikatsioonid = models.ManyToManyField(Auto_modifikatsioon) pilt = models.ImageField( null=True, blank=True, upload_to='img_varuosad/', storage=DatabaseStorage(options=settings.DBS_OPTIONS), default='img_varuosad/default_varuosa.png') def __unicode__(self): return self.artikli_nr def save(self, *args, **kwargs): from django.core.exceptions import ValidationError try: self.clean() except ValidationError: raise # Call the "real" save() method.s super(Varuosa, self).save(*args, **kwargs) def clean(self): from django.core.exceptions import ValidationError import re # Don't allow draft entries to have a pub_date. pattern = re.compile('^ALM-[\d]+[a-zA-Z]*') if not pattern.match(self.artikli_nr): raise ValidationError( 'Artikli nr. should be like ALM-x, where x is any digit') if self.tukihind < 0: raise ValidationError('Tukihind should be positive number') class Meta: ordering = ['artikli_nr'] unique_together = (("artikli_nr", "varuosa_kategooria"), ) verbose_name_plural = "varuosad"
def download_attach(request, filename): # Read file from database storage = DatabaseStorage(DBS_OPTIONS) gpg_file = storage.open(filename, 'rb') if not gpg_file: raise Http404 file_content = gpg_file.read() # Prepare response content_type, content_encoding = mimetypes.guess_type(filename) response = HttpResponse(content=file_content, mimetype=content_type) response['Content-Disposition'] = 'inline; filename=%s' % filename if content_encoding: response['Content-Encoding'] = content_encoding items = [] items.append({'suser': request.user}) items.append({'cs1Label': 'filename'}) items.append({'cs1': filename}) log_cef("AdminDownload", "Desktop Admin downloaded file %s" % filename) return response
class SingleImageDraw(ImageDraw): number = models.CharField(max_length=20, primary_key=True) denomination = models.CharField(max_length=80, null=True) image = models.ImageField( null=True, blank=True, upload_to='single/', storage=DatabaseStorage(options=ImageDraw.DBS_OPTIONS)) def __unicode__(self): return u'%s' % self.number class Meta: ordering = ('number', )
class SchemaImageDraw(ImageDraw): reference = models.CharField(max_length=20, primary_key=True) model = models.CharField(max_length=20) image = models.ImageField( null=True, blank=True, upload_to='schema/', storage=DatabaseStorage(options=ImageDraw.DBS_OPTIONS)) def __unicode__(self): return u'%s' % self.reference class Meta: ordering = ('reference', )
class RelationImageDraw(ImageDraw): # Only for RelationImageDraw. DRAW_TYPE = (('H', 'Horizontal'), ('V', 'Vertical')) code = models.CharField(max_length=20, primary_key=True) reference = models.CharField(max_length=20) type = models.CharField(max_length=1, choices=DRAW_TYPE) image = models.ImageField( null=True, blank=True, upload_to='relation/', storage=DatabaseStorage(options=ImageDraw.DBS_OPTIONS)) def __unicode__(self): return u'%s' % self.code class Meta: ordering = ('reference', )
class File(models.Model): Nom = models.TextField(null=True, blank=True) Size = models.FloatField(null=True, blank=True) Binary = models.FileField(upload_to="file", storage=DatabaseStorage(settings.DB_FILES)) Date = models.DateField(null=True, blank=True)