diff --git a/requirements.txt b/requirements.txt index a1e0e38b..233ec10e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,5 @@ pytz django-websocket-redis openpyxl raven -pyppeteer==0.2.5 \ No newline at end of file +pyppeteer==0.2.5 +fclist-cffi==1.1.2 \ No newline at end of file diff --git a/trans/admin.py b/trans/admin.py index 2c8fae66..dde298bb 100644 --- a/trans/admin.py +++ b/trans/admin.py @@ -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',) diff --git a/trans/templates/settings.html b/trans/templates/settings.html index f9bd619f..e64b1c9a 100644 --- a/trans/templates/settings.html +++ b/trans/templates/settings.html @@ -7,13 +7,14 @@

Settings

-
-
-
Custom Font
-
- - A true-type font can be uploaded here to be used in the translation. - +
+
Custom Font
+
+ + A true-type font can be uploaded here to be used in the translation. This supersedes all other font settings. + + +
{% if text_font_name %} @@ -35,9 +36,44 @@

Settings

{% endif %}
-
+
- +
+ + Alternatively, these fonts are already installed in Translation App. If you see a font you like, you can use it directly. + +
+ + {% csrf_token %} +
+ +
+ +
+
+ {% if text_font_name %} + + {% else %} + + {% endif %} +
+
+
+
+
diff --git a/trans/views/user.py b/trans/views/user.py index 17314c33..81eb0b36 100644 --- a/trans/views/user.py +++ b/trans/views/user.py @@ -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): @@ -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)