Add input box for sending messages to the sign

master
Tanner Collin 2 years ago
parent e501427f38
commit 7b5dac7c6e
  1. 1
      apiserver/apiserver/api/utils_stats.py
  2. 17
      apiserver/apiserver/api/views.py
  3. 1
      apiserver/apiserver/secrets.py.example
  4. 53
      webclient/src/Home.js

@ -26,6 +26,7 @@ DEFAULTS = {
'card_scans': 0,
'track': {},
'alarm': {},
'sign': '',
}
if secrets.MUMBLE:

@ -569,6 +569,23 @@ class StatsViewSet(viewsets.ViewSet, List):
except KeyError:
raise exceptions.ValidationError(dict(data='This field is required.'))
@action(detail=False, methods=['post'])
def sign(self, request):
try:
sign = request.data['sign']
cache.set('sign', sign)
try:
post_data = dict(access_token=secrets.SIGN_TOKEN, args=sign)
r = requests.post('https://api.particle.io/v1/devices/200042000647343232363230/text/', data=post_data, timeout=5)
r.raise_for_status()
except:
raise exceptions.ValidationError(dict(sign='Something went wrong :('))
return Response(200)
except KeyError:
raise exceptions.ValidationError(dict(sign='This field is required.'))
@action(detail=False, methods=['post'])
def alarm(self, request):
try:

@ -68,6 +68,7 @@ DOOR_CODE = ''
WIFI_PASS = ''
MINECRAFT = ''
MUMBLE = ''
SIGN_TOKEN = ''
# Portal Email Credentials
# For sending password resets, etc.

@ -138,6 +138,53 @@ function MemberInfo(props) {
);
};
function SignForm(props) {
const { token } = props;
const [error, setError] = useState({});
const [sign, setSign] = useState('');
const [loading, setLoading] = useState(false);
const [success, setSuccess] = useState(false);
const handleValues = (e, v) => setSign(v.value);
const handleChange = (e) => handleValues(e, e.currentTarget);
const handleSubmit = (e) => {
if (loading) return;
setLoading(true);
const data = {sign: sign};
requester('/stats/sign/', 'POST', token, data)
.then(res => {
setLoading(false);
setSuccess(true);
setError({});
setSign('');
})
.catch(err => {
setLoading(false);
console.log(err);
setError(err.data);
});
};
return (
<Form onSubmit={handleSubmit}>
<p>Send a message to the sign:</p>
<Form.Input
name='sign'
onChange={handleChange}
value={sign}
error={error.sign}
/>
{!!token &&<Form.Button loading={loading} error={error.non_field_errors}>
Submit
</Form.Button>}
{success && <div>Success!</div>}
</Form>
);
};
export function Home(props) {
const { user, token } = props;
const [stats, setStats] = useState(JSON.parse(localStorage.getItem('stats', 'false')));
@ -213,9 +260,6 @@ export function Home(props) {
</Grid.Column>
<Grid.Column>
<Segment>
<Header size='medium'>Home</Header>
<p>Welcome to the Protospace member portal! Here you can view member info, join classes, and manage your membership.</p>
<Header size='medium'>Quick Links</Header>
<p><a href='http://protospace.ca/' target='_blank' rel='noopener noreferrer'>Main Website</a></p>
<p><a href='http://wiki.protospace.ca/Welcome_to_Protospace' target='_blank' rel='noopener noreferrer'>Protospace Wiki</a> <Link to='/auth/wiki'>[register]</Link></p>
@ -306,7 +350,10 @@ export function Home(props) {
{user && <p>Alarm status: {alarmStat()}{doorOpenStat()}</p>}
</div>
<SignForm token={token} />
</Segment>
</Grid.Column>
</Grid>
</Container>

Loading…
Cancel
Save