def create_copy(self, user): # region Copy Deck new_deck = Deck.objects.create( name=self.name, deck_type=self.deck_type, is_private=UserProfile.get_deck_private(user), image_url=self.image_url, description=self.description, color_id=self.color_id, created_by=self.created_by, deck_user=user.username, is_pre_con=False, card_list=self.card_list, side_list=self.side_list ) # endregion # region Copy Cards deck_cards = DeckCard.objects.deck_card_by_deck(self.id) deck_cards_list = list(deck_cards.split("},")) if deck_cards_list[0] == '': deck_cards_list = [] for card in deck_cards_list: card_json = json.loads(card) DeckCard.objects.create( deck=new_deck.id, card_oracle=card_json['oracle_id'], quantity=card_json['quantity'], sideboard=card_json['sideboard'], commander=card_json['commander'], ) # endregion return new_deck
def get(self, request): """Displays new deck page Redirects to new deck page @param request: :todo: Finish new deck page """ SessionManager.clear_other_session_data(request, SessionManager.Profile) user_id = int(request.GET.get('user_id', request.user.id)) deck_id = int(request.GET.get('deck_id', -1)) try: deck_obj = Deck.objects.get_deck(request.user.username, deck_id) deck_type_obj = Deck.objects.get_deck_type(deck_id) deck_private = deck_obj.is_private if deck_obj.deck_user != request.user.username: deck_obj = deck_obj.create_copy(request.user) messages.success(request, "Deck copied to your profile.") return HttpResponseRedirect( reverse('modify_deck') + '?user_id=' + str(user_id) + '&deck_id=' + str(deck_obj.id)) except ObjectDoesNotExist: deck_obj = "new" deck_type_obj = '{"type_id": "1"}' deck_private = UserProfile.get_deck_private(request.user) deck_types = DeckType.objects.get_types() deck_type_split = list(deck_types.split("},")) deck_commander = DeckCard.objects.deck_card_by_deck_side( deck_id, False, True) deck_cards = DeckCard.objects.deck_card_by_deck_side( deck_id, False, False) side_cards = DeckCard.objects.deck_card_by_deck_side( deck_id, True, False) commanders = list(deck_commander.split("},")) if commanders[0] == '': commanders = [] deck_cards_list = list(deck_cards.split("},")) if deck_cards_list[0] == '': deck_cards_list = [] side_cards_list = list(side_cards.split("},")) if side_cards_list[0] == '': side_cards_list = [] font_family = UserProfile.get_font(request.user) should_translate = UserProfile.get_translate(request.user) context = { 'font_family': font_family, 'should_translate': should_translate, 'deck_obj': deck_obj, 'deck_types': deck_type_split, 'deck_id': deck_id, 'is_private': deck_private, 'deck_type_obj': deck_type_obj, 'commander': commanders, 'commander_len': len(deck_commander), 'deck_cards': deck_cards_list, 'side_cards': side_cards_list, 'user_id': user_id } return render(request, 'Users/Profile/ProfileDecks/modify_deck.html', context)