def fetch_members(self, board, trello_board): trello_members = trello_board.all_members() # For each member, check if he/she doesn't exist. In that case, create him/her for trello_member in trello_members: # If the member does not exist, create it with empty Trello credentials try: member = Member.objects.get( trello_member_profile__trello_id=trello_member.id) if self.debug: print(u"Member {0} already existed ".format( member.external_username)) except Member.DoesNotExist: member = Member(creator=self.member) member.save() trello_member_profile = TrelloMemberProfile( trello_id=trello_member.id, member=member, username=trello_member.username, initials=trello_member.initials) trello_member_profile.save() if self.debug: print(u"Member {0} created".format( member.external_username)) # Only add the board to the member if he/she has not it yet if not member.boards.filter(uuid=board.uuid).exists(): member.boards.add(board) if self.debug: print(u"Member {0} has role {1}".format( member.external_username, trello_member.member_type)) # If this member has no role in this board, add the role to the member if not member.roles.filter(board=board).exists(): if self.debug: print("Creating role {0} for {1}".format( trello_member.member_type, board.name)) member_role, created = MemberRole.objects.get_or_create( board=board, type=trello_member.member_type) member_role.members.add(member) # If this member has a role but is different from the role he/she has in Trello, # update his/her role elif member.roles.get( board=board).type != trello_member.member_type: if self.debug: print("Updating {0}'s role from {1} to {2} in {3}".format( member.external_username, member.roles.get(board=board).type, trello_member.member_type, board.name)) member.roles.clear() member_role, created = MemberRole.objects.get_or_create( board=board, type=trello_member.member_type) member_role.members.add(member)
def handle(self, *args, **options): if 'username' not in options or not options['username'] or len( options['username']) != 1: self.stdout.write(self.style.ERROR("username is mandatory")) return False if 'password' not in options or not options['password'] or len( options['password']) != 1: self.stdout.write(self.style.ERROR("password is mandatory")) return False username = options['username'][0] if User.objects.filter(username=username).exists(): self.stdout.write(self.style.ERROR("This username already exists")) return False password = options['password'][0] try: with transaction.atomic(): # Creation of user user = User(username=username, is_superuser=True, is_active=True) user.set_password(password) user.save() # Assignment of member member = Member(user=user) member.save() # Assignment of user to group administrators administrators = Group.objects.get( name=settings.ADMINISTRATOR_GROUP) administrators.user_set.add(user) except Exception as e: self.stdout.write( self.style.ERROR( "Supermember couldn't be created successfully because of exception {0}" .format(e))) # Everything went well self.stdout.write( self.style.SUCCESS( "Supermember with username {0} has been created successfully". format(username)))
def save(self, commit=False): member = Member() user = User( first_name=self.cleaned_data.get("first_name"), last_name=self.cleaned_data.get("last_name"), username=self.cleaned_data.get("username"), email=self.cleaned_data.get("email"), ) user.set_password(self.cleaned_data["password1"]) if commit: user.save() member.user = user member.max_number_of_boards = Member.DEFAULT_MAX_NUMBER_OF_BOARDS member.save() return member
def _create_comments(card): card_comments = [] # {u'type': u'commentCard', u'idMemberCreator': u'56e2ac8e14e4eda06ac6b8fd', # u'memberCreator': {u'username': u'diegoj5', u'fullName': u'Diego J.', u'initials': u'DJ', # u'id': u'56e2ac8e14e4eda06ac6b8fd', u'avatarHash': u'a3086f12908905354b15972cd67b64f8'}, # u'date': u'2016-04-20T23:06:38.279Z', # u'data': {u'text': u'Un comentario', u'list': {u'name': u'En desarrollo', u'id': u'5717fb3fde6bdaed40201667'}, # u'board': {u'id': u'5717fb368199521a139712f0', u'name': u'Test', u'shortLink': u'2CGPEnM2'}, # u'card': {u'idShort': 6, u'id': u'57180ae1ed24b1cff7f8da7c', u'name': u'Por todas', # u'shortLink': u'bnK3c1jF'}}, u'id': u'57180b7e25abc60313461aaf'} member_dict = {} local_timezone = pytz.timezone(settings.TIME_ZONE) card_deleted_comments = { comment.uuid: comment for comment in card.comments.all() } # Create each one of the comments for comment in card.trello_card.comments: # Author of the comment loaded using memoization trello_member_id = comment["idMemberCreator"] if trello_member_id not in member_dict: try: member_dict[trello_member_id] = Member.objects.get( trello_member_profile__trello_id=comment[ "idMemberCreator"]) # If the member doesn't exist, create it except Member.DoesNotExist as e: deleted_member = Member() deleted_member.save() try: trello_member_profile = TrelloMemberProfile.objects.get( trello_id=comment["idMemberCreator"], username=comment["memberCreator"]["username"], initials=comment["memberCreator"]["initials"]) except TrelloMemberProfile.DoesNotExist: trello_member_profile = TrelloMemberProfile( member=deleted_member, trello_id=comment["idMemberCreator"], username=comment["memberCreator"]["username"], initials=comment["memberCreator"]["initials"]) trello_member_profile.member = deleted_member trello_member_profile.save() member_dict[trello_member_id] = deleted_member author = member_dict[trello_member_id] # Comment uuid uuid = comment["id"] # Comment content content = comment["data"]["text"] # Comment creation datetime comment_naive_creation_datetime = datetime.strptime( comment["date"], '%Y-%m-%dT%H:%M:%S.%fZ') comment_creation_datetime = local_timezone.localize( comment_naive_creation_datetime) # Creation of the card comment try: card_comment = card.comments.get(uuid=uuid) card_comment.content = content card_comment.blocking_card = None del card_deleted_comments[uuid] except CardComment.DoesNotExist: card_comment = CardComment( uuid=uuid, card=card, board=card.board, author=author, creation_datetime=comment_creation_datetime, content=content) #print "{0} {1} {2}".format(card.name, card_comment.content, card_comment.creation_datetime) # Check if comment has a blocking card blocking_card = card_comment.blocking_card_from_content if blocking_card: card_comment.blocking_card = blocking_card card_comment.save() # Create card comment list card_comments.append(card_comment) # Delete all card comments that are not present in trello.com for comment_uuid, comment in card_deleted_comments.items(): comment.delete() for trello_member_id, member in member_dict.items(): if not card.members.filter( trello_member_profile__trello_id=trello_member_id).exists( ): card.members.add(member) return card_comments