Convert Card last_seen_at Date to last_seen DateTime

This commit is contained in:
Tanner Collin 2021-11-14 03:49:18 +00:00
parent e511723c07
commit 42ad1ac327
8 changed files with 69 additions and 14 deletions

View File

@ -105,9 +105,10 @@ class Card(models.Model):
card_number = models.CharField(unique=True, max_length=16, blank=True, null=True) card_number = models.CharField(unique=True, max_length=16, blank=True, null=True)
notes = models.TextField(blank=True, null=True) notes = models.TextField(blank=True, null=True)
last_seen_at = models.DateField(blank=True, null=True) last_seen_at = models.DateField(blank=True, null=True)
last_seen = models.DateTimeField(blank=True, null=True)
active_status = models.CharField(max_length=32, blank=True, null=True) active_status = models.CharField(max_length=32, blank=True, null=True)
history = HistoricalRecords(excluded_fields=['last_seen_at']) history = HistoricalRecords(excluded_fields=['last_seen_at', 'last_seen'])
class Course(models.Model): class Course(models.Model):
name = models.TextField(blank=True, null=True) name = models.TextField(blank=True, null=True)

View File

@ -73,7 +73,6 @@ class TransactionSerializer(serializers.ModelSerializer):
fields = '__all__' fields = '__all__'
read_only_fields = [ read_only_fields = [
'id', 'id',
'last_seen_at',
'user', 'user',
'recorder', 'recorder',
'paypal_txn_id', 'paypal_txn_id',
@ -354,7 +353,7 @@ class AdminSearchSerializer(serializers.Serializer):
queryset = obj.user.cards queryset = obj.user.cards
else: else:
queryset = models.Card.objects.filter(member_id=obj.id) queryset = models.Card.objects.filter(member_id=obj.id)
queryset = queryset.order_by('-last_seen_at') queryset = queryset.order_by('-last_seen')
serializer = CardSerializer(data=queryset, many=True) serializer = CardSerializer(data=queryset, many=True)
serializer.is_valid() serializer.is_valid()
return serializer.data return serializer.data
@ -406,7 +405,7 @@ class CardSerializer(serializers.ModelSerializer):
fields = '__all__' fields = '__all__'
read_only_fields = [ read_only_fields = [
'id', 'id',
'last_seen_at', 'last_seen',
'user', 'user',
] ]

View File

@ -9,8 +9,9 @@ from django.utils.timezone import now, pytz
from apiserver.api import models from apiserver.api import models
from apiserver import secrets from apiserver import secrets
tz = pytz.timezone('America/Edmonton')
def today_alberta_tz(): def today_alberta_tz():
return datetime.now(pytz.timezone('America/Edmonton')).date() return datetime.now(tz).date()
DEFAULTS = { DEFAULTS = {
'last_card_change': time.time(), 'last_card_change': time.time(),
@ -140,13 +141,16 @@ def check_mumble_server():
def calc_card_scans(): def calc_card_scans():
date = today_alberta_tz() date = today_alberta_tz()
dt = datetime.combine(date, datetime.min.time())
midnight = tz.localize(dt)
cards = models.Card.objects cards = models.Card.objects
count = cards.filter(last_seen_at=date).count() count = cards.filter(last_seen__gte=midnight).count()
cache.set('card_scans', count) cache.set('card_scans', count)
models.StatsSpaceActivity.objects.update_or_create( models.StatsSpaceActivity.objects.update_or_create(
date=today_alberta_tz(), date=date,
defaults=dict(card_scans=count), defaults=dict(card_scans=count),
) )

View File

@ -122,7 +122,7 @@ class SearchViewSet(Base, Retrieve):
elif self.action == 'create' and sort == 'last_scanned': elif self.action == 'create' and sort == 'last_scanned':
if self.request.user.member.allow_last_scanned: if self.request.user.member.allow_last_scanned:
queryset = queryset.filter(allow_last_scanned=True) queryset = queryset.filter(allow_last_scanned=True)
queryset = queryset.order_by('-user__cards__last_seen_at') queryset = queryset.order_by('-user__cards__last_seen')
else: else:
queryset = [] queryset = []
elif self.action == 'create' and sort == 'best_looking': elif self.action == 'create' and sort == 'best_looking':
@ -465,7 +465,7 @@ class DoorViewSet(viewsets.ViewSet, List):
@action(detail=True, methods=['post']) @action(detail=True, methods=['post'])
def seen(self, request, pk=None): def seen(self, request, pk=None):
card = get_object_or_404(models.Card, card_number=pk) card = get_object_or_404(models.Card, card_number=pk)
card.last_seen_at = utils.today_alberta_tz() card.last_seen = now()
card.save() card.save()
try: try:

24
apiserver/convert_card_seen.py Executable file
View File

@ -0,0 +1,24 @@
import django, sys, os
os.environ['DJANGO_SETTINGS_MODULE'] = 'apiserver.settings'
django.setup()
from datetime import datetime
import json
import pytz
from apiserver.api import models, utils
tz = pytz.timezone('America/Edmonton')
cards = models.Card.objects.order_by('last_seen_at')
for card in cards:
seen = card.last_seen_at
if seen:
t = datetime.combine(seen, datetime.min.time())
card.last_seen = tz.localize(t)
card.save()
print('card', card.card_number, 'date', seen, '-->', card.last_seen)
print('Done.')

View File

@ -103,8 +103,16 @@ function AdminCardDetail(props) {
</Form.Group> </Form.Group>
</Form.Group> </Form.Group>
Notes: {input.notes || 'None'}, Notes: {input.notes || 'None'}<br />
Last Seen: {input.last_seen_at || 'Unknown'} Last Seen:{' '}
{input.last_seen ?
input.last_seen > '2021-11-14T02:01:35.415685Z' ?
moment.utc(input.last_seen).tz('America/Edmonton').format('lll')
:
moment.utc(input.last_seen).tz('America/Edmonton').format('ll')
:
'Unknown'
}
</Form> </Form>
</Segment> </Segment>
: :

View File

@ -1,6 +1,7 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { BrowserRouter as Router, Switch, Route, Link, useParams } from 'react-router-dom'; import { BrowserRouter as Router, Switch, Route, Link, useParams } from 'react-router-dom';
import './light.css'; import './light.css';
import moment from 'moment-timezone';
import { Button, Container, Divider, Dropdown, Form, Grid, Header, Icon, Image, Menu, Message, Segment, Table } from 'semantic-ui-react'; import { Button, Container, Divider, Dropdown, Form, Grid, Header, Icon, Image, Menu, Message, Segment, Table } from 'semantic-ui-react';
import { BasicTable, requester, staticUrl } from './utils.js'; import { BasicTable, requester, staticUrl } from './utils.js';
import { NotFound, PleaseLogin } from './Misc.js'; import { NotFound, PleaseLogin } from './Misc.js';
@ -45,7 +46,16 @@ export function Cards(props) {
<Table.Row key={x.id}> <Table.Row key={x.id}>
<Table.Cell>{x.card_number}</Table.Cell> <Table.Cell>{x.card_number}</Table.Cell>
<Table.Cell>{x.notes}</Table.Cell> <Table.Cell>{x.notes}</Table.Cell>
<Table.Cell>{x.last_seen_at}</Table.Cell> <Table.Cell>
{x.last_seen ?
x.last_seen > '2021-11-14T02:01:35.415685Z' ?
moment.utc(x.last_seen).tz('America/Edmonton').format('lll')
:
moment.utc(x.last_seen).tz('America/Edmonton').format('ll')
:
'Unknown'
}
</Table.Cell>
<Table.Cell>{cardStatus(x)}</Table.Cell> <Table.Cell>{cardStatus(x)}</Table.Cell>
</Table.Row> </Table.Row>
)} )}
@ -64,7 +74,16 @@ export function Cards(props) {
</Table.Row> </Table.Row>
<Table.Row> <Table.Row>
<Table.Cell>Last Seen:</Table.Cell> <Table.Cell>Last Seen:</Table.Cell>
<Table.Cell>{card.last_seen_at}</Table.Cell> <Table.Cell>
{card.last_seen ?
card.last_seen > '2021-11-14T02:01:35.415685Z' ?
moment.utc(card.last_seen).tz('America/Edmonton').format('lll')
:
moment.utc(card.last_seen).tz('America/Edmonton').format('ll')
:
'Unknown'
}
</Table.Cell>
</Table.Row> </Table.Row>
<Table.Row> <Table.Row>
<Table.Cell>Active:</Table.Cell> <Table.Cell>Active:</Table.Cell>

View File

@ -14,7 +14,7 @@ function MemberInfo(props) {
const member = user.member; const member = user.member;
const lastTrans = user.transactions && user.transactions.slice(0,3); const lastTrans = user.transactions && user.transactions.slice(0,3);
const lastCard = user.cards && user.cards.sort((a, b) => a.last_seen_at < b.last_seen_at)[0]; const lastCard = user.cards && user.cards.sort((a, b) => a.last_seen < b.last_seen)[0];
return ( return (
<div> <div>