dynamic_item_img()   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A Hack.__init__() 0 3 1
A Hack.__getattr() 0 3 1
A Hack.getattr() 0 3 1
A Hack.close() 0 2 1
A Hack.read() 0 14 4
1
# Generates dynamic file content on the fly from the DB
2
3
from pyramid.events import subscriber
4
from pyramid.events import BeforeRender
5
from pyramid.httpexceptions import HTTPFound
6
from pyramid.renderers import render
7
from pyramid.renderers import render_to_response
8
from pyramid.response import Response
9
from pyramid.response import FileResponse, FileIter
10
from pyramid.view import view_config, forbidden_view_config
11
12
from .models import *
13
from .models.model import *
14
from .models import user as __user
15
from .models.user import User
16
from .models.item import Item
17
from .models.box import Box
18
from .models.box_item import BoxItem
19
from .models.transaction import Transaction, Deposit, CashDeposit, BTCDeposit, Purchase
20
from .models.transaction import Inventory, InventoryLineItem
21
from .models.transaction import PurchaseLineItem, SubTransaction, SubSubTransaction
22
from .models.account import Account, VirtualAccount, CashAccount
23
from .models.event import Event
24
from .models import event as __event
25
from .models.vendor import Vendor
26
from .models.item_vendor import ItemVendor
27
from .models.box_vendor import BoxVendor
28
from .models.request import Request
29
from .models.announcement import Announcement
30
from .models.btcdeposit import BtcPendingDeposit
31
from .models.receipt import Receipt
32
from .models.pool import Pool
33
from .models.pool_user import PoolUser
34
35
@view_config(route_name='dynamic_item_img')
36
def dynamic_item_img(request):
37
	item = Item.from_id(request.matchdict['item_id'])
38
	response = Response(content_type='image/jpeg')
39
	class Hack():
40
		def __init__(self, img):
41
			self.img = img
42
			self.idx = 0
43
		def getattr(self, item):
44
			print("item {}".format(item))
45
			raise AttributeError
46
		def __getattr(self, item):
47
			print("item {}".format(item))
48
			raise AttributeError
49
		def read(self, block_size = None):
50
			if self.idx >= len(self.img):
51
				return ''
52
			if block_size is None:
53
				self.idx = len(self.img)
54
				return self.img
55
			if self.idx + block_size > len(self.img):
56
				r = self.img[self.idx:]
57
				self.idx = len(self.img)
58
				return r
59
			else:
60
				r = self.img[self.idx:block_size]
61
				self.idx += block_size
62
				return r
63
		def close(self):
64
			pass
65
	h = Hack(item.img.img)
66
	response.app_iter = FileIter(h)
67
	return response
68