def test_delete(self): sm1 = self.sample_material.copy() sm1['title'] = 'Title of the second material' sm1['text'] = 'This is the new update of this version' res = self.client.post('/1/materials/', sm1) content = normRes(res) self.assertEqual(res.status_code, CODE_CREATED) self.compareRes(sm1, content) # delete the first time res = deleteMaterial( self.content0['material_id'], self.content0['version']) self.assertEqual(res, True) # delete the 2nd time res = deleteMaterial( self.content0['material_id'], self.content0['version']) self.assertEqual(res, True) # verify the rest material res = self.client.get('/1/materials/') content = normRes(res) self.assertEqual(res.status_code, CODE_SUCCESS) self.assertEqual(content['count'], 1) self.assertEqual(content['results'][0]['title'], sm1['title'])
def destroy(self, request, *args, **kwargs): """ Delete the material """ if request.META.get('HTTP_VOER', None) != '1': ret_code = 400 else: try: res = models.deleteMaterial( kwargs.get('mid'), kwargs.get('version')) if res: ret_code = status.HTTP_200_SUCCESS else: ret_code = status.HTTP_204_NO_CONTENT except: raise404(request) return Response(status=ret_code)
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)