Commit ce7a5548 authored by David Haynes's avatar David Haynes 🙆

Add support for selecting expiration dates

- new react lib addition
parent c668fa1c
Pipeline #3638 passed with stage
in 1 minute and 29 seconds
......@@ -7,6 +7,10 @@ import { Routes } from "Utils";
import "masonstrap/build/css/masonstrap.min.css";
import "masonstrap/build/js/masonstrap.min.js";
// Other
import "react-dates/initialize";
import "react-dates/lib/css/_datepicker.css";
ReactDOM.render(
<HashRouter>
<Routes />
......
......@@ -2,6 +2,8 @@ import React from "react";
import * as Yup from "yup";
import { Formik, Field, Form, ErrorMessage } from "formik";
import { GetCSRFToken } from "../../Utils";
import { SingleDatePicker } from "react-dates";
import moment from "moment";
const DebugCreateYup = Yup.object().shape({
destination: Yup.string()
......@@ -9,50 +11,81 @@ const DebugCreateYup = Yup.object().shape({
.max(1000, "Too Long!"),
short: Yup.string()
.required("Required")
.max(20, "Too Long!")
// expires: Yup.date()
// .nullable()
// .min(new Date(new Date().getTime() + 24 * 60 * 60 * 1000))
.max(20, "Too Long!"),
expires: Yup.date()
.nullable()
.min(new Date())
});
const DebugCreate = () => (
<div>
<Formik
initialValues={{ destination: "", short: "", expires: null }}
validationSchema={DebugCreateYup}
onSubmit={(values, { setSubmitting }) => {
fetch("/api/golinks/", {
method: "post",
headers: {
"Content-Type": "application/json",
"X-CSRFToken": GetCSRFToken()
},
body: JSON.stringify(values)
})
.then(response => console.log(response))
.then(setSubmitting(false));
}}
render={({ isSubmitting }) => (
<Form>
{"Destination: "}
<Field name="destination" placeholder="https://longwebsitelink.com" />
<ErrorMessage name="destination" component="div" />
<br />
{"Short: "}
<Field name="short" />
<ErrorMessage name="short" />
<br />
{"Expires: "}
<Field type="select" name="expires" placeholder="leave blank" />
<ErrorMessage name="expires" />
<br />
<button type="submit" disabled={isSubmitting}>
Submit
</button>
</Form>
)}
/>
</div>
);
class DebugCreate extends React.Component {
constructor(props) {
super(props);
this.state = {
focused: false,
date: null
};
}
render() {
return (
<div>
<Formik
initialValues={{
destination: "",
short: "",
expires: moment(new Date())
}}
validationSchema={DebugCreateYup}
onSubmit={(values, { setSubmitting }) => {
const newValues = {
destination: values.destination,
short: values.short,
date_expires: values.expires.format()
};
console.log(newValues);
fetch("/api/golinks/", {
method: "post",
headers: {
"Content-Type": "application/json",
"X-CSRFToken": GetCSRFToken()
},
body: JSON.stringify(newValues)
})
.then(response => console.log(response))
.then(setSubmitting(false));
}}
render={({ values, isSubmitting, setFieldValue }) => (
<Form>
{"Destination: "}
<Field
name="destination"
placeholder="https://longwebsitelink.com"
/>
<ErrorMessage name="destination" component="div" />
<br />
{"Short: "}
<Field name="short" />
<ErrorMessage name="short" />
<br />
{"Expires: "}
<SingleDatePicker
date={values["expires"]} // momentPropTypes.momentObj or null
onDateChange={date => setFieldValue("expires", date)} // PropTypes.func.isRequired
focused={this.state.focused} // PropTypes.bool
onFocusChange={({ focused }) => this.setState({ focused })} // PropTypes.func.isRequired
id="expires" // PropTypes.string.isRequired,
/>
<ErrorMessage name="expires" />
<br />
<button type="submit" disabled={isSubmitting}>
Submit
</button>
</Form>
)}
/>
</div>
);
}
}
export default DebugCreate;
......@@ -25,7 +25,7 @@ const DebugUpdate = () => (
oldshort: "",
newshort: "",
newdestination: "",
expires: null
expires: new Date()
}}
validationSchema={DebugUpdateYup}
onSubmit={(
......
# Generated by Django 2.0.10 on 2019-01-12 21:06
from django.db import migrations, models
import go_back.validators
class Migration(migrations.Migration):
dependencies = [
('go_back', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='url',
name='date_expires',
field=models.DateTimeField(blank=True, null=True, validators=[go_back.validators.valid_date], verbose_name='Go Link Expiry Date'),
),
]
......@@ -19,7 +19,7 @@ from django.conf import settings
# Other Imports
from hashids import Hashids
from rest_framework.authtoken.models import Token
from .validators import regex_short_validator, unique_short_validator
from .validators import regex_short_validator, unique_short_validator, valid_date
# Generate the salt and initialize Hashids
# Note: the Hashids library already implements several restrictions oncharacter
......@@ -86,7 +86,9 @@ class URL(models.Model):
date_created = models.DateTimeField("Go Link Creation Date", default=timezone.now)
date_expires = models.DateTimeField("Go Link Expiry Date", blank=True, null=True)
date_expires = models.DateTimeField(
"Go Link Expiry Date", blank=True, null=True, validators=[valid_date]
)
destination = models.URLField(
"Go Link Destination URL", max_length=1000, default="https://go.gmu.edu"
......
......@@ -23,9 +23,11 @@ class URLPermission(permissions.BasePermission):
message = "You do not have the necessary approvals to perform that action."
def has_permission(self, request, view):
"""Has permission to interact with URL"""
return True
def has_object_permission(self, request, view, obj):
"""Has permission to interact with a specific object"""
return obj.owner == request.user.registereduser
......
This diff is collapsed.
Markdown is supported
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