mirror of
https://git.wownero.com/wownero/wownero-funding-system.git
synced 2024-08-15 00:53:45 +00:00
Include QR codes on the proposal page; added API route
This commit is contained in:
parent
75c7f49842
commit
531072d2aa
15 changed files with 191 additions and 21 deletions
86
funding/bin/qr.py
Normal file
86
funding/bin/qr.py
Normal file
|
@ -0,0 +1,86 @@
|
|||
import os
|
||||
from io import BytesIO
|
||||
|
||||
import pyqrcode
|
||||
from PIL import Image, ImageDraw
|
||||
|
||||
import settings
|
||||
|
||||
|
||||
class QrCodeGenerator:
|
||||
def __init__(self):
|
||||
self.base = 'funding/static/qr'
|
||||
self.image_size = (300, 300)
|
||||
self.pil_save_options = {
|
||||
'quality': 25,
|
||||
'optimize': True
|
||||
}
|
||||
|
||||
if not os.path.exists(self.base):
|
||||
os.mkdir(self.base)
|
||||
|
||||
def exists(self, address):
|
||||
if not os.path.exists(self.base):
|
||||
os.mkdir(self.base)
|
||||
if os.path.exists(os.path.join(self.base, '%s.png' % address)):
|
||||
return True
|
||||
|
||||
def create(self, address, dest=None, color_from=(210, 83, 200), color_to=(255, 169, 62)):
|
||||
"""
|
||||
Create QR code image. Optionally a gradient.
|
||||
:param address:
|
||||
:param dest:
|
||||
:param color_from: gradient from color
|
||||
:param color_to: gradient to color
|
||||
:return:
|
||||
"""
|
||||
if len(address) != settings.COIN_ADDRESS_LENGTH:
|
||||
raise Exception('faulty address length')
|
||||
|
||||
if not dest:
|
||||
dest = os.path.join(self.base, '%s.png' % address)
|
||||
|
||||
created = pyqrcode.create(address, error='L')
|
||||
buffer = BytesIO()
|
||||
created.png(buffer, scale=14, quiet_zone=2)
|
||||
|
||||
im = Image.open(buffer)
|
||||
im = im.convert("RGBA")
|
||||
im.thumbnail(self.image_size)
|
||||
|
||||
im_data = im.getdata()
|
||||
|
||||
# make black color transparent
|
||||
im_transparent = []
|
||||
for color_point in im_data:
|
||||
if sum(color_point[:3]) == 255 * 3:
|
||||
im_transparent.append(color_point)
|
||||
else:
|
||||
# get rid of the subtle grey borders
|
||||
alpha = 0 if color_from and color_to else 1
|
||||
im_transparent.append((0, 0, 0, alpha))
|
||||
continue
|
||||
|
||||
if not color_from and not color_to:
|
||||
im.save(dest, **self.pil_save_options)
|
||||
return dest
|
||||
|
||||
# turn QR into a gradient
|
||||
im.putdata(im_transparent)
|
||||
|
||||
gradient = Image.new('RGBA', im.size, color=0)
|
||||
draw = ImageDraw.Draw(gradient)
|
||||
|
||||
for i, color in enumerate(QrCodeGenerator.gradient_interpolate(color_from, color_to, im.width * 2)):
|
||||
draw.line([(i, 0), (0, i)], tuple(color), width=1)
|
||||
|
||||
im_gradient = Image.alpha_composite(gradient, im)
|
||||
im_gradient.save(dest, **self.pil_save_options)
|
||||
|
||||
return dest
|
||||
|
||||
@staticmethod
|
||||
def gradient_interpolate(color_from, color_to, interval):
|
||||
det_co = [(t - f) / interval for f, t in zip(color_from, color_to)]
|
||||
for i in range(interval):
|
||||
yield [round(f + det * i) for f, det in zip(color_from, det_co)]
|
|
@ -1,15 +1,21 @@
|
|||
from datetime import datetime, date
|
||||
import requests
|
||||
from flask import g
|
||||
from flask.json import JSONEncoder
|
||||
import os
|
||||
import json
|
||||
from datetime import datetime, date
|
||||
|
||||
import pyqrcode
|
||||
import requests
|
||||
from flask import g, request
|
||||
from flask.json import JSONEncoder
|
||||
|
||||
import settings
|
||||
|
||||
|
||||
def json_encoder(obj):
|
||||
if isinstance(obj, (datetime, date)):
|
||||
return obj.isoformat()
|
||||
raise TypeError ("Type %s not serializable" % type(obj))
|
||||
|
||||
|
||||
class Summary:
|
||||
@staticmethod
|
||||
def fetch_prices():
|
||||
|
@ -60,26 +66,35 @@ class Summary:
|
|||
cache.set(cache_key, data=data, expiry=300)
|
||||
return data
|
||||
|
||||
|
||||
def price_cmc_btc_usd():
|
||||
headers = {'User-Agent': 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0'}
|
||||
try:
|
||||
print('request coinmarketcap')
|
||||
r = requests.get('https://api.coinmarketcap.com/v2/ticker/1/?convert=USD', headers=headers)
|
||||
r.raise_for_status()
|
||||
return r.json().get('data', {}).get('quotes', {}).get('USD', {}).get('price')
|
||||
except:
|
||||
return
|
||||
|
||||
|
||||
def coin_btc_value():
|
||||
headers = {'User-Agent': 'Mozilla/5.0 (Android 4.4; Mobile; rv:41.0) Gecko/41.0 Firefox/41.0'}
|
||||
try:
|
||||
print('request TO')
|
||||
r = requests.get('https://tradeogre.com/api/v1/ticker/BTC-WOW', headers=headers)
|
||||
r.raise_for_status()
|
||||
return float(r.json().get('high'))
|
||||
except:
|
||||
return
|
||||
|
||||
|
||||
def coin_to_usd(amt: float, usd_per_btc: float, btc_per_coin: float):
|
||||
try:
|
||||
return round(usd_per_btc / (1.0 / (amt * btc_per_coin)), 2)
|
||||
except:
|
||||
pass
|
||||
pass
|
||||
|
||||
|
||||
def get_ip():
|
||||
return request.headers.get('X-Forwarded-For') or request.remote_addr
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue