Fakultas Ilmu Komputer UI

Verified Commit c7f85f91 authored by Muhammad Ariq Basyar's avatar Muhammad Ariq Basyar
Browse files

[GREEN] implemented new read only field on bisagouser and profile picture of komentar's creator

- new field: 'hidden_fields_verbose', 'can_see_hidden_fields', and 'hidden_fields_color'
parent 4eedf091
Pipeline #81218 passed with stages
in 5 minutes and 39 seconds
......@@ -271,7 +271,13 @@ def list_komentar(request, place_id, id):
komentar_details["deskripsi"] = komentar.deskripsi
komentar_details["creator"] = komentar.user.last_name
komentar_details["creator_email"] = komentar.user.email
komentar_details["creator_picture"] = request.build_absolute_uri(komentar.user.bisagouser.foto.url)
foto = komentar.user.bisagouser.foto
try:
url = foto.url
except ValueError:
url = None
komentar_details["creator_picture"] =\
request.build_absolute_uri(url) if url else None
komentar_details["created"] = komentar.created.strftime(
TIME_FORMAT)
return JsonResponse(return_json, status=HTTPStatus.OK)
......
from rest_framework import serializers
from colour import Color
from django.contrib.auth.models import User
from django.utils.regex_helper import _lazy_re_compile
......@@ -15,13 +16,17 @@ class BisaGoUserSerializer(serializers.ModelSerializer):
input_formats=['%d %B %Y', '%Y-%m-%d'])
hidden_fields = serializers.SerializerMethodField()
hidden_fields_verbose = serializers.SerializerMethodField()
can_see_hidden_fields = serializers.SerializerMethodField()
hidden_fields_color = serializers.SerializerMethodField()
class Meta:
model = BisaGoUser
fields = (
'phone_number', 'tanggal_lahir', 'jenis_kelamin', 'name', 'email',
'disabilitas', 'pekerjaan', 'alamat', 'foto', 'seen', 'username',
'organisasi_komunitas', 'hidden_fields',
'organisasi_komunitas', 'hidden_fields', 'hidden_fields_verbose',
'can_see_hidden_fields', 'hidden_fields_color'
)
hidden_fields = ('phone_number', 'email', 'alamat', 'tanggal_lahir')
......@@ -44,18 +49,27 @@ class BisaGoUserSerializer(serializers.ModelSerializer):
update_user_fields = ('last_name',)
update_user_fields_mapper = {}
def get_hidden_fields(self, *args):
def get_hidden_fields(self, *_):
return self.Meta.hidden_fields
def get_hidden_fields_verbose(self, *_):
hidden_fields = self.Meta.hidden_fields
verbose = self.Meta.hidden_fields_verbose
return [verbose.get(field) or field for field in hidden_fields]
def can_see_hidden_fields(self, instance):
def get_can_see_hidden_fields(self, instance):
return self._can_see_hidden_fields(instance)
def get_hidden_fields_color(self, *_):
return Color('#808080').hex_l
def _can_see_hidden_fields(self, instance):
request = self.context['request']
return request.user.id == instance.user.id or instance.seen
def to_representation(self, instance):
representation = super().to_representation(instance)
if not self.can_see_hidden_fields(instance):
if not self._can_see_hidden_fields(instance):
for hidden_field in self.Meta.hidden_fields:
verbose_name = self.Meta.hidden_fields_verbose.get(hidden_field)\
or hidden_field
......
......@@ -74,3 +74,4 @@ pyfcm==1.4.9
django-mock-queries==2.1.6
mock==4.0.3
model-bakery==1.1.1
colour==0.1.5
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