Пример #1
0
def getSimilarByKeywords(material_id, version):
    """Returns list of similar materials getting by comparing keywords"""

    # why possibly version gets nothing as value?
    if not version:
        version = models.getMaterialLatestVersion(material_id)

    try:
        weight_list = {}
        material = models.Material.objects.filter(
            material_id=material_id, 
            version=version).values('keywords')[0]

        # build the keyword query
        keywords = material.get('keywords', '')
        keywords = keywords.split('\n')
        for kw in keywords:
            objs = models.Material.objects.exclude(material_id=material_id).\
                filter(keywords__contains=kw).values('material_id', 'title')
            m_list = [(item['material_id'], item['title']) for item in objs]
            for m_item in m_list:
                mid = m_item[0]
                if not weight_list.has_key(mid):
                    item_dict = {'material_id': m_item[0], 'title': m_item[1]}
                    weight_list[mid] = [0, item_dict]
                weight_list[mid][0] += SM_WEIGHT_KEYWORD 
        
        # sort by weights then limit the results
        couples = weight_list.values()
        couples.sort(reverse=True)
        result = [item[1] for item in couples[:10]]
    except:
        raise404(request, 404)
    return result
Пример #2
0
def listMaterialFiles(request, *args, **kwargs):
    """Lists all files attached to the specific material, except the material image
    """
    material_id = kwargs.get('mid', None)
    version = kwargs.get('version', None)
    # why possibly version gets nothing as value?
    if not version:
        version = models.getMaterialLatestVersion(material_id)
    file_ids = models.listMaterialFiles(material_id, version)

    return Response(file_ids)   
Пример #3
0
    def put(self, request, *args, **kwargs):
        """ Check in a material  """
        try: 
            serializer = self.get_serializer(data=request.DATA)
            response = None
            if serializer.is_valid():

                # check if valid editor or new material will be created
                sobj = serializer.object
                sobj.material_id = material_id = kwargs['mid']

                # special update or checkin?
                if request.GET.get('magic-update', None):
                    sobj.version = current_version = int(kwargs['version'])
                    res = models.deleteMaterial(material_id, current_version)
                    if not res:
                        raise   # 404
                else:
                    last_version = models.getMaterialLatestVersion(sobj.material_id)
                    try:
                        sobj.version = last_version + 1
                    except AttributeError:
                        sobj.version = 1
                        
                self.pre_save(sobj)
                self.object = serializer.save()

                # update the person and roles
                models.setMaterialPersons(self.object.id, request.DATA)

                # add the attached image manually
                self.object.image = request.FILES.get('image', None)
                self.object.save()

                # next, add all other files submitted
                material_id = self.object.material_id
                material_version = self.object.version 
                for key in request.FILES.keys():
                    if key == 'image': continue
                    mfile = models.MaterialFile()
                    mfile.material_id = material_id 
                    mfile.version = material_version
                    file_content = request.FILES.get(key, None)
                    mfile.mfile = file_content 
                    mfile.name = request.FILES[key].name
                    mfile.description = request.DATA.get(key+'_description', '')
                    mfile.mime_type = mimetypes.guess_type(
                        os.path.realpath(mfile.mfile.name))[0] or ''
                    mfile.save()

                # (module/collection) create the zip package and post to vpt
                try:
                    if not request.DATA.get('export-later', 0):
                        requestMaterialPDF(self.object)
                except:
                    pass

                response = Response(serializer.data, status=status.HTTP_201_CREATED)
            else:
                response = Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)            

            return response
        except: 
            raise404(request)