def GetFiles(request): #Add new files to database from RecSearch import RecSearch from Tagger import AutoTags Root = request.GET.get('Root', DefaultRoot) PathList = RecSearch(Root) Prog = 0 Percentage = 0 Total = len(PathList) Tags = map(lambda TagItem: (TagItem.Name, TagItem), TagObj.objects.all()) TagDict = dict(Tags) for NewPath in PathList: Prog += 1 if (Prog * 100) / Total > Percentage: Percentage = (Prog * 100) / Total print Percentage try: FileObj.objects.get(Path = NewPath) except: NewName = NewPath.rpartition('/')[2] FileItem = FileObj(Path = NewPath, Name = NewName) FileItem.save() TagNames = AutoTags(NewPath, Root) for TagName in TagNames: try: TagItem = TagDict[TagName] except: TagItem = TagObj(Name = TagName) TagItem.save() TagDict[TagName] = TagItem FileItem.Tags.add(TagItem) return HttpResponseRedirect('/update/')
def AddTag(request, FileIndex): TagName = request.GET.get('TagName', '').lower() TagIndex = request.GET.get('TagIndex', '') print TagIndex try: TagItem = TagObj.objects.get(id = TagIndex) except: if not TagName: return HttpResponseRedirect('/files/%s/newtag/' % FileIndex) else: try: TagItem = TagObj.objects.get(Name = TagName) except: TagItem = TagObj(Name = TagName) TagItem.save() try: FileItem = FileObj.objects.get(id = FileIndex) except: return HttpResponseRedirect('/files/') FileItem.Tags.add(TagItem) return HttpResponseRedirect('/files/%s/' % FileIndex)