| 1 |  |  | # coding: utf-8 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from taiga import TaigaAPI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # django classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from django.utils.log import getLogger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from django.core.cache import caches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | # django_th classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from django_th.services.services import ServicesMgr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from django_th.models import UserService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from th_taiga.models import Taiga | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | logger = getLogger('django_th.trigger_happy') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | cache = caches['th_taiga'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class ServiceTaiga(ServicesMgr): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         Service Slack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     def __init__(self, token=None, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         super(ServiceTaiga, self).__init__(token, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         self.user = kwargs.get('user') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     def taiga_api(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         us = UserService.objects.get(user=self.user, name='ServiceTaiga') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if us.token: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             api = TaigaAPI(token=us.token, host=us.host) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             api = TaigaAPI(host=us.host) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             api.auth(us.username, us.password) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return api | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     def read_data(self, **kwargs): | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             get the data from the service | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             :param kwargs: contain keyword args : trigger_id and model name | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             :type kwargs: dict | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             :rtype: dict | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         trigger_id = kwargs.get('trigger_id') | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         kwargs['model_name'] = 'Taiga' | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         super(ServiceTaiga, self).read_data(**kwargs) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         data = kwargs.get('data') | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         cache.set('th_taiga_' + str(trigger_id), data) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         # return the data | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         return data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     def save_data(self, trigger_id, **data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             get the data from the service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             :param trigger_id: id of the trigger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             :params data, dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             :rtype: dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         status = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         taiga = Taiga.objects.get(trigger_id=trigger_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         title = self.set_title(data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         body = self.set_content(data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         # add a 'story' to the project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if taiga.project_name: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             api = self.taiga_api() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             new_project = api.projects.get_by_slug(taiga.project_name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             userstory = new_project.add_user_story(title, description=body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if userstory: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 status = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |         return status | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  |