示例#1
0
    def interest(request, entity='news', entity_id=None):
        '''
        :param request:
        :param entity:
        :param entity_id:
        :return:
            GET mode
                If request's user exists, the api will return all user interested in the entity specified
                If request is from an anonymous users, just the number of interested people is will be returned
            POST mode(only for logged)
                Toggle the interest for the specified entity for the logged user
        '''
        results = {}
        local_entity = None
        try:
            #logged user
            profile = request.user.profile
            try:
                local_entity = ModelHelper.find_this_entity(entity, entity_id)
            except ObjectDoesNotExist as odne:
                return not_found()
            if request.method == 'GET':
                results['iaminterested'] = profile.is_this_interested_by_me(
                    local_entity)
                results['interested'] = ProfileSerializer(
                    local_entity.interested(), many=True).data
                results['interested_counter'] = len(local_entity.interested())

            else:
                # Toggle interest
                results['iaminterested'] = profile.interest_this(local_entity)
                results['interested'] = ProfileSerializer(
                    local_entity.interested(), many=True).data
                results['interested_counter'] = len(local_entity.interested())
            return JsonResponse({
                'status': 'ok',
                'result': results,
            },
                                status=200)
        except Exception as e:
            #Anonymous user
            if request.method == 'GET':
                local_entity = ModelHelper.find_this_entity(entity, entity_id)
                results['interested_counter'] = len(local_entity.interested())
                return JsonResponse({
                    'status': 'ok',
                    'result': results,
                },
                                    status=202)
            else:
                return not_authorized()
示例#2
0
 def bookmark(request, entity='news', entity_id=None):
     # GET return status of a bookmark (ES: {bookmarked:true|false})
     # POST toggle status of a bookmark an return it (ES: {bookmarked:true|false})
     results = {}
     try:
         local_entity = None
         profile = request.user.profile
         try:
             local_entity = ModelHelper.find_this_entity(entity, entity_id)
         except ObjectDoesNotExist as odne:
             return not_found()
         if request.method == 'POST':
             results['bookmarked'] = profile.bookmark_this(local_entity)
         else:
             results['bookmarked'] = profile.is_this_bookmarked_by_me(
                 local_entity)
         return success('ok', 'bookmark', results)
     except AttributeError as a:
         return not_authorized()
     except Exception as e:
         print(e)
         return not_authorized()
示例#3
0
    def project(request, project_id=None):
        from dashboard.serializer import ProjectSerializer
        # DELETE PROJECT
        if request.method == 'DELETE' and project_id is not None:
            try:
                profile = request.user.profile
                projects = Project.objects.get(id=project_id, profile=profile)
                projects.delete()
            except ObjectDoesNotExist as e:
                print(e)
                print(not_authorized())
            return success('ok', 'project deleted', {})

        # GET ALL
        if request.method == 'GET' and project_id is None:
            try:
                profile = request.user.profile
                projects = Project.objects.filter(profile=profile)
                serialized = ProjectSerializer(projects, many=True)
                return success('ok', 'user projects', serialized.data)
            except Exception as e:
                print(e)
                return not_found()

        # GET SINGLE
        if request.method == 'GET' and project_id is not None:
            try:
                project = Project.objects.filter(id=project_id)
                serialized = ProjectSerializer(project, many=True)
                return success('ok', 'single project', serialized.data)
            except ObjectDoesNotExist as o:
                print(o)
                return not_found()
            except Exception as e:
                print(e)
                return error()

        # UPDATE
        if request.method == 'POST' and project_id is not None:
            data_to_update = {}
            data_to_update['name'] = request.POST.get('name', '')
            data_to_update['description'] = request.POST.get('description', '')
            data_to_update['start_date'] = request.POST.get('start_date', '')
            data_to_update['creator_role'] = request.POST.get(
                'creator_role', '')
            data_to_update['project_url'] = request.POST.get('project_url', '')
            end_date = request.POST.get('end_date', '')

            # check if the image needs to be updated
            if request.POST.get('picture', None) != '':
                # image to be updated
                picture = request.FILES.get('picture')
                v13.check_image(picture)
                data_to_update['picture'] = picture
            # check if is or not an ongoing project
            try:
                if end_date != '':
                    end_date = dt.strptime(end_date, '%Y-%m-%d')
                    data_to_update['start_date'] = dt.strptime(
                        data_to_update['start_date'], '%Y-%m-%d')
                    # if end_date > dt.now():
                    #     return bad_request('The project end_date cannot be in the future')
                    if end_date < data_to_update['start_date']:
                        return bad_request(
                            'The project end date cannot be before the project start date'
                        )
                    data_to_update['end_date'] = end_date
            except Exception as e:
                print(e)
                return error()
            # get the model object and check the profile owns that project
            try:
                profile = request.user.profile
                project = Project.objects.filter(id=project_id,
                                                 profile=profile).first()
                # clear and update tag
                project.set_tags(request.POST.get('tags', ''))
                # remove tag from data_to_update
                project.__dict__.update(data_to_update)
                project.save()
            except Project.DoesNotExist as e:
                print(e)
                return not_authorized()
            except Exception as e:
                print(e)
                return error()
            result = ProjectSerializer(project).data
            return success('ok', 'project updated', result)

        # CREATE
        if request.method == 'POST' and project_id is None:
            # check if fields are filled
            try:
                project_image = request.FILES.get('picture')
                project_name = request.POST['name']
                project_description = request.POST['description']
                project_start_date = dt.strptime(request.POST['start_date'],
                                                 '%Y-%m-%d')
                project_creator_role = request.POST['creator_role']
                project_url = request.POST['project_url']
                project_tags = request.POST['tags']

                # Check image
                is_image_ok = v13.check_image(project_image)
                if is_image_ok is not True:
                    return is_image_ok

            except KeyError as k:
                print(k)
                return bad_request("Please fill all the fields")
            except Exception as e:
                return bad_request(e)

            # check if is or not an ongoing project
            try:
                project_end_date = dt.strptime(request.POST['end_date'],
                                               '%Y-%m-%d')
            except KeyError:
                project_end_date = None

            # if it is not an ongoing project check dates
            if project_end_date is not None:
                if project_end_date < project_start_date:
                    return bad_request(
                        'The project END DATE cannot be before the project START DATE'
                    )

            try:
                # check user has not project with that name
                profile = request.user.profile
                projects = Project.objects.filter(profile=profile,
                                                  name=project_name)
                if len(projects) > 0:
                    return bad_request('Project name already exist')

                project = Project(profile=profile,
                                  name=project_name,
                                  picture=project_image,
                                  description=project_description,
                                  start_date=project_start_date,
                                  end_date=project_end_date,
                                  creator_role=project_creator_role,
                                  project_url=project_url)
                project.save()
                project.tags.clear()
                for tagName in [
                        x.lower().capitalize() for x in project_tags.split(",")
                ]:
                    project.tags.add(
                        Tag.objects.filter(name=tagName).first()
                        or Tag.create(name=tagName))
                project.save()
                result = ProjectSerializer(project).data
            except Exception as e:
                return bad_request(e)

            return success('ok', 'project created', result)