[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [pups/master] Using @staticmethod instead of instantiating Token() objects



commit ca32dabf87eb018af2886fe35cd6dfe561c12e70
Author: Sherief Alaa <sheriefalaa.w@xxxxxxxxx>
Date:   Sun Jun 22 00:43:25 2014 +0300

    Using @staticmethod instead of instantiating Token() objects
---
 webchat/models.py |   10 +++++++---
 webchat/views.py  |    9 +++------
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/webchat/models.py b/webchat/models.py
index 375ba04..6722aa1 100644
--- a/webchat/models.py
+++ b/webchat/models.py
@@ -41,7 +41,8 @@ class Token(models.Model):
     def __unicode__(self):
         return u'ID: %s Owner: %s visits: %s' % (self.t_id, self.owner, self.visits)
 
-    def create_token(self, owner_id, expiration_days, comment):
+    @staticmethod
+    def create_token(owner_id, expiration_days, comment):
         q = Token(
             owner=User.objects.get(id=owner_id),
             token=uuid.uuid4().hex,
@@ -52,13 +53,15 @@ class Token(models.Model):
 
         return q.t_id is not None
 
+    @staticmethod
     def get_token(self, token):
         try:
             return Token.objects.get(token=token)
         except ObjectDoesNotExist:
             return []
 
-    def revoke_tokens(self, token_list):
+    @staticmethod
+    def revoke_tokens(token_list):
         '''
         Sets the expiration date equals to the creation date of a token or more
         '''
@@ -68,7 +71,8 @@ class Token(models.Model):
                 .update(expires_at=F('created_at'))
         return True
 
-    def get_assistant_tokens(self, assistant):
+    @staticmethod
+    def get_assistant_tokens(assistant):
         '''
         Returns a list of non-expired/revoked assistant's tokens
         '''
diff --git a/webchat/views.py b/webchat/views.py
index f5b9aad..166e9be 100644
--- a/webchat/views.py
+++ b/webchat/views.py
@@ -32,7 +32,6 @@ from pups import settings
 
 @login_required
 def tokens_page(request):
-    token = Token()
 
     # Create one token
     if 'create_token' in request.POST:
@@ -44,7 +43,7 @@ def tokens_page(request):
 
     params = {
         'name': request.user.username,
-        'tokens': token.get_assistant_tokens(
+        'tokens': Token.get_assistant_tokens(
             User.objects.get(id=request.user.id)),
         'url': settings.CONFIG['url']
     }
@@ -53,9 +52,8 @@ def tokens_page(request):
 
 
 def create_token(request):
-    token = Token()
 
-    if not token.create_token(request.user.id,
+    if not Token.create_token(request.user.id,
                               settings.CONFIG['expiration_days'],
                               request.POST.get('comment', '')):
 
@@ -67,7 +65,6 @@ def create_token(request):
 
 
 def revoke_tokens(request):
-    token = Token()
 
     # If nothing was selected redirect and complain
     if len(request.POST.getlist("selected_list")) == 0:
@@ -75,7 +72,7 @@ def revoke_tokens(request):
         return redirect('/tokens')
 
     # Revoke a token or more
-    token.revoke_tokens(request.POST.getlist("selected_list"))
+    Token.revoke_tokens(request.POST.getlist("selected_list"))
     messages.add_message(request, messages.INFO, fbm.revoke_success)
     return redirect('/tokens')
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits