def handle(self, *args, **options): project_id = False if args: project_id = int(args[0]) if options['forced']: self.forced = True if project_id: for company in Company.objects.filter(active=True).exclude(basecamp_api_key="", basecamp_id="", basecamp_base_url=""): username = password = company.basecamp_api_key base_url = company.basecamp_base_url self.bc = Basecamp(base_url, username, password) self.base_url = base_url self.company = company project = ET.fromstring(self.bc.project(project_id)) self.update_project(project) else: for company in Company.objects.filter(active=True).exclude(basecamp_api_key="").exclude(basecamp_id="").exclude(basecamp_base_url=""): username = password = company.basecamp_api_key base_url = company.basecamp_base_url self.bc = Basecamp(base_url, username, password) self.base_url = base_url self.company = company xml = ET.fromstring(self.bc.projects()) for project in xml.findall('project'): self.update_project(project)
def handle_noargs(self, **options): output = options.pop("output") test = options.pop("test") if test: self.stdout.write(40 * ".") self.stdout.write("\nSync basecamp and local companies command. Ignore output..\n\n") if output: for company in Company.objects.filter(active=True).exclude(basecamp_api_key="").exclude(basecamp_id="").exclude(basecamp_base_url=""): username = password = company.basecamp_api_key base_url = company.basecamp_base_url bc = Basecamp(base_url, username, password) xml = ET.fromstring(bc.companies()) for company in xml.findall('company'): phone_number_office = company.find('phone-number-office').text try: client_of = company.find('client-of').text except: client_of = False web_address = company.find('web-address').text city = company.find('city').text name = company.find('name').text basecamp_id = company.find('id').text try: django_company = Company.objects.get(basecamp_id=basecamp_id) self.stdout.write("Company %s already exist." % django_company) except Company.DoesNotExist: django_company = Company( name = name, basecamp_id = basecamp_id ) django_company.save() self.stdout.write("Company %s creates." % django_company) if client_of: try: provider = Company.objects.get(basecamp_id=client_of) service = Service.objects.filter(company=provider, client_company=django_company) if not service: service = Service( company=provider, client_company=django_company ) service.save() self.stdout.write("Service %s creates." % service) except: pass if test: self.stdout.write("\nFinished sync basecamp and local users command.. continuing..\n")
def handle_noargs(self, **options): output = options.pop("output") test = options.pop("test") if test: self.stdout.write(40 * ".") self.stdout.write("\nSync basecamp and local users command. Ignore output..\n\n") if output: for company in Company.objects.filter(active=True).exclude(basecamp_api_key="").exclude(basecamp_base_url="").exclude(basecamp_id=""): username = password = company.basecamp_api_key base_url = company.basecamp_base_url company_id = int(company.basecamp_id) bc = Basecamp(base_url, username, password) xml = ET.fromstring(bc.people(company_id)) self.sync_users(xml, company) for other_company in company.clients.all(): self.stdout.write("Sync %s company" % other_company.id) xml_company = ET.fromstring(bc.people(int(other_company.basecamp_id))) self.sync_users(xml_company, other_company) if test: self.stdout.write("\nFinished sync basecamp and local users command.. continuing..\n")
class Command(BaseCommand): args = 'project_id' help = 'Sync basecamp and local projects and todo-lists on it.' bc = False base_url = False forced = False company = False option_list = BaseCommand.option_list + ( make_option('--forced', action='store_true', dest='forced', default=False, help='Update all todoitem, not look on date'), ) def update_project(self, project, *args): bc_id = int(project.find('id').text) bc_name = project.find('name').text bc_status = project.find('status').text company_id = project.find('company').find("id").text try: company = Company.objects.get(basecamp_id=company_id) except Company.DoesNotExist: company = self.company bc_last_changed_on = project.find('last-changed-on').text if bc_last_changed_on: bc_last_changed_on = parser.parse(bc_last_changed_on) need_update = False try: django_project = Project.objects.get(basecamp_id=bc_id) if django_project.name != bc_name: django_project.name = bc_name django_project.save() try: self.stdout.write(u"Project %s already exist." % django_project) except UnicodeDecodeError: self.stdout.write(u"Project already exist.") if (django_project.last_changed_on != bc_last_changed_on): need_update = True else: need_update = False except Project.DoesNotExist: try: django_project = Project.objects.get(name=bc_name, basecamp_id__isnull=True) django_project.basecamp_id = bc_id django_project.basecamp_url = self.base_url+'projects/'+str(bc_id)+'/todo_lists' if bc_status == 'active': django_project.status = "A" if bc_status == 'on_hold': django_project.status = "P" if bc_status == 'archived': django_project.status = "D" django_project.save() need_update = True self.stdout.write(u"Project %s updated." % django_project) except Project.DoesNotExist: django_project = False except Project.MultipleObjectsReturned: django_project = False except UnicodeDecodeError: self.stdout.write(u"Project updated.") if not django_project: django_project = Project( name = bc_name, basecamp_id = bc_id, basecamp_url = self.base_url+'projects/'+str(bc_id)+'/todo_lists', company=company ) if bc_status == 'active': django_project.status = "A" if bc_status == 'on_hold': django_project.status = "P" if bc_status == 'archived': django_project.status = "D" django_project.save() need_update = True try: self.stdout.write(u"Project %s creates." % django_project) except UnicodeDecodeError: self.stdout.write(u"Project creates.") if need_update or self.forced: try: self.stdout.write(u"Project %s need update." % django_project) except UnicodeDecodeError: self.stdout.write(u"Project need update.") self.update_todolists(django_project) django_project.last_changed_on = bc_last_changed_on if bc_status == 'active': django_project.status = "A" if bc_status == 'on_hold': django_project.status = "P" if bc_status == 'archived': django_project.status = "D" django_project.save() try: self.stdout.write(u"Project %s updated." % django_project) except UnicodeDecodeError: self.stdout.write(u"Project updated.") else: try: self.stdout.write(u"Project %s no need update." % django_project) except UnicodeDecodeError: self.stdout.write(u"Project no need update.") xml = ET.fromstring(self.bc.people_per_project(bc_id)) for person in xml.findall('person'): role = "G" email = person.find('email-address').text basecamp_id = person.find('id').text try: administrator = person.find('administrator').text if administrator == "true": role = "A" except: pass try: user = User.objects.get(profile__basecamp_id=basecamp_id) except User.DoesNotExist: try: user = User.objects.get(email=email) except User.DoesNotExist: user = False pass if user: django_project.add_user(user, role) def update_todolists(self, django_project): xml = ET.fromstring(self.bc.todo_lists(django_project.basecamp_id)) for todolist in xml.findall('todo-list'): bc_id = int(todolist.find('id').text) bc_name = todolist.find('name').text bc_position = todolist.find('position').text bc_description = '' bc_private = todolist.find('private').text if bc_private != "true": bc_private = False else: bc_private = True bc_completed = todolist.find('completed').text if bc_completed != "true": bc_completed = False else: bc_completed = True try: d_todolist = TodoList.objects.get(basecamp_id=bc_id) d_todolist.sort = bc_position d_todolist.name = bc_name d_todolist.description = bc_description d_todolist.private = bc_private d_todolist.completed = bc_completed d_todolist.save() self.stdout.write(u"Todolist %s updated." % d_todolist) except UnicodeDecodeError: self.stdout.write(u"Todolist updated.") except TodoList.DoesNotExist: d_todolist = TodoList( basecamp_id = bc_id, sort = bc_position, name = bc_name, description = bc_description, private = bc_private, project = django_project, completed = bc_completed, ) d_todolist.save() try: self.stdout.write(u"Todolist %s created." % d_todolist) except UnicodeDecodeError: self.stdout.write(u"Todolist created.") self.update_todos(bc_id) def update_todos(self, todo_id): try: todolists = TodoList.objects.filter(basecamp_id=todo_id) except TodoList.DoesNotExist: raise CommandError('Todo list whith given basecamp_id not found') for todolist in todolists: xml = ET.fromstring(self.bc.todo_list(todolist.basecamp_id)) for todoitem in xml.find('todo-items').findall('todo-item'): bc_id = int(todoitem.find('id').text) bc_name = todoitem.find('content').text bc_created_at = parser.parse(todoitem.find('created-at').text) bc_updated_at = parser.parse(todoitem.find('updated-at').text) try: bc_creator = Profile.objects.get(basecamp_id=todoitem.find('creator-id').text).user except Profile.DoesNotExist: bc_creator = None bc_due_at = todoitem.find('due-at').text try: bc_responsible_type = todoitem.find('responsible-party-type').text if (bc_responsible_type == 'Person'): try: bc_responsible = Profile.objects.\ get(basecamp_id=todoitem.find('responsible-party-id').text).user except Profile.DoesNotExist: bc_responsible = None else: bc_responsible = None except: bc_responsible = None if bc_due_at is not None: bc_due_at = parser.parse(bc_due_at) bc_position = todoitem.find('position').text bc_completed = todoitem.find('completed').text if bc_completed != "true": bc_completed = False else: bc_completed = True if bc_completed: bc_completed_at = parser.parse(todoitem.find('completed-at').text) try: bc_completer = Profile.objects.get(basecamp_id=todoitem.find('completer-id').text).user except Profile.DoesNotExist: bc_completer = None need_update_todo_item = False try: d_todoitem = TodoItem.objects.get(basecamp_id=bc_id) if d_todoitem.updated_at < bc_updated_at or self.forced: need_update_todo_item = True d_todoitem.sort = bc_position d_todoitem.name = bc_name d_todoitem.creator = bc_creator d_todoitem.completed = bc_completed d_todoitem.due_at = bc_due_at d_todoitem.todo_list = todolist d_todoitem.project = todolist.project d_todoitem.responsible = bc_responsible d_todoitem.created_at = bc_created_at d_todoitem.updated_at = bc_updated_at if bc_completed: d_todoitem.completed_at = bc_completed_at d_todoitem.completer = bc_completer d_todoitem.save() except: need_update_todo_item = True d_todoitem = TodoItem( basecamp_id = bc_id, sort = bc_position, name = bc_name, creator = bc_creator, completed = bc_completed, responsible = bc_responsible, due_at = bc_due_at, todo_list = todolist, project = todolist.project, created_at = bc_created_at, updated_at = bc_updated_at ) if bc_completed: d_todoitem.completed_at = bc_completed_at d_todoitem.completer = bc_completer d_todoitem.save() try: self.stdout.write(u"TodoItem %s created." % d_todoitem) except UnicodeDecodeError: self.stdout.write(u"TodoItem created.") if (need_update_todo_item): comments_xml = ET.fromstring(self.bc.todo_item_comments(d_todoitem.basecamp_id)) for comment in comments_xml.findall('comment'): bc_comment_id = comment.find('id').text try: Comment.objects.for_model(TodoItem)\ .get(object_pk = d_todoitem.pk, user_url = bc_comment_id) except Comment.DoesNotExist: try: bc_author = Profile.objects.get(basecamp_id=comment.find('author-id').text).user except Profile.DoesNotExist: bc_author = None django_comment = Comment( content_type = ContentType.objects.get_for_model(TodoItem), object_pk = d_todoitem.pk, user = bc_author, user_name = comment.find('author-name').text, user_email = '', user_url = bc_comment_id, comment = comment.find('body').text, submit_date = parser.parse(comment.find('created-at').text), site_id = settings.SITE_ID, is_public = True, is_removed = False, ) django_comment.save() def handle(self, *args, **options): project_id = False if args: project_id = int(args[0]) if options['forced']: self.forced = True if project_id: for company in Company.objects.filter(active=True).exclude(basecamp_api_key="", basecamp_id="", basecamp_base_url=""): username = password = company.basecamp_api_key base_url = company.basecamp_base_url self.bc = Basecamp(base_url, username, password) self.base_url = base_url self.company = company project = ET.fromstring(self.bc.project(project_id)) self.update_project(project) else: for company in Company.objects.filter(active=True).exclude(basecamp_api_key="").exclude(basecamp_id="").exclude(basecamp_base_url=""): username = password = company.basecamp_api_key base_url = company.basecamp_base_url self.bc = Basecamp(base_url, username, password) self.base_url = base_url self.company = company xml = ET.fromstring(self.bc.projects()) for project in xml.findall('project'): self.update_project(project)