问题描述
在Django Rest Framework中,我试图为视图集中的以下两个方法编写一个mixin:
class OrderViewSet(viewsets.ModelViewSet):
# ...
def get_object(self):
pk = self.kwargs.get('pk')
if pk == 'me':
user = self.request.user
return get_object_or_404(Order,user=user.pk)
return super(OrderViewSet,self).get_object()
def get_queryset(self):
user = self.request.user
return Agent.objects.filter(user=user)
除了super(OrderViewSet,self)
之外,这似乎很简单,因为在声明它们之前,我需要以某种方式在我的mixin类中引用不同的视图集类。有提示吗?
解决方法
使用新样式的super()调用
class DefaultFileRepository {
// MARK: Properties
let queue: DispatchQueue = .global()
let fileManager: FileManager = .default
lazy var baseURL: URL = {
try! fileManager
.url(for: .libraryDirectory,in: .userDomainMask,appropriateFor: nil,create: true)
.appendingPathComponent("MyFiles")
}()
// MARK: Private functions
private func doRead(from path: String) throws -> String {
let url = baseURL.appendingPathComponent(path)
var isDir: ObjCBool = false
guard fileManager.fileExists(atPath: url.path,isDirectory: &isDir) && !isDir.boolValue else {
throw ReadWriteError.doesNotExist
}
let string: String
do {
string = try String(contentsOf: url)
} catch {
throw ReadWriteError.readFailed(error)
}
return string
}
private func doWrite(_ string: String,to path: String) throws {
let url = baseURL.appendingPathComponent(path)
let folderURL = url.deletingLastPathComponent()
var isFolderDir: ObjCBool = false
if fileManager.fileExists(atPath: folderURL.path,isDirectory: &isFolderDir) {
if !isFolderDir.boolValue {
throw ReadWriteError.canNotCreateFolder
}
} else {
do {
try fileManager.createDirectory(at: folderURL,withIntermediateDirectories: true)
} catch {
throw ReadWriteError.canNotCreateFolder
}
}
var isDir: ObjCBool = false
guard !fileManager.fileExists(atPath: url.path,isDirectory: &isDir) || !isDir.boolValue else {
throw ReadWriteError.canNotCreateFile
}
guard let data = string.data(using: .utf8) else {
throw ReadWriteError.encodingFailed
}
do {
try data.write(to: url)
} catch {
throw ReadWriteError.writeFailed(error)
}
}
}
extension DefaultFileRepository: FileRepository {
func read(from path: String) throws -> String {
try queue.sync { try self.doRead(from: path) }
}
func readAsync(from path: String,completion: @escaping (Result<String,Error>) -> Void) {
queue.async {
do {
let result = try self.doRead(from: path)
completion(.success(result))
} catch {
completion(.failure(error))
}
}
}
func write(_ string: String,to path: String) throws {
try queue.sync { try self.doWrite(string,to: path) }
}
func writeAsync(_ string: String,to path: String,completion: @escaping (Result<Void,Error>) -> Void) {
queue.async {
do {
try self.doWrite(string,to: path)
completion(.success(Void()))
} catch {
completion(.failure(error))
}
}
}
}
enum ReadWriteError: LocalizedError {
// MARK: Cases
case doesNotExist
case readFailed(Error)
case canNotCreateFolder
case canNotCreateFile
case encodingFailed
case writeFailed(Error)
}