Skip to content

Commit

Permalink
New feature: allow users to select from pre-installed fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
sijie123 committed Jun 11, 2021
1 parent 1ae06f2 commit de15a74
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 25 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ pytz
django-websocket-redis
openpyxl
raven
pyppeteer==0.2.5
pyppeteer==0.2.5
fclist-cffi==1.1.2
2 changes: 1 addition & 1 deletion trans/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CustomUserResource(ModelResource):

class Meta:
model = User
fields = ('username', 'country', 'language', 'font', 'raw_password', 'num_of_contestants',)
fields = ('username', 'country', 'language', 'text_family', 'raw_password', 'num_of_contestants',)
import_id_fields = ('username',)


Expand Down
54 changes: 45 additions & 9 deletions trans/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
<div class="container">
<div class="col-sm-8 col-sm-offset-2">
<h1>Settings</h1>
<form class="form-horizontal" action="{% url 'settings'%}" method="post" enctype="multipart/form-data">
<div class="panel panel-default">
<div class="panel-heading">Custom Font</div>
<div class="panel-body">
<span class="help-block" style="margin: 10px 0 20px;">
A true-type font can be uploaded here to be used in the translation.
</span>
<div class="panel panel-default">
<div class="panel-heading">Custom Font</div>
<div class="panel-body">
<span class="help-block" style="margin: 10px 0 20px;">
A true-type font can be uploaded here to be used in the translation. This supersedes all other font settings.
</span>
<form class="form-horizontal" action="{% url 'settings'%}" method="post" enctype="multipart/form-data">
<input type="hidden" name="font_upload" id="font_upload" value="font_upload" />
<div class="form-group">
{% if text_font_name %}
<label class="control-label col-sm-3" for="email">Uploaded Font:</label>
Expand All @@ -35,9 +36,44 @@ <h1>Settings</h1>
</div>
{% endif %}
</div>
</div>
</form>
</div>
</form>
<div class="panel-body">
<span class="help-block" style="margin: 10px 0 20px;">
Alternatively, these fonts are already installed in Translation App. If you see a font you like, you can use it directly.
</span>
<form class="form-horizontal" action="{% url 'settings'%}" method="post" enctype="multipart/form-data">
<input type="hidden" name="font_select" id="font_select" value="font_select" />
{% csrf_token %}
<div class="form-group">
<label class="control-label col-sm-3" for="text_family">Select Font:</label>
<div class="col-sm-5">
<select class="form-control" name="text_family" id="text_family">
{% if not text_family %}
<option value="">Default (Arial) (Currently selected)</option>
{% else %}
<option value="">Default (Arial)</option>
{% endif %}
{% for font in available_fonts %}
{% if text_family and font == text_family %}
<option selected value="{{ font }}">{{ font }} (Currently selected)</option>
{% else %}
<option value="{{ font }}">{{ font }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="col-sm-4">
{% if text_font_name %}
<input type="submit" id="form-submit" class="btn btn-warning" disabled value="&times; Clear custom font first">
{% else %}
<input type="submit" id="form-submit" class="btn btn-primary" value="Use font">
{% endif %}
</div>
</div>
</form>
</div>
</div>
</div>
</div>

Expand Down
51 changes: 37 additions & 14 deletions trans/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from trans.models import User, Translation
from trans.utils.pdf import released_pdf_path, unreleased_pdf_path

from fclist import fclist, fcmatch

class FirstPage(View):
def get(self, request, *args, **kwargs):
Expand Down Expand Up @@ -48,23 +49,45 @@ class Settings(LoginRequiredMixin,View):
def get(self, request):
user = User.objects.get(username=request.user)
form = UploadFileForm()
return render(request, 'settings.html', {'form': form, 'text_font_name': user.text_font_name})
available_fonts = sorted(set([font.family for font in fclist(fontformat='TrueType')]))
return render(request, 'settings.html', {'form': form, 'text_font_name': user.text_font_name, 'available_fonts': available_fonts, 'text_family': user.text_family})

def post(self, request):
form = UploadFileForm(request.POST, request.FILES)
if not form.is_valid():
return HttpResponseBadRequest("You should attach a file")
font_file = request.FILES['uploaded_file']
if not font_file:
return HttpResponseBadRequest("You should attach a file")
import base64
text_font_base64 = base64.b64encode(font_file.read())
user = User.objects.get(username=request.user.username)
self.__remove_user_related_pdfs(user)
user.text_font_base64 = text_font_base64
user.text_font_name = font_file.name
user.save()
return HttpResponseRedirect(request.META['HTTP_REFERER'])

if 'font_upload' in request.POST:
form = UploadFileForm(request.POST, request.FILES)
if not form.is_valid():
return HttpResponseBadRequest("You should attach a file")
font_file = request.FILES['uploaded_file']
if not font_file:
return HttpResponseBadRequest("You should attach a file")
import base64
text_font_base64 = base64.b64encode(font_file.read())

self.__remove_user_related_pdfs(user)
user.text_font_base64 = text_font_base64
user.text_font_name = font_file.name
user.save()
return HttpResponseRedirect(request.META['HTTP_REFERER'])
elif 'font_select' in request.POST:
if 'text_family' not in request.POST:
return HttpResponseBadRequest("You should select a font")

if not request.POST['text_family']:
self.__remove_user_related_pdfs(user)
user.text_family = ''
user.save()
return HttpResponseRedirect(request.META['HTTP_REFERER'])

available_fonts = set([font.family for font in fclist(fontformat='TrueType')])
if request.POST['text_family'] not in available_fonts:
return HttpResponseBadRequest("Selected font not available.")
self.__remove_user_related_pdfs(user)
user.text_family = request.POST['text_family']
user.save()

return HttpResponseRedirect(request.META['HTTP_REFERER'])

def delete(self, request):
user = User.objects.get(username=request.user)
Expand Down

0 comments on commit de15a74

Please sign in to comment.