def deduplicate_project(self, request, project_id): # Allows to split a project into multiple that are linked to the # same resources original_project = get_object_or_404(Project, pk=project_id) if request.method == 'POST': # If the form has been submitted... # Get a formset bound to the POST data ProjectFormSet = formset_factory(DeduplicateProjectForm) formset = ProjectFormSet(request.POST) if formset.is_valid(): # All validation rules pass, retrieve the list of resources # that were linked to the original project resources = original_project.resource_set.all() success = False # Process the data in formset.cleaned_data for cleaned_data in formset.cleaned_data: if cleaned_data: project_name = cleaned_data["name"].strip() try: # Check if it's an already-known project p = Project.objects.get(name=project_name) if "VF" == p.status: # If the project was already verified, # bump it back to Pending p.status = "PD" p.save() except Project.DoesNotExist: # Create a new project p = Project(name=project_name, description=u"Empty description", status="NW") # Check if the nice url is not already in use nu = cleaned_data["nice_url"].strip() if 0 == Project.objects.filter(nice_url=nu).count() and \ 0 == Series.objects.filter(nice_url=nu).count(): p.nice_url = nu # Save the newly created project p.save() # Link the [new] project to all the resources of # the original project for r in resources: r.projects.add(p) success = True else: # An empty string, nothing to do pass if success: # Delete the original project if at least one new # project got created original_project.delete() # Redirect after POST return HttpResponseRedirect("/admin/flosstalks_app/project/deduplicate/%d/" % (int(project_id) + 1)) else: # This is a GET # Try to split the names of the projects new_projects_names = [original_project.name] if ", " in original_project.name: # Split the project names on ", " new_projects_names = original_project.name.split(", ") if " and " in new_projects_names[-1]: # Split the last 2 project names on " and " new_projects_names.extend(new_projects_names[-1].split(" and ")) # Remove the one containing " and " since it's now split new_projects_names.pop(-3) new_projects = [] for p in new_projects_names: new_projects.append({"name": p.strip(), "nice_url": self.slugify(p.strip())}) ProjectFormSet = formset_factory(DeduplicateProjectForm, extra=3) formset = ProjectFormSet(initial=new_projects) return render_to_response( "admin/projects/deduplicate.html", { "original_project" : original_project, "formset": formset, }, RequestContext(request, {}), )
def handle(self, *args, **options): testmode = False if 1 == len(args) and "testmode" == args[0]: testmode = True self.stdout.write("TEST MODE ON\n\n") elif 0 != len(args): raise CommandError('Invalid argument') self.stdout.write("Updating all series:\n") for s in Series.objects.all(): feeds = s.seriesfeedurl_set.all() ss = None if "FLOSS Weekly" == s.name: ss = FLOSSWeekly(feeds) elif "Sourcetrunk" == s.name: ss = Sourcetrunk(feeds) elif "The Changelog" == s.name: ss = TheChangelog(feeds) else: self.stdout.write( "\n\nWarning: Series %s not supported yet!\n" % s.name) continue if testmode: # In test mode, use the sample RSS feeds instead of the real ones ss.testmode() if 0 == len(feeds): self.stdout.write("Skipping series that has no feed...\n") else: self.stdout.write("\n\n- Updating %d feeds for %s" % (len(feeds), s)) for f in ss.feeds: self.stdout.write("\n\n%s (%s): %s" % (f, f.format, f.url)) d = feedparser.parse(f.url) # Check last updated, d["updated"] or d["updated_parsed"] for e in d.entries: self.stdout.write("\n%s:\t" % e.title) # Check if this download is already known entry_link = ss.get_entry_link(e) if 0 != len( ResourceDownloadURL.objects.filter( url=entry_link)): self.stdout.write(u"\u2714") continue self.stdout.write("U") u = ResourceDownloadURL( media_type=f.media_type, format=f.format, url=entry_link, ) # Get the project's name and the entry's id (project_name, entry_id) = ss.get_project_name_and_entry_id(s, f, e) # Check if this resource is already known try: r = Resource.objects.get(external_id=entry_id) u.resource = r u.save() # The new download url is linked to the # already-existing resource, nothing left to do continue except Resource.DoesNotExist: self.stdout.write(" R") r = Resource( name=e.title, description=e.subtitle_detail.value, url=ss.get_resource_link(e), series=s, status="NW", external_id=entry_id, pub_date=strftime("%Y-%m-%d %H:%M:%S+00:00", e.updated_parsed), ) if e.has_key("itunes_duration"): r.length = e.itunes_duration r.save() # Link the download URL to the resource u.resource = r u.save() # Check if this project is already known try: p = Project.objects.get(name=project_name) # Link the resource to the project r.projects.add(p) continue except Project.DoesNotExist: self.stdout.write(" P") # Project does not exist, create it p = Project(name=project_name, description=e.subtitle_detail.value, status="NW") # Give this project a nice URL only if not yet used # for either a project or a series nu = ss.get_nice_url(project_name) if 0 == Project.objects.filter(nice_url=nu).count() and \ 0 == Series.objects.filter(nice_url=nu).count(): p.nice_url = nu else: self.stdout.write( "\nWarning: nice url '/%s' already in use!\n" % nu) p.save() # Link the resource to the project r.projects.add(p) self.stdout.write("\n")
def handle(self, *args, **options): testmode = False if 1 == len(args) and "testmode" == args[0]: testmode = True self.stdout.write("TEST MODE ON\n\n") elif 0 != len(args): raise CommandError("Invalid argument") self.stdout.write("Updating all series:\n") for s in Series.objects.all(): feeds = s.seriesfeedurl_set.all() ss = None if "FLOSS Weekly" == s.name: ss = FLOSSWeekly(feeds) elif "Sourcetrunk" == s.name: ss = Sourcetrunk(feeds) elif "The Changelog" == s.name: ss = TheChangelog(feeds) else: self.stdout.write("\n\nWarning: Series %s not supported yet!\n" % s.name) continue if testmode: # In test mode, use the sample RSS feeds instead of the real ones ss.testmode() if 0 == len(feeds): self.stdout.write("Skipping series that has no feed...\n") else: self.stdout.write("\n\n- Updating %d feeds for %s" % (len(feeds), s)) for f in ss.feeds: self.stdout.write("\n\n%s (%s): %s" % (f, f.format, f.url)) d = feedparser.parse(f.url) # Check last updated, d["updated"] or d["updated_parsed"] for e in d.entries: self.stdout.write("\n%s:\t" % e.title) # Check if this download is already known entry_link = ss.get_entry_link(e) if 0 != len(ResourceDownloadURL.objects.filter(url=entry_link)): self.stdout.write(u"\u2714") continue self.stdout.write("U") u = ResourceDownloadURL(media_type=f.media_type, format=f.format, url=entry_link) # Get the project's name and the entry's id (project_name, entry_id) = ss.get_project_name_and_entry_id(s, f, e) # Check if this resource is already known try: r = Resource.objects.get(external_id=entry_id) u.resource = r u.save() # The new download url is linked to the # already-existing resource, nothing left to do continue except Resource.DoesNotExist: self.stdout.write(" R") r = Resource( name=e.title, description=e.subtitle_detail.value, url=ss.get_resource_link(e), series=s, status="NW", external_id=entry_id, pub_date=strftime("%Y-%m-%d %H:%M:%S+00:00", e.updated_parsed), ) if e.has_key("itunes_duration"): r.length = e.itunes_duration r.save() # Link the download URL to the resource u.resource = r u.save() # Check if this project is already known try: p = Project.objects.get(name=project_name) # Link the resource to the project r.projects.add(p) continue except Project.DoesNotExist: self.stdout.write(" P") # Project does not exist, create it p = Project(name=project_name, description=e.subtitle_detail.value, status="NW") # Give this project a nice URL only if not yet used # for either a project or a series nu = ss.get_nice_url(project_name) if ( 0 == Project.objects.filter(nice_url=nu).count() and 0 == Series.objects.filter(nice_url=nu).count() ): p.nice_url = nu else: self.stdout.write("\nWarning: nice url '/%s' already in use!\n" % nu) p.save() # Link the resource to the project r.projects.add(p) self.stdout.write("\n")
def deduplicate_project(self, request, project_id): # Allows to split a project into multiple that are linked to the # same resources original_project = get_object_or_404(Project, pk=project_id) if request.method == 'POST': # If the form has been submitted... # Get a formset bound to the POST data ProjectFormSet = formset_factory(DeduplicateProjectForm) formset = ProjectFormSet(request.POST) if formset.is_valid(): # All validation rules pass, retrieve the list of resources # that were linked to the original project resources = original_project.resource_set.all() success = False # Process the data in formset.cleaned_data for cleaned_data in formset.cleaned_data: if cleaned_data: project_name = cleaned_data["name"].strip() try: # Check if it's an already-known project p = Project.objects.get(name=project_name) if "VF" == p.status: # If the project was already verified, # bump it back to Pending p.status = "PD" p.save() except Project.DoesNotExist: # Create a new project p = Project(name=project_name, description=u"Empty description", status="NW") # Check if the nice url is not already in use nu = cleaned_data["nice_url"].strip() if 0 == Project.objects.filter(nice_url=nu).count() and \ 0 == Series.objects.filter(nice_url=nu).count(): p.nice_url = nu # Save the newly created project p.save() # Link the [new] project to all the resources of # the original project for r in resources: r.projects.add(p) success = True else: # An empty string, nothing to do pass if success: # Delete the original project if at least one new # project got created original_project.delete() # Redirect after POST return HttpResponseRedirect( "/admin/flosstalks_app/project/deduplicate/%d/" % (int(project_id) + 1)) else: # This is a GET # Try to split the names of the projects new_projects_names = [original_project.name] if ", " in original_project.name: # Split the project names on ", " new_projects_names = original_project.name.split(", ") if " and " in new_projects_names[-1]: # Split the last 2 project names on " and " new_projects_names.extend( new_projects_names[-1].split(" and ")) # Remove the one containing " and " since it's now split new_projects_names.pop(-3) new_projects = [] for p in new_projects_names: new_projects.append({ "name": p.strip(), "nice_url": self.slugify(p.strip()) }) ProjectFormSet = formset_factory(DeduplicateProjectForm, extra=3) formset = ProjectFormSet(initial=new_projects) return render_to_response( "admin/projects/deduplicate.html", { "original_project": original_project, "formset": formset, }, RequestContext(request, {}), )