def save(self, *args, **kwargs): if not self.img_hash: pil_img = PilImage.open(self.get_absolute_path()) self.img_hash = imagehash.dhash(pil_img, 16) if self.get_datetime() > datetime.datetime.now( tz=get_tz()) - datetime.timedelta(0, 60): found_img = None try: found_img = Image.objects.filter(md5sum=self.md5sum).exclude( pk=self.pk)[0] except IndexError: pass try: found_img = Image.objects.filter( img_hash=self.img_hash).exclude(pk=self.pk)[0] except IndexError: pass if found_img: if self.get_absolute_path() != found_img.get_absolute_path(): os.remove(self.get_absolute_path()) return found_img super(Image, self).save(*args, **kwargs) return self
def create(cls, to_comment_id): comment = Comment.objects.get(pk=to_comment_id) if not comment.root_id: return root = Comment.objects.get(pk=comment.root_id) if comment.cookie_id == root.cookie_id: return to_wallet, _ = Wallet.objects.get_or_create(cookie_id=root.cookie_id) to_cost = cls.get_incoming_cost() cls.objects.create( root_id=root.pk, comment_id=to_comment_id, to_wallet=to_wallet ) Wallet.objects.filter(pk=to_wallet.pk).update( current_balance = min(to_wallet.current_balance + to_cost, to_wallet.max_balance), last_add = datetime.datetime.now(get_tz()) )
def create(cls, from_cookie, comment_id, is_staff, new_cat): comment = Comment.objects.get(pk=comment_id) from_wallet, _ = Wallet.objects.get_or_create(cookie_id=from_cookie) from_cost = cls.get_outgoing_cost() if from_wallet.current_balance < from_cost: return if is_staff: Wallet.objects.filter(pk=from_wallet.pk).update( current_balance=KarmaConfig.MODERATOR_WALLET ) else: Wallet.objects.filter(pk=from_wallet.pk).update( current_balance=F('current_balance') - from_cost, last_sub=datetime.datetime.now(get_tz()) ) cls.objects.create( root_id=comment.root_id or comment.comment_id, comment_id=comment_id, to_wallet=from_wallet, new_cat=new_cat, canceled=comment.deleted ) if not comment.deleted: Lenta.objects.filter(root_id=comment.root_id or comment.comment_id).update(category=new_cat)
def reapply(self): to_cost = self.get_incoming_cost() if not self.canceled: Wallet.objects.filter(pk=self.to_wallet.pk).update( current_balance=min(self.to_wallet.current_balance + to_cost, self.to_wallet.max_balance), last_sub = datetime.datetime.now(get_tz()) )
def reapply(self): to_cost = self.get_incoming_cost() from_cost = self.get_outgoing_cost() Wallet.objects.filter(pk=self.from_wallet.pk).update( current_balance=F('current_balance') - from_cost, max_balance=F('max_balance') + KarmaConfig.LIKE_CAP_INCREASE, last_add = datetime.datetime.now(get_tz()) ) if not self.canceled: Wallet.objects.filter(pk=self.to_wallet.pk).update( current_balance=min(self.to_wallet.current_balance + to_cost, self.to_wallet.max_balance), last_sub = datetime.datetime.now(get_tz()) ) Comment.objects.filter(comment_id=self.comment_id).update(rating=F('rating') + 1)
def reapply(self): from_cost = self.get_outgoing_cost() Wallet.objects.filter(pk=self.to_wallet.pk).update( current_balance=F('current_balance') - from_cost, last_add=datetime.datetime.now(get_tz()) ) Lenta.objects.filter(root_id=self.root_id).update(category=self.new_cat)
def create(cls, from_cookie, comment_id, is_staff): comment = Comment.objects.get(pk=comment_id) from_wallet, _ = Wallet.objects.get_or_create(cookie_id=from_cookie) to_wallet, _ = Wallet.objects.get_or_create(cookie_id=comment.cookie_id) to_cost = cls.get_incoming_cost() from_cost = cls.get_outgoing_cost() if from_wallet.current_balance < from_cost: return if is_staff: Wallet.objects.filter(pk=from_wallet.pk).update( current_balance = KarmaConfig.MODERATOR_WALLET ) else: Wallet.objects.filter(pk=from_wallet.pk).update( current_balance=F('current_balance') - from_cost, max_balance=F('max_balance') + KarmaConfig.LIKE_CAP_INCREASE, last_sub = datetime.datetime.now(get_tz()) ) cls.objects.create( root_id=comment.root_id or comment.comment_id, comment_id=comment_id, from_wallet=from_wallet, to_wallet=to_wallet, canceled=comment.deleted ) if not comment.deleted: Wallet.objects.filter(pk=to_wallet.pk).update( current_balance = min(to_wallet.current_balance + to_cost, to_wallet.max_balance), last_add = datetime.datetime.now(get_tz()) ) Comment.objects.filter(comment_id=comment.comment_id).update(rating=F('rating') + 1)
def cancel(cls, **kwargs): transactions = cls.objects.filter(canceled=False, **kwargs) transactions.update(canceled=True) count = transactions.count() to_cost = cls.get_incoming_cost() for t in transactions: Wallet.objects.filter(pk=t.to_wallet.pk).update( current_balance = max(t.to_wallet.current_balance - to_cost, 0), last_sub = datetime.datetime.now(get_tz()) ) if kwargs.get('comment_id'): comment = Comment.objects.get(pk=kwargs.get('comment_id')) if comment.root_id and comment.root_id != comment.comment_id: Comment.objects.filter(comment_id=comment.root_id).update(rating=F('rating') - count)
def save_image(self, f): """ :param f: UploadedFile :return: """ self.invalidate_datetime() dt = datetime.datetime.fromtimestamp(self.datetime, tz=get_tz()) today_path = os.path.join(dt.strftime("%Y%m"), dt.strftime("%d")) path = os.path.join('images', today_path) self.text_id, ext = self.generate_name(f.name) while os.path.exists( os.path.join(settings.MEDIA_ROOT, path, "%s.%s" % (self.text_id, ext))): self.text_id, ext = self.generate_name(f.name) final_fname = "%s.%s" % (self.text_id, ext) final_path = os.path.join(path, final_fname) full_final_path = os.path.join(settings.MEDIA_ROOT, final_path) hash = hashlib.md5() try: os.makedirs(os.path.dirname(full_final_path)) except OSError: print pass with open(full_final_path, 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) hash.update(chunk) img = PilImage.open(full_final_path) self.x_size, self.y_size = img.size self.extension = final_path.rsplit('.')[-1] self.md5sum = hash.hexdigest() thumber = easy_thumbnails.files.get_thumbnailer( open(full_final_path), relative_name=final_path) thumb = thumber.generate_thumbnail({ 'size': (200, 0), 'crop': 'smart', 'sharpen': True }) try: os.makedirs(os.path.join(settings.MEDIA_ROOT, 'thumbs', today_path)) except OSError: pass thumb.image.save( os.path.join(settings.MEDIA_ROOT, 'thumbs', today_path, final_fname)) preview = thumber.generate_thumbnail({'size': (600, 600)}) try: os.makedirs( os.path.join(settings.MEDIA_ROOT, 'previews', today_path)) except OSError: pass preview_img = self.apply_watermark(preview.image) preview_img.save( os.path.join(settings.MEDIA_ROOT, 'previews', today_path, final_fname)) self.apply_watermark(img).save(full_final_path)