Commit 61887e05 authored by Florent Chehab's avatar Florent Chehab
Browse files

endpoints updated for main campus

parent 39210901
Pipeline #26558 passed with stages
in 3 minutes and 11 seconds
from .university import University # noqa: F401 from .university import University # noqa: F401
from .campus import Campus, CampusViewSet, CampusSerializer # noqa: F401 from .campus import Campus, CampusViewSet, CampusSerializer, MainCampusViewSet # noqa: F401
from .universityAPI import UniversitySerializer, UniversityViewSet # noqa: F401 from .universityAPI import UniversitySerializer, UniversityViewSet # noqa: F401
from .universityScholarship import UniversityScholarship, UniversityScholarshipSerializer, UniversityScholarshipViewSet # noqa: F401 from .universityScholarship import UniversityScholarship, UniversityScholarshipSerializer, UniversityScholarshipViewSet # noqa: F401
......
...@@ -46,3 +46,8 @@ class CampusViewSet(BasicModuleViewSet): ...@@ -46,3 +46,8 @@ class CampusViewSet(BasicModuleViewSet):
queryset = Campus.objects.all().prefetch_related( queryset = Campus.objects.all().prefetch_related(
'updated_by') # pylint: disable=E1101 'updated_by') # pylint: disable=E1101
serializer_class = CampusSerializer serializer_class = CampusSerializer
class MainCampusViewSet(BasicModuleViewSet):
queryset = Campus.objects.filter(is_main_campus=True)
serializer_class = CampusSerializer
from backend.models.university import University from backend.models.university import University
from rest_framework import permissions from rest_framework import permissions
from backend.models.university.campus import CampusSerializer
from backend.models.tools import NoDeleteIfNotAdmin from backend.models.tools import NoDeleteIfNotAdmin
from backend.models.my_model import MyModelSerializer, MyModelViewSet from backend.models.my_model import MyModelSerializer, MyModelViewSet
class UniversitySerializer(MyModelSerializer): class UniversitySerializer(MyModelSerializer):
univ_campus = CampusSerializer(many=True, read_only=True)
class Meta: class Meta:
model = University model = University
...@@ -20,5 +18,4 @@ class UniversityViewSet(MyModelViewSet): ...@@ -20,5 +18,4 @@ class UniversityViewSet(MyModelViewSet):
permissions.IsAuthenticated, permissions.IsAuthenticated,
NoDeleteIfNotAdmin, NoDeleteIfNotAdmin,
) )
queryset = University.objects.all()\ queryset = University.objects.all() # pylint: disable=E1101
.prefetch_related('univ_campus', 'univ_campus__updated_by') # pylint: disable=E1101
...@@ -28,7 +28,7 @@ from backend.models.location import CityOtherStuffViewSet ...@@ -28,7 +28,7 @@ from backend.models.location import CityOtherStuffViewSet
from backend.models.location import CityPhotoViewSet from backend.models.location import CityPhotoViewSet
from backend.models.university import UniversityViewSet from backend.models.university import UniversityViewSet
from backend.models.university import CampusViewSet from backend.models.university import CampusViewSet, MainCampusViewSet
from backend.models.university import UniversityScholarshipViewSet from backend.models.university import UniversityScholarshipViewSet
from backend.models.university import UniversityInfoViewSet from backend.models.university import UniversityInfoViewSet
from backend.models.university import UniversitySemestersDatesViewSet from backend.models.university import UniversitySemestersDatesViewSet
...@@ -74,6 +74,7 @@ if settings.TESTING: ...@@ -74,6 +74,7 @@ if settings.TESTING:
router.register(r'country', CountryViewSet) router.register(r'country', CountryViewSet)
router.register(r'city', CityViewSet) router.register(r'city', CityViewSet)
router.register(r'campus', CampusViewSet) router.register(r'campus', CampusViewSet)
router.register(r'main_campus', MainCampusViewSet)
router.register(r'university', UniversityViewSet) router.register(r'university', UniversityViewSet)
......
...@@ -40,6 +40,9 @@ contexts = [ ...@@ -40,6 +40,9 @@ contexts = [
}, { }, {
'NAME': 'COUNTRIES', 'NAME': 'COUNTRIES',
'name': 'countries' 'name': 'countries'
}, {
'NAME': 'MAIN_CAMPUS',
'name': 'mainCampus'
} }
] ]
......
...@@ -5,7 +5,9 @@ import { Map, TileLayer, Marker, Popup, LayersControl, FeatureGroup, Circle, Lay ...@@ -5,7 +5,9 @@ import { Map, TileLayer, Marker, Popup, LayersControl, FeatureGroup, Circle, Lay
// import MarkerClusterGroup from 'react-leaflet-markercluster'; // import MarkerClusterGroup from 'react-leaflet-markercluster';
import { import {
universitiesFetchData, universitiesFetchData,
universitiesInvalidated universitiesInvalidated,
mainCampusFetchData,
mainCampusInvalidated
} from '../../generated/actions'; } from '../../generated/actions';
...@@ -13,16 +15,21 @@ import { ...@@ -13,16 +15,21 @@ import {
class UnivMap extends Component { class UnivMap extends Component {
componentDidMount() { componentDidMount() {
if (this.props.universities.length == 0 || this.props.invalidated) { if (this.props.universities.length == 0 || this.props.invalidated) {
console.log("here mother uc", this.props.universities); this.props.fetchUniversitiesData('http://127.0.0.1:8000/api/university');
this.props.fetchData('http://127.0.0.1:8000/api/university'); }
if (this.props.mainCampus.length == 0 || this.props.invalidated) {
this.props.fetchMainCampusData('http://127.0.0.1:8000/api/main_campus');
} }
} }
componentDidUpdate() { componentDidUpdate() {
// TODO ajouter expire date // TODO ajouter expire date
if (this.props.invalidated) { if (this.props.universitiesInvalidated) {
this.props.fetchData('http://127.0.0.1:8000/api/university'); this.props.fetchUniversitiesData('http://127.0.0.1:8000/api/university');
}
if (this.props.mainCampusInvalidated) {
this.props.fetchMainCampusData('http://127.0.0.1:8000/api/main_campus');
} }
} }
...@@ -31,24 +38,32 @@ class UnivMap extends Component { ...@@ -31,24 +38,32 @@ class UnivMap extends Component {
} }
render() { render() {
if (this.props.hasError) { if (this.props.universitiesHasError || this.props.mainCampusHasError) {
return <p>Sorry! There was an error loading the items</p>; return <p>Sorry! There was an error loading the items</p>;
} }
if (this.props.isLoading || this.props.invalidated) { if (this.props.universitiesIsLoading || this.props.universitiesInvalidated) {
return <Loading />; return <Loading />;
} }
if (this.props.mainCampusIsLoading || this.props.mainCampusInvalidated) {
return <Loading />;
}
let universities = this.props.universities; let universities = this.props.universities;
let mainCampus = this.props.mainCampus;
console.log(universities, mainCampus)
let univ_to_plot = []; let univ_to_plot = [];
for (let univ_pk in universities) { for (let main_campus_pk in mainCampus) {
let univ = universities[univ_pk]; let campus = mainCampus[main_campus_pk]
if (univ.univ_campus.length > 0) { let univ = universities[campus.university]
univ_to_plot.push({ univ_to_plot.push({
name: univ.name, name: univ.name,
main_campus: univ.univ_campus[0] main_campus: campus
}) })
} }
}
return ( return (
<Map center={[35, 0]} zoom={2} style={{ height: "800px" }}> <Map center={[35, 0]} zoom={2} style={{ height: "800px" }}>
...@@ -108,17 +123,25 @@ class UnivMap extends Component { ...@@ -108,17 +123,25 @@ class UnivMap extends Component {
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
universities: state.universitiesFetched.universities, universities: state.universitiesFetched.universities,
fetchedAt: state.universitiesFetched.fetchedAt, universitiesFetchedAt: state.universitiesFetched.fetchedAt,
hasError: state.universitiesHasError, universitiesHasError: state.universitiesHasError,
isLoading: state.universitiesIsLoading, universitiesIsLoading: state.universitiesIsLoading,
invalidated: state.universitiesInvalidated universitiesInvalidated: state.universitiesInvalidated,
mainCampus: state.mainCampusFetched.mainCampus,
mainCampusFetchedAt: state.mainCampusFetched.fetchedAt,
mainCampusHasError: state.mainCampusHasError,
mainCampusIsLoading: state.mainCampusIsLoading,
mainCampusInvalidated: state.mainCampusInvalidated,
}; };
}; };
const mapDispatchToProps = (dispatch) => { const mapDispatchToProps = (dispatch) => {
return { return {
fetchData: (url) => dispatch(universitiesFetchData(url)), fetchUniversitiesData: (url) => dispatch(universitiesFetchData(url)),
invalidate: () => dispatch(universitiesInvalidated(true)) fetchMainCampusData: (url) => dispatch(mainCampusFetchData(url)),
// invalidateUniversities: (bool) => dispatch(universitiesInvalidated(bool)),
// invalidateMainCampus: (bool) => dispatch(mainCampusInvalidated(bool))
}; };
}; };
......
...@@ -5,10 +5,16 @@ import { ...@@ -5,10 +5,16 @@ import {
universitiesHasError, universitiesHasError,
universitiesIsLoading, universitiesIsLoading,
universitiesInvalidated, universitiesInvalidated,
countriesFetched, countriesFetched,
countriesHasError, countriesHasError,
countriesIsLoading, countriesIsLoading,
countriesInvalidated countriesInvalidated,
mainCampusFetched,
mainCampusHasError,
mainCampusIsLoading,
mainCampusInvalidated,
} from '../generated/reducers'; } from '../generated/reducers';
...@@ -17,10 +23,16 @@ const rootReducer = combineReducers({ ...@@ -17,10 +23,16 @@ const rootReducer = combineReducers({
universitiesHasError, universitiesHasError,
universitiesIsLoading, universitiesIsLoading,
universitiesInvalidated, universitiesInvalidated,
countriesFetched, countriesFetched,
countriesHasError, countriesHasError,
countriesIsLoading, countriesIsLoading,
countriesInvalidated countriesInvalidated,
mainCampusFetched,
mainCampusHasError,
mainCampusIsLoading,
mainCampusInvalidated,
}) })
export default rootReducer export default rootReducer
......
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