Fakultas Ilmu Komputer UI

Commit e2727fd1 authored by Benny William Pardede's avatar Benny William Pardede
Browse files

Merge branch '1606917550-207' into 'master'

Merge Issue 207

See merge request !286
parents 4a4ca6f5 e17215f5
Pipeline #26873 passed with stages
in 31 minutes and 11 seconds
import requests
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.core.files import File
from rest_framework import viewsets, status
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
......@@ -8,7 +9,7 @@ from rest_framework.response import Response
from core.models.accounts import Student, Supervisor
from core.serializers.accounts import UserSerializer
from core.views.sso_login import get_access_token, verify_user, get_summary_user
from .utils import determine_first_name_last_name
from .utils import determine_first_name_last_name, create_initial_profpic
class LoginViewSet(viewsets.GenericViewSet):
......@@ -81,6 +82,9 @@ class LoginViewSet(viewsets.GenericViewSet):
'program')[0].get('angkatan')
)
student.save()
student.photo.save("%s.png" % (resp.get("kodeidentitas")),
File(create_initial_profpic(first_name, last_name, resp.get("kodeidentitas"))), save=True)
student.save()
else:
supervisor = Supervisor.objects.create(
user=user,
......
import os
import random
from PIL import Image, ImageDraw, ImageFont
def determine_first_name_last_name(name):
name = name.split(" ")
if len(name) == 1:
......@@ -8,3 +14,44 @@ def determine_first_name_last_name(name):
else:
return " ".join(name[0:-1]), name[-1]
def create_initial_profpic(first_name, last_name, npm):
rand = random.Random()
rand.seed(first_name + last_name)
initial = first_name[0]
if last_name != '':
initial += last_name[0]
r = rand.randint(0, 220)
g = rand.randint(0, 220)
b = rand.randint(0, 220)
img_size = 300
pic = Image.new('RGB', (img_size, img_size), color=(r, g, b))
for ttf in ["arial.ttf", "calibri.ttf", "Roboto-Regular.ttf", "FreeMono.ttf", "FreeSans.ttf", "OpenSans-Regular.ttf"]:
try:
font = ImageFont.truetype(ttf, img_size // 2)
break
except OSError:
font = None
continue
drawer = ImageDraw.Draw(pic)
if font is not None:
text_width = font.getsize(initial)[0]
text_height = font.getsize(initial)[1]
text_coord = (img_size / 2 - text_width / 2, img_size / 2 - text_height / 1.75)
drawer.text(text_coord, initial, font=font, fill=(255, 255, 255), align="center")
else:
drawer.text((img_size//4, img_size//2), ("%s %s" % (first_name, last_name)), fill=(255, 255, 255), align="center")
img_path = "%s.png" % npm
pic.save(img_path)
img_file = open(img_path, 'rb')
os.remove(img_path)
return img_file
......@@ -8,6 +8,7 @@ django-webpack-loader>=0.4,<1
djangorestframework>=3,<4
gunicorn
packaging>=19,<20
Pillow
psycopg2-binary>=2,<3
pyparsing>=2,<3
requests
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment