Ejemplo n.º 1
0
 def get(self, request, *args, **kwargs):
     form = forms.IdForm(request.GET)
     if form.is_valid():
         pk = form.cleaned_data["id"]
         try:
             point = get_object_or_404(MainModels.Points, pk=pk)
             person = MainModels.Person.objects.get(username=request.user)
             if MainModels.Points.objects.filter(
                     id=pk, been__id=person.id).count() > 0:
                 point.been.remove(person)
             else:
                 point.been.add(person)
             point.save()
         except:
             return JsonHTTPResponse({
                 "id":
                 pk,
                 "status":
                 1,
                 "txt":
                 "ошибка процедуры добавления 'я тут был'"
             })
         else:
             return JsonHTTPResponse({"id": pk, "status": 0, "txt": ""})
     else:
         return JsonHTTPResponse({
             "status": 1,
             "txt": "некорректно задан id места",
             "id": 0
         })
Ejemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        DEFAULT_LEVEL = 2

        errors = []

        params = request.GET
        form = forms.IdForm(params)
        if not form.is_valid():
            return JsonHTTPResponse({
                "status": 0,
                "id": 0,
                "txt": "Ожидается id места"
            })

        form = forms.AddPointForm(
            params,
            instance=MainModels.Points.objects.get(pk=form.cleaned_data['id']))
        if form.is_valid():
            point = form.save(commit=False)

            person = MainModels.Person.objects.get(username=request.user)
            point.author = person
            point.save()

            params_form = forms.ExtendedAddForm(params)
            if params_form.is_valid():

                tags = params.getlist("tags[]")
                if tags:
                    for tag in tags:
                        new_tag = TagsModels.Tags.objects.filter(name=tag)
                        if new_tag.count == 0 and tag.isdigit():
                            new_tag = TagsModels.Tags.objects.filter(id=tag)
                        if new_tag.count() == 0:
                            new_tag = TagsModels.Tags.objects.create(
                                name=tag,
                                level=DEFAULT_LEVEL,
                                author=person,
                                content_object=point)
                        else:
                            new_tag = new_tag[0]
                        point.tags.add(new_tag)

            point.save()

            # params["id"] = point.id
            return self.pointsList([point])
        else:
            e = form.errors
            for er in e:
                errors.append(er + ':' + e[er][0])
        return JsonHTTPResponse({
            "id": 0,
            "status": 1,
            "txt": ", ".join(errors)
        })
Ejemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        errors = []
        params = request.POST

        point_id = kwargs.get("id", None)
        if not point_id:
            form = forms.IdForm(params)
            if not form.is_valid():
                return JsonHTTPResponse({
                    "status": 0,
                    "id": 0,
                    "txt": "Ожидается id места"
                })
            else:
                point_id = form.cleaned_data["id"]

        form = forms.AddPointByUserForm(
            params, instance=MainModels.PointsByUser.objects.get(pk=point_id))
        if form.is_valid():
            point = form.save()

            images = params.getlist('imgs[]')
            if images:
                originalPoint = MainModels.Points.objects.get(
                    pk=point.point.id)
                for image in images:
                    try:
                        img = PhotosModels.Photos.objects.get(id=image)
                        point.imgs.add(img)
                        originalPoint.imgs.add(img)
                    except:
                        message = "ошибка добавления изображения"
                        if message not in errors: errors.append(message)
                point.save()
            return self.pointsList(
                MainModels.PointsByUser.objects.filter(id=point.id).extra(
                    **self.getPointsByUserSelect(request)))
        else:
            e = form.errors
            for er in e:
                errors.append(er + ':' + e[er][0])
        return JsonHTTPResponse({
            "id": 0,
            "status": 1,
            "txt": ", ".join(errors)
        })
Ejemplo n.º 4
0
    def post(self, request):
        form = forms.IdForm(request.POST)
        if form.is_valid():
            pk = form.cleaned_data["id"]
            point = get_object_or_404(MainModels.Points, pk=pk)

            CommentsModels.Comments.objects(content_object=point).delete()
            point.delete()
            return JsonHTTPResponse({
                "id": pk,
                "status": 1,
                "txt": "Ошибка удаления"
            })
        return JsonHTTPResponse({
            "id": 0,
            "status": 1,
            "txt": "Ошибка удаления"
        })
Ejemplo n.º 5
0
 def get(self, request, *args, **kwargs):
     form = forms.IdForm(request.GET)
     if form.is_valid():
         id = form.cleaned_data["id"]
         try:
             point = get_object_or_404(MainModels.Person, pk=id)
             person = MainModels.Person.objects.get(username=request.user)
             if MainModels.Person.objects.filter(id=id, followers__id=person.id).count() > 0:
                 point.followers.remove(person)
             else:
                 point.followers.add(person)
             point.save()
         except:
             import sys
             print sys.exc_info()
             return JsonHTTPResponse({"id": id, "status": 0, "txt": "ошибка процедуры подписи на изменения персоны"})
         else:
             return JsonHTTPResponse({"id": id, "status": 2, "txt": ""})
     else:
         return JsonHTTPResponse({"status": 0, "txt": "некорректно задан id места", "id": 0})
Ejemplo n.º 6
0
    def post(self, request, *args, **kwargs):
        errors = []
        params = request.POST

        point_id = kwargs.get("id", None)
        if not point_id:
            form = forms.IdForm(params)
            if not form.is_valid():
                return JsonHTTPResponse({
                    "status":
                    0,
                    "id":
                    0,
                    "txt":
                    "Ожидается id места для копирования"
                })
            else:
                point_id = form.cleaned_data["id"]

        originalPoint = get_object_or_404(MainModels.Points, pk=point_id)

        form = forms.AddPointByUserForm(params)
        if form.is_valid():
            person = MainModels.Person.objects.get(username=request.user)
            point = form.save(commit=False)
            point.author = person
            point.point = originalPoint
            point.save()

            images = params.getlist('imgs[]')
            if images:
                for image in images:
                    try:
                        img = PhotosModels.Photos.objects.get(id=image)
                        point.imgs.add(img)
                        originalPoint.imgs.add(img)
                    except:
                        message = "ошибка добавления изображения"
                        if message not in errors: errors.append(message)

            reviews = params.get("reviews")
            if reviews:
                reviews = json.load(reviews)
                for review in reviews:
                    try:
                        new_review = ReviewsModels.Reviews.objects.create(
                            name=review.review,
                            author=person,
                            rating=review.rating)
                        point.reviews.add(new_review)
                        originalPoint.reviews.add(new_review)
                    except:
                        message = "ошибка добавления отзыва"
                        if message not in errors: errors.append(message)

            point.save()
            originalPoint.save()
            if request.user.is_authenticated():
                isliked_select = "SELECT case when COUNT(*) > 0 then 1 else 0 end FROM main_pointsbyuser_likeusers WHERE main_pointsbyuser_likeusers.pointsbyuser_id=main_pointsbyuser.id and main_pointsbyuser_likeusers.user_id=" + str(
                    person.id)
            else:
                isliked_select = "SELECT 0"

            points = MainModels.PointsByUser.objects.filter(id=point.id).extra(
                tables=["main_points"],
                where=["main_points.id=main_pointsbyuser.point_id"],
                select={
                    'name': 'main_points.name',
                    'address': 'main_points.address',
                    'wifi': 'main_points.wifi',
                    'wc': 'main_points.wc',
                    'invalid': 'main_points.invalid',
                    'parking': 'main_points.parking',
                    'longitude': 'main_points.longitude',
                    'latitude': 'main_points.latitude',
                    "reviewusersplus":
                    "select count(*) from main_pointsbyuser_reviews join reviews_reviews on reviews_reviews.id=main_pointsbyuser_reviews.reviews_id where main_pointsbyuser_reviews.pointsbyuser_id=main_pointsbyuser.id and rating=1",
                    "reviewusersminus":
                    "select count(*) from main_pointsbyuser_reviews join reviews_reviews on reviews_reviews.id=main_pointsbyuser_reviews.reviews_id where main_pointsbyuser_reviews.pointsbyuser_id=main_pointsbyuser.id and rating=0",
                    "beens_count":
                    "select count(*) from main_points_been join main_pointsbyuser on main_points_been.points_id=main_pointsbyuser.point_id",
                    "likes_count":
                    "select count(*) from main_pointsbyuser_likeusers where main_pointsbyuser_likeusers.pointsbyuser_id=main_pointsbyuser.id",
                    "collections_count":
                    "select count(*) from collections_collections_points join main_points on collections_collections_points.points_id=main_points.id where main_points.id=main_pointsbyuser.point_id",
                    "isliked": isliked_select,
                    "id_point": "select " + str(originalPoint.id)
                })
            return self.pointsList(points)
        else:
            e = form.errors
            for er in e:
                errors.append(er + ':' + e[er][0])
        return JsonHTTPResponse({
            "id": 0,
            "status": 1,
            "txt": ", ".join(errors)
        })