Esempio n. 1
0
File: api.py Progetto: marved/tfg
    def create(self, request, *args, **kwargs):
        university_name = request.data.get("name", None)
        city = request.data.get("city", None)
        city_id = city.get("pk", None)
        try:
            city = City.objects.get(pk=int(city_id))
        except:
            return http_400_bad_request(INVALID_CREDENTIALS_ERROR_MSG)
        university, created = University.objects.get_or_create(name=university_name, city=city)
        if created:
            latLng = getLatLngData(university.name, university.city.name)
            university.latitude = latLng['lat']
            university.longitude = latLng['lng']
            university.save()

        context = {"request": request}
        serializer = UniversitySerializer(university, context=context)
        if created:
            return http_201_created(serializer.data)
        return http_200_ok(serializer.data)
Esempio n. 2
0
File: views.py Progetto: marved/tfg
    def get(self, request, **kwargs):

        university_id = kwargs.get("id", None)
        if university_id:
            try:
                university = University.objects.get(pk=university_id)
            except:
                return render(request, "404.html")

            if university.latitude == None or university.longitude == None:
                latLng = getLatLngData(university.name, university.city.name)
                university.latitude = latLng['lat']
                university.longitude = latLng['lng']
                university.save()

            subjects = Subject.objects.all().filter(university=university_id).order_by('name')
            context = {
                'university': university,
                'subjects': subjects
            }
            return render(request, "pages/university-detail.html", context)

        else:
            return render(request, "404.html")