Skip to content

Commit 9920902

Browse files
Added PR number field, and underline utility
1 parent 53aeb41 commit 9920902

File tree

9 files changed

+48
-15
lines changed

9 files changed

+48
-15
lines changed

certificate_generator/app/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from flask import Flask
22

33
app = Flask(__name__)
4+
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
45

5-
6-
from app import routes
6+
from app import routes

certificate_generator/app/routes.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,32 @@
33
from flask import render_template, request, send_file
44
import os
55

6+
7+
@app.after_request
8+
def add_header(r):
9+
"""
10+
Add headers to both force latest IE rendering engine or Chrome Frame,
11+
and also to cache the rendered page for 10 minutes.
12+
"""
13+
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
14+
r.headers["Pragma"] = "no-cache"
15+
r.headers["Expires"] = "0"
16+
r.headers['Cache-Control'] = 'public, max-age=0'
17+
return r
18+
619
@app.route('/', methods=['GET', 'POST'])
720
def home():
821
return render_template("certificate.html")
922

1023

11-
@app.route('/download', methods=['GET', 'POST'])
12-
def download_certificate():
24+
@app.route('/render', methods=['POST'])
25+
def render_certificate():
1326
if request.method == "POST":
14-
file_name = generate_certificate(request.form['name'])
27+
file_name = generate_certificate(request.form['name'], request.form['pr_num'])
1528
return render_template('download.html', file_name=file_name)
1629

1730
@app.route('/download_certificate', methods=['GET'])
18-
def method_name():
31+
def download():
1932
if request.method == "GET":
2033
filename = request.args.get("filename")
2134
filepath = os.path.join("static/certificates/generated", filename)
-6.19 KB
Loading
Binary file not shown.
Binary file not shown.
-41.6 KB
Loading

certificate_generator/app/templates/certificate.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
{% block content %}
44
<div class="container w-50">
5-
<form action="/download" method="post">
5+
<form action="/render" method="post">
66
<div class="form-floating mb-3">
77
<input name="name" type="text" class="form-control" id="floatingInput" placeholder="Enter Name">
88
<label for="floatingInput">Enter Name</label>
99
</div>
10-
<div class="form-floating">
11-
<input name="date" type="date" class="form-control" id="floatingDate">
12-
<label for="floatingDate">Date</label>
10+
<div class="input-group mb-3">
11+
<span class="input-group-text" id="basic-addon1">#</span>
12+
<input name="pr_num" type="number" class="form-control" placeholder="PR number" aria-label="PR number"
13+
aria-describedby="basic-addon1">
1314
</div>
1415
<div class="my-4">
15-
<a href="/download">
16+
<a href="/render">
1617
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
1718
</a>
1819
</div>

certificate_generator/app/templates/home.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
</head>
1515

1616
<body>
17-
<div class="container text-center w-70 my-3">
17+
<div class="container text-center w-70 my-4">
1818
<h1>Certificate Generator</h1>
1919
</div>
20+
2021
{% block content %}
2122

2223
{% endblock content %}
24+
2325
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
2426
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
2527
crossorigin="anonymous"></script>

certificate_generator/app/utils.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from app import app
33
import os
44

5-
def generate_certificate(name):
5+
def generate_certificate(name, pr_num):
66
template_dir = os.path.join(app.root_path, "static/certificates/template")
77
template_filename = "template.png"
88

@@ -12,8 +12,25 @@ def generate_certificate(name):
1212

1313
img = Image.open(os.path.join(template_dir, template_filename))
1414
draw = ImageDraw.Draw(img)
15-
font = ImageFont.truetype(os.path.join(template_dir, "DroidSansMono.ttf"), 150)
16-
draw.text((1000, 1390), name, (0, 0, 0), font=font)
15+
16+
event_name = "Hacktoberfest"
17+
contributed_at = "Automation scripts"
18+
msg = f"""for taking part in {event_name} and
19+
contribution in #{pr_num} at {contributed_at}.
20+
"""
21+
22+
name_font = ImageFont.truetype(os.path.join(template_dir, "Sanchez-Regular.ttf"), 150)
23+
msg_font = ImageFont.truetype(os.path.join(template_dir, "Sanchez-Regular.ttf"), 70)
24+
25+
draw.text((1000, 1390), name, (51, 213, 172), font=name_font)
26+
draw.text((1000, 1650), msg, (14, 69, 115), font=msg_font)
27+
28+
twidth, theight = draw.textsize(contributed_at, font=msg_font)
29+
prwidth, prheight = draw.textsize(pr_num, font=msg_font)
30+
31+
lx = 1560 + prwidth + 120
32+
ly = 1810
33+
draw.line((lx, ly, lx + twidth, ly), fill=(14, 69, 115), width=5)
1734

1835
img.save(os.path.join(output_dir, output_filename))
1936

0 commit comments

Comments
 (0)