Exemple #1
0
def project_add_resource(request, pk):
    project = get_object_or_404(Project, pk=pk)
    if request.method == 'POST': # If the form has been submitted...
        resource_form = NewResourceForm(request.POST, prefix="resource")
        resource_download_url_form = ResourceDownloadURLForm(request.POST, prefix="download")
        if resource_form.is_valid():
            # All NewResourceForm validation rules pass
            success = True
            # Process the data in resource_form.cleaned_data
            new_resource = Resource(name=resource_form.cleaned_data["name"],
                                    description=resource_form.cleaned_data["description"],
                                    url=resource_form.cleaned_data["url"],
                                    status="SG"
            )
            # We've got our new resource, now let's check if the download
            # information provided is valid. If the download URL is filled
            # and there is an error, show the error messages.
            # If the download URL is empty, just ignore it.
            u = None
            if resource_download_url_form.is_valid():
                # All ResourceDownloadURLForm validation rules pass
                u = ResourceDownloadURL(media_type=resource_download_url_form.cleaned_data["media_type"],
                                        format=resource_download_url_form.cleaned_data["format"],
                                        url=resource_download_url_form.cleaned_data["url"])
            elif resource_download_url_form.data["download-url"]:
                # The download URL is populated, yet some of the info related
                # to the download URL is not valid, so show the error message
                success = False
            if success:
                # Create the new resource
                new_resource.save()
                new_resource.projects.add(project)
                if u:
                    # Create the new download URL
                    u.resource = new_resource
                    u.save()
                # Redirect after POST to show success message
                return HttpResponseRedirect('/p/%d/resource-added' % project.pk)
    else:
        # Unbound forms
        resource_form = NewResourceForm(prefix="resource")
        resource_download_url_form = ResourceDownloadURLForm(prefix="download")

    c = RequestContext(request, {
        'resource_form': resource_form,
        "resource_download_url_form": resource_download_url_form,
        'project': project,
    })
    return render_to_response('project_add_resource.html', c)
Exemple #2
0
 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")
Exemple #3
0
 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")