Commits

Lukasz Balcerzak committed 6b1c3f8

Signals sending moved to separate methods

Comments (0)

Files changed (1)

projector/contrib/git/views.py

 class ProjectGitBaseView(ProjectView):
     """
     Base view class for git http handler.
-
-    Extra attributes:
-
-    - ``type``: returns ``READ`` or ``WRITE`` value of ``Type`` subclass of
-      ``projector.contrib.git.githttp.GitWebServer``. It specifies if user
-      is reading from or writing into the repository.
-
-    Signals:
-
-    Basing on ``type`` attribute, git handler sends one of ``pre_clone``,
-    ``post_clone``, ``pre_push`` or ``post_push`` signal from
-    ``vcs.web.simplevcs.signals``.
     """
 
     csrf_exempt = True
 
 
 class ProjectGitHandler(ProjectGitBaseView):
+    """
+    Extra attributes:
+
+    - ``type``: returns ``READ`` or ``WRITE`` value of ``Type`` subclass of
+      ``projector.contrib.git.githttp.GitWebServer``. It specifies if user
+      is reading from or writing into the repository.
+
+    Signals:
+
+    Basing on ``type`` attribute, git handler sends one of ``pre_clone``,
+    ``post_clone``, ``pre_push`` or ``post_push`` signal from
+    ``vcs.web.simplevcs.signals``.
+    """
 
     def response(self, request, username, project_slug):
         try:
-            if 'git-upload-pack' in self.request.META['PATH_INFO']:
-                self.type = GitWebServer.Type.READ
-                pre_push.send(sender=self,
-                    repo_path=self.project.repository.path,
-                    ip=self.request.META.get('REMOTE_ADDR', ''),
-                    username=self.request.user.username)
-            elif 'git-receive-pack' in self.request.META['PATH_INFO']:
-                self.type = GitWebServer.Type.WRITE
-                pre_clone.send(sender=self,
-                    repo_path=self.project.repository.path,
-                    ip=self.request.META.get('REMOTE_ADDR', ''),
-                    username=self.request.user.username)
-            else:
-                self.type = GitWebServer.Type.UNSPECIFIED
+            self.send_pre_signals()
 
             auth_response = self.get_authed_user()
             if auth_response:
                 return auth_response
+
             git_server = GitWebServer(self.project.repository)
             response = git_server.get_response(request)
-            if self.is_read():
-                post_clone.send(sender=self,
-                    repo_path=self.project.repository.path,
-                    ip=self.request.META.get('REMOTE_ADDR', ''),
-                    username=self.request.user.username)
-            elif self.is_write():
-                post_push.send(sender=self,
-                    repo_path=self.project.repository.path,
-                    ip=self.request.META.get('REMOTE_ADDR', ''),
-                    username=self.request.user.username)
+
+            self.send_post_signals()
         except Exception, err:
             log_error(err)
             raise err
+        return response
 
-        #print response
-        return response
+    @property
+    def type(self):
+        """
+        Returns type of request (READ, WRITE or UNSPECIFIED).
+        """
+        if 'git-upload-pack' in self.request.META['PATH_INFO']:
+            type = GitWebServer.Type.READ
+        elif 'git-receive-pack' in self.request.META['PATH_INFO']:
+            type = GitWebServer.Type.WRITE
+        else:
+            type = GitWebServer.Type.UNSPECIFIED
+        return type
+
+    def get_signal_info(self):
+        return {
+            'sender': self,
+            'repo_path': self.project.repository.path,
+            'ip': self.request.META.get('REMOTE_ADDR', ''),
+            'username': self.request.user.username,
+        }
+
+    def send_pre_signals(self):
+        if self.is_read():
+            signal = pre_clone
+        elif self.is_write():
+            signal = pre_push
+        else:
+            # Don't send any signal if we can't recognize request
+            return
+        info = self.get_signal_info()
+        return signal.send(**info)
+
+    def send_post_signals(self):
+        if self.is_read():
+            signal = post_clone
+        elif self.is_write():
+            signal = post_push
+        else:
+            # Don't send any signal if we can't recognize request
+            return
+        info = self.get_signal_info()
+        return signal.send(**info)
 
     def get_authed_user(self):
         if self.project.is_public():
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.