Skip to content

Commit

Permalink
Added option to generate file of N uuids to copy and paste from
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmarsden committed Aug 5, 2021
1 parent f73b77a commit 7cef616
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
8 changes: 6 additions & 2 deletions web/templates/uuid.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ <h1>Nansen Legacy UUID Generator</h1>
<p> ${str(uuid.uuid1())}</p>
<p> ${str(uuid.uuid1())}</p>
<p> ${str(uuid.uuid1())}</p>
<form method=post>
<p><input type=submit value='Get new UUID'>
<form method=post enctype="multipart/form-data"><br>
<p><input type=submit value='Get new UUIDs' name='regenerate'></p>
<br><br>OR<br><br>
<p>Create a .txt file with any number of UUIDs</p>
Enter number of UUIDs to generate: <input type = "text" name = "number"><br><br>
<p><input type=submit value='Generate file' name='file'>
<div class="wrapper">
</div>
</form>
Expand Down
60 changes: 49 additions & 11 deletions web/uuid.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@ import codecs
import uuid
import shutil
import textwrap
import tempfile

__updated__ = '2018-06-29'


# cgitb.enable()
cgitb.enable()

def uuids_many(path, number=100):
'''
Creates a file with n uuids, one per line
'''
f = open(path,"w")
for n in range(number):
f.write(str(uuid.uuid1())+'\n')

def warn(message, color='red'):
message = bytes(message, "utf-8")
sys.stdout.buffer.write(b'<p style="color:'+bytes(color,"utf-8")+b'">'+message+b'</p>')

method = os.environ.get("REQUEST_METHOD", "GET")

Expand All @@ -35,10 +47,6 @@ from mako.lookup import TemplateLookup
templates = TemplateLookup(
directories=['templates'], output_encoding='utf-8')


# method = 'POST'
# method = 'Test'

if method == "GET": # This is for getting the page

# Using sys, as print doesn't work for cgi in python3
Expand All @@ -50,9 +58,39 @@ if method == "GET": # This is for getting the page
template.render(uuid=uuid))

elif method == "POST":
template = templates.get_template("uuid.html")

sys.stdout.flush()
sys.stdout.buffer.write(b"Content-Type: text/html\n\n")
sys.stdout.buffer.write(
template.render(uuid=uuid))

form = cgi.FieldStorage()

if "file" in form:
tmpfile = '/tmp/tmpfile.txt'
try:
number = int(form["number"].value)
except:
number = 0

if number < 1:
error = "Please enter an integer greater than 0"
sys.stdout.flush()
sys.stdout.buffer.write(b"Content-Type: text/html\n\n")
sys.stdout.buffer.write(b"<!doctype html>\n<html>\n <meta charset='utf-8'>")
warn(error)
sys.stdout.buffer.write(b'</html>')
else:

filename='uuids.txt'
print("Content-Type: text/plain")
print("Content-Disposition: attachment; filename="+filename+"\n")
path = "/tmp/" + next(tempfile._get_candidate_names()) + ".txt"

uuids_many(path, number)

with open(path, "rb") as f:
sys.stdout.flush()
shutil.copyfileobj(f, sys.stdout.buffer)
else:
template = templates.get_template("uuid.html")

sys.stdout.flush()
sys.stdout.buffer.write(b"Content-Type: text/html\n\n")
sys.stdout.buffer.write(
template.render(uuid=uuid))

0 comments on commit 7cef616

Please sign in to comment.