Calculate instructor name correctly

This commit is contained in:
Tanner Collin 2020-01-15 07:51:00 +00:00
parent 200df3fdc8
commit 8a8161c976
4 changed files with 24 additions and 25 deletions

View File

@ -6,7 +6,7 @@ from django.utils.timezone import now
from . import old_models
class Member(models.Model):
user = models.OneToOneField(User, blank=True, null=True, on_delete=models.SET_NULL)
user = models.OneToOneField(User, related_name='member', blank=True, null=True, on_delete=models.SET_NULL)
old_email = models.CharField(max_length=254, blank=True, null=True)
photo_large = models.CharField(max_length=64, blank=True, null=True)
photo_medium = models.CharField(max_length=64, blank=True, null=True)
@ -69,6 +69,7 @@ class Session(models.Model):
instructor = models.ForeignKey(User, related_name='teaching', blank=True, null=True, on_delete=models.SET_NULL)
course = models.ForeignKey(Course, related_name='sessions', blank=True, null=True, on_delete=models.SET_NULL)
is_cancelled = models.BooleanField(default=False)
old_instructor = models.TextField(blank=True, null=True)
datetime = models.DateTimeField(blank=True, null=True)
cost = models.DecimalField(max_digits=5, decimal_places=2)

View File

@ -225,19 +225,27 @@ class TransactionSerializer(serializers.ModelSerializer):
fields = '__all__'
class SessionDetailSerializer(serializers.ModelSerializer):
class Meta:
model = models.Training
exclude = ['user']
class SessionSerializer(serializers.ModelSerializer):
student_count = serializers.SerializerMethodField()
course_name = serializers.SerializerMethodField()
instructor_name = serializers.SerializerMethodField()
datetime = serializers.DateTimeField()
instructor = serializers.PrimaryKeyRelatedField(queryset=models.User.objects.all())
course = serializers.PrimaryKeyRelatedField(queryset=models.Course.objects.all())
class Meta:
model = models.Session
fields = '__all__'
depth = 1
def get_student_count(self, obj):
return len(obj.students.all())
def get_course_name(self, obj):
return obj.course.name
def get_instructor_name(self, obj):
if obj.instructor and hasattr(obj.instructor, 'member'):
name = '{} {}'.format(obj.instructor.member.preferred_name, obj.instructor.member.last_name[0])
else:
name = 'Unknown'
return obj.old_instructor or name
class SessionListSerializer(SessionSerializer):
class Meta:
@ -245,6 +253,7 @@ class SessionListSerializer(SessionSerializer):
fields = '__all__'
class CourseSerializer(serializers.ModelSerializer):
name = serializers.CharField()
class Meta:

View File

@ -140,8 +140,9 @@ class CourseViewSet(Base, List, Retrieve, Create, Update):
return serializers.CourseDetailSerializer
class SessionViewSet(viewsets.ModelViewSet):
permission_classes = [AllowMetadata | IsAuthenticated]
class SessionViewSet(Base, List, Retrieve, Create, Update):
permission_classes = [AllowMetadata | IsAuthenticated, IsAdminOrReadOnly | IsInstructorOrReadOnly]
serializer_class = serializers.SessionSerializer
def get_queryset(self):
if self.action == 'list':
@ -149,12 +150,6 @@ class SessionViewSet(viewsets.ModelViewSet):
else:
return models.Session.objects.all()
def get_serializer_class(self):
#if self.action == 'retrieve':
# return serializers.CourseDetailSerializer
#else:
return serializers.SessionSerializer
class UserView(views.APIView):
permission_classes = [AllowMetadata | IsAuthenticated]

View File

@ -9,9 +9,6 @@ import { NotFound, PleaseLogin } from './Misc.js';
function ClassTable(props) {
const { classes } = props;
const getInstructor = (session) =>
session.instructor ? session.instructor.first_name : session.old_instructor;
return (
<Table basic='very'>
<Table.Header>
@ -29,14 +26,14 @@ function ClassTable(props) {
{classes.length ?
classes.map((x, i) =>
<Table.Row key={i}>
<Table.Cell>{x.course.name}</Table.Cell>
<Table.Cell>{x.course_name}</Table.Cell>
<Table.Cell>
<Link to={'/classes/'+x.id}>
{moment.utc(x.datetime).format('ll')}
</Link>
</Table.Cell>
<Table.Cell>{moment.utc(x.datetime).format('LT')}</Table.Cell>
<Table.Cell>{getInstructor(x)}</Table.Cell>
<Table.Cell>{x.instructor_name}</Table.Cell>
<Table.Cell>{x.cost === '0.00' ? 'Free' : '$'+x.cost}</Table.Cell>
<Table.Cell>{x.student_count}</Table.Cell>
</Table.Row>
@ -103,9 +100,6 @@ export function ClassDetail(props) {
});
}, []);
const getInstructor = (session) =>
session.instructor ? session.instructor.first_name : session.old_instructor;
return (
<Container>
{!error ?
@ -118,8 +112,8 @@ export function ClassDetail(props) {
<Table.Row>
<Table.Cell>Name:</Table.Cell>
<Table.Cell>
<Link to={'/courses/'+clazz.course.id}>
{clazz.course.name}
<Link to={'/courses/'+clazz.course}>
{clazz.course_name}
</Link>
</Table.Cell>
</Table.Row>
@ -137,7 +131,7 @@ export function ClassDetail(props) {
</Table.Row>
<Table.Row>
<Table.Cell>Instructor:</Table.Cell>
<Table.Cell>{getInstructor(clazz)}</Table.Cell>
<Table.Cell>{clazz.instructor_name}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>Cost:</Table.Cell>