| 430 | class FilePathField(Field): |
| 431 | def __init__(self, verbose_name=None, name=None, path='', match='', recursive=False, **kwargs): |
| 432 | self.path = path |
| 433 | self.match = match |
| 434 | self.recursive = recursive |
| 435 | Field.__init__(self, verbose_name, name, **kwargs) |
| 436 | |
| 437 | def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False): |
| 438 | if self.match != '': |
| 439 | import re |
| 440 | p = re.compile(self.match) |
| 441 | files = os.listdir(self.path) |
| 442 | theFileList = [] |
| 443 | if self.recursive: |
| 444 | for root, dirs, files in os.walk(self.path): |
| 445 | for f in files: |
| 446 | if self.match != '': |
| 447 | m = p.match(f) |
| 448 | if m: |
| 449 | theFileList.append(tuple([ os.path.join(self.path, f), f])) |
| 450 | else: |
| 451 | theFileList.append(tuple([ os.path.join(self.path, f), f])) |
| 452 | else: |
| 453 | for f in files: |
| 454 | if os.path.isfile(os.path.join(self.path,f)): |
| 455 | if self.match != '': |
| 456 | m = p.match(f) |
| 457 | if m: |
| 458 | theFileList.append(tuple([ os.path.join(self.path,f), f])) |
| 459 | else: |
| 460 | theFileList.append(tuple([ os.path.join(self.path,f), f])) |
| 461 | self.choices = tuple(theFileList) |
| 462 | return Field.get_manipulator_fields(self, opts, manipulator, change, name_prefix, rel) |
| 463 | |
| 464 | def get_manipulator_field_objs(self): |
| 465 | return [formfields.TextField] |
| 466 | |