Fix usages bug when member's not a Spaceport user

This commit is contained in:
Tanner Collin 2021-04-16 01:14:01 +00:00
parent 74dfdc6c06
commit a0c9c52475
2 changed files with 8 additions and 3 deletions

View File

@ -157,9 +157,11 @@ class StatsSpaceActivity(models.Model):
class UsageTrack(models.Model):
user = models.ForeignKey(User, related_name='usages', blank=True, null=True, on_delete=models.SET_NULL)
username = models.CharField(max_length=64)
devicename = models.CharField(max_length=64)
# member_id = models.IntegerField(blank=True, null=True) # restrict to current users for now
username = models.CharField(max_length=64) # allows us to match non-Spaceport users later
devicename = models.CharField(max_length=64)
start_time = models.DateTimeField(auto_now_add=True)
num_seconds = models.IntegerField()

View File

@ -303,7 +303,10 @@ class AdminSearchSerializer(serializers.Serializer):
return serializer.data
def get_usages(self, obj):
queryset = obj.user.usages.order_by('-start_time')
if obj.user:
queryset = obj.user.usages.order_by('-start_time')
else:
queryset = []
serializer = UsageSerializer(data=queryset, many=True)
serializer.is_valid()
return serializer.data