Commits

Anonymous committed 1278c8f

finished the function to created complete csv files from a request with celery

Comments (0)

Files changed (4)

followerslookup/tasks.py

 from social_auth.models import UserSocialAuth
 from followerslookup.authentication import get_account
 from followerslookup.fetching import get_followers_infos,get_followers_ids
-from followerslookup.models import TwitterUser
+from followerslookup.models import TwitterUser, User
+import csv,codecs
+try:
+    from cStringIO import StringIO
+except ImportError:
+    from StringIO import StringIO
 
 
 @task()
         followers_infos = get_followers_infos(account, followers_ids,user.name )
     return 'Fetch ended'
 
+@task()
+def convert_context_to_csv(context, csv_file = StringIO()):
+    if type(csv_file) == unicode:
+        csv_file = open("%s%s"%(csv_file,'-users.csv'),"w")
+    writer = csv.writer(csv_file)
+    writer.writerow(User._meta.get_all_field_names())
+    for obj in context["object_list"]:
+        row = []
+        for elem in User._meta.get_all_field_names():
+            if elem == "twitteruser":
+                twitter_users = []
+                for usr in obj.twitteruser_set.all():
+                    twitter_users.append(usr.name)
+                    row.append(','.join(twitter_users))
+            else:
+                row.append(unicode(getattr(obj,elem)).encode("utf-8"))
+        writer.writerow(row)
+    if type(csv_file) == file:
+        csv_file.close()
+        return
+    return csv_file.getvalue()
 
 
 

followerslookup/urls.py

 urlpatterns = patterns('',
     url(r'^howto/$',TemplateView.as_view(template_name="how-to.html")),
 #    url(r'^twitteruser/(?P<user>\w+)/$',GenericListView.as_view(model = User, paginate_by=100)),
+    url(r'^lookup/complete/(?P<user>\w+)/$',
+        CsvGenericListView.as_view(model = User),
+        {'complete':True}
+        ),
     url(r'^lookup/(?P<user>\w+)/$',
         CsvGenericListView.as_view(model = User,paginate_by=100)),
     url(r'^lookup/$',CsvGenericListView.as_view(model = User,paginate_by=100)),
+
     url(r'^test/(?P<pk>\d+)/$',TwitterUserListView.as_view(model=TwitterUser)),
     )
 

followerslookup/views.py

 from django.views.generic import ListView,DetailView
 from django.http import HttpResponse
 from django.conf import settings
-import csv,codecs
 import os
-try:
-    from cStringIO import StringIO
-except ImportError:
-    from StringIO import StringIO
+from tasks import convert_context_to_csv
 
 class TwitterUserListView(DetailView):
     
 class CsvGenericListView(GenericListViewMixin, ListView):
 
     def render_to_response(self,context):
+        print self.kwargs
         if "complete" in self.kwargs :
             filename_list = []
             for k,v in self.request.GET.iteritems():
             if not os.path.exists(base_path):
                      os.makedirs(base_path)
             
-            csv_file = open("%s%s"%(path,'-user.csv'),"w")
-            self.get_csv_response(self.convert_context_to_csv(context,csv_file = csv_file))
-            csv_file.close()
-            return HttpResponse("requested file : %s will be generated on our server"%self.kwargs['user'])
+            convert_context_to_csv.delay(context,csv_file = path)
+            return HttpResponse(
+        """
+            requested file will be generated on our server. Find it when it will 
+            be ready at <a href=%s>%s</a>
+        """%(settings.MEDIA_URL+relative_path,settings.MEDIA_URL+relative_path)
+                )
                                   
                                                               
-        return self.get_csv_response(self.convert_context_to_csv(context))
+        return self.get_csv_response(convert_context_to_csv(context))
 
     
     def get_csv_response(self, content, **httpresponse_kwargs):
                             **httpresponse_kwargs)
         resp['Content-Disposition'] = 'attachment; filename=users.csv'
         return resp
-    def convert_context_to_csv(self, context, csv_file = StringIO()):
-        writer = csv.writer(csv_file)
-        writer.writerow(User._meta.get_all_field_names())
-        for obj in context["object_list"]:
-            row = []
-            for elem in User._meta.get_all_field_names():
-                if elem == "twitteruser":
-                    twitter_users = []
-                    for usr in obj.twitteruser_set.all():
-                        twitter_users.append(usr.name)
-                    row.append(','.join(twitter_users))
-                else:
-                    row.append(unicode(getattr(obj,elem)).encode("utf-8"))
-            writer.writerow(row)
-        if type(csv_file) == file:
-            return
-        return csv_file.getvalue()
+    
+    # def convert_context_to_csv(self, context, csv_file = StringIO()):
+    #     writer = csv.writer(csv_file)
+    #     writer.writerow(User._meta.get_all_field_names())
+    #     for obj in context["object_list"]:
+    #         row = []
+    #         for elem in User._meta.get_all_field_names():
+    #             if elem == "twitteruser":
+    #                 twitter_users = []
+    #                 for usr in obj.twitteruser_set.all():
+    #                     twitter_users.append(usr.name)
+    #                 row.append(','.join(twitter_users))
+    #             else:
+    #                 row.append(unicode(getattr(obj,elem)).encode("utf-8"))
+    #         writer.writerow(row)
+    #     if type(csv_file) == file:
+    #         return
+    #     return csv_file.getvalue()
         
 # URL that handles the media served from MEDIA_ROOT. Make sure to use a
 # trailing slash.
 # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
-MEDIA_URL = 'http://boblefrag.pilotsystems.net/media/'
+
 
 # Absolute path to the directory static files should be collected to.
 # Don't put anything in this directory yourself; store your static files
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.