def select_issues_from_preselection(request, issues, cancel, post_process_selection=None, collection_list=None): if not issues.exists(): raise ErrorWithMessage("No issues to select from.") data = { 'issue': True, 'allowed_selects': [ 'issue', ], 'return': add_selected_issues_to_collection, 'cancel': cancel } if post_process_selection: data['post_process_selection'] = post_process_selection if collection_list: data['collection_list'] = collection_list select_key = store_select_data(request, None, data) context = { 'select_key': select_key, 'multiple_selects': True, 'item_name': 'issue', 'plural_suffix': 's', 'no_bulk_edit': True, 'all_pre_selected': True, 'heading': 'Issues' } return paginate_response(request, issues, 'gcd/search/issue_list.html', context, per_page=issues.count())
def add_selected_issues_to_collection(request, data): selections = data['selections'] issues = Issue.objects.filter(id__in=selections['issue']) if 'story' in selections: issues |= Issue.objects.filter(story__id__in=selections['story']) issues = issues.distinct() if not issues.count(): raise ErrorWithMessage("No issues were selected.") post_process_selection = data.get('post_process_selection', None) if 'confirm_selection' in request.POST: collection_id = int(request.POST['collection_id']) return add_issues_to_collection(request, collection_id, issues, urlresolvers.reverse('view_collection', kwargs={'collection_id': collection_id}), post_process_selection=post_process_selection) else: if 'collection_list' in data: collection_list = data['collection_list'] else: collection_list = request.user.collector.ordered_collections() context = { 'item_name': 'issue', 'plural_suffix': 's', 'no_bulk_edit': True, 'heading': 'Issues', 'confirm_selection': True, 'collection_list': collection_list } return paginate_response(request, issues, 'gcd/search/issue_list.html', context, per_page=issues.count())
def move_item(request, item_id, collection_id): if request.method != 'POST': raise ErrorWithMessage("This page may only be accessed through the " "proper form.") from_collection = get_object_or_404(Collection, id=collection_id) item = get_item_for_collector(item_id, request.user.collector) check_item_is_in_collection(request, item, from_collection) collection_form = CollectionSelectForm(request.user.collector, None, request.POST) if collection_form.is_valid(): to_collection_id = collection_form.cleaned_data['collection'] to_collection = get_object_or_404(Collection, id=to_collection_id) check_item_is_not_in_collection(request, item, to_collection) if 'move' in request.POST: from_collection.items.remove(item) item.collections.add(to_collection) messages.success(request, _("Item moved between collections")) if to_collection.own_used and to_collection.own_default is not None: if to_collection.own_default != item.own: item.own = to_collection.own_default item.save() messages.warning( request, mark_safe( _("Item status for own/want differed from" " default of new collection <b>%s</b> and " "was changed." % esc(to_collection.name)))) return HttpResponseRedirect( urlresolvers.reverse('view_item', kwargs={ 'item_id': item_id, 'collection_id': to_collection_id })) elif 'copy' in request.POST: item.collections.add(to_collection) messages.success(request, _("Item copied between collections")) if to_collection.own_used and \ to_collection.own_default is not None: if to_collection.own_default != item.own: messages.warning( request, _("Own/want default for new " "collection differs from item status.")) else: messages.error(request, _("Item unchanged")) else: messages.error(request, _("Item unchanged")) return HttpResponseRedirect( urlresolvers.reverse('view_item', kwargs={ 'item_id': item_id, 'collection_id': collection_id }))
def add_single_issue_to_collection(request, issue_id): if not request.POST: raise ErrorWithMessage("No collection was selected.") issue = get_object_or_404(Issue, id=issue_id) collection, error_return = get_collection_for_owner( request, collection_id=int(request.POST['collection_id'])) if not collection: return error_return collected = create_collection_item(issue, collection) messages.success( request, mark_safe("Issue <a href='%s'>%s</a> was added to your <b>%s</b> " "collection." % (collected.get_absolute_url(collection), esc(issue), esc(collection.name)))) request.session['collection_id'] = collection.id return HttpResponseRedirect( urlresolvers.reverse('show_issue', kwargs={'issue_id': issue_id}))
def delete_item(request, item_id, collection_id): if request.method != 'POST': raise ErrorWithMessage("This page may only be accessed through the " "proper form.") collection = get_object_or_404(Collection, id=collection_id) item = get_item_for_collector(item_id, request.user.collector) check_item_is_in_collection(request, item, collection) position = collection.items.filter(issue__series__sort_name__lte= item.issue.series.sort_name)\ .exclude(issue__series__sort_name= item.issue.series.sort_name, issue__series__year_began__gt= item.issue.series.year_began)\ .exclude(issue__series_id= item.issue.series.id, issue__sort_code__gte= item.issue.sort_code).count() collection.items.remove(item) messages.success( request, mark_safe( _("Item <b>%s</b> removed from %s" % (esc(item.issue.full_name()), esc(collection))))) if not item.collections.count(): if item.acquisition_date: item.acquisition_date.delete() if item.sell_date: item.sell_date.delete() item.delete() else: return HttpResponseRedirect( urlresolvers.reverse('view_item', kwargs={ 'item_id': item.id, 'collection_id': item.collections.all()[0].id })) return HttpResponseRedirect( urlresolvers.reverse('view_collection', kwargs={'collection_id': collection_id}) + "?page=%d" % (position / DEFAULT_PER_PAGE + 1))
def import_items(request): if 'import_my_issues' in request.FILES: # real file to be able to use pythons Universal Newline Support tmpfile_handle, tmpfile_name = tempfile.mkstemp(".mycomics_import") for chunk in request.FILES['import_my_issues'].chunks(): os.write(tmpfile_handle, chunk) os.close(tmpfile_handle) number_of_lines = file_len(tmpfile_name) if number_of_lines > 501: messages.error( request, _('More than 500 lines. Please split' ' the import file into smaller chunks.')) return HttpResponseRedirect( urlresolvers.reverse('collections_list')) rawdata = open(tmpfile_name, 'rb').read() result = chardet.detect(rawdata) encoding = result['encoding'] tmpfile = open(tmpfile_name, encoding=encoding) upload = csv.reader(tmpfile) issues = Issue.objects.none() line = next(upload) not_found = "" if line[0] == 'Title' and line[1] == 'Issue Number': comicbookdb = True publisher_col = -1 for i in range(2, len(line)): if line[i] == 'Publisher': publisher_col = i if publisher_col < 0: raise ErrorWithMessage("We cannot find 'Publisher' in the " "list of columns") else: comicbookdb = False upload = csv.reader(tmpfile) for line in upload: if len(line) == 0: break issue = Issue.objects.none() if comicbookdb: if line[0][-1] == ')' and line[0][-6] == '(': series = line[0][:-6].strip() else: raise ErrorWithMessage("Cannot find '(year)')") try: number = line[1].strip().lstrip('#') publisher = line[publisher_col].strip() except IndexError: raise ErrorWithMessage("Not enough columns") if publisher == "Image Comics Inc.": publisher = "Image" elif publisher == "DC Comics": publisher = "DC" elif publisher == "Valiant Entertainment LLC": publisher = "Valiant Entertainment" elif publisher == "Archie Comic Publications Inc.": publisher = "Archie" elif publisher == "IDW Publishing": publisher = "IDW" issue = Issue.objects.filter(series__name=series, number=number, series__publisher__name=publisher) else: if len(line) >= 2: series = line[0].strip() number = line[1].strip().lstrip('#') if series != '' and number != '': issue = Issue.objects.filter( series__name__icontains=series, number=number) else: issue = Issue.objects.none() if len(line) >= 3: publisher = line[2].strip() if publisher != '': issue = issue.filter( series__publisher__name__icontains=publisher) if len(line) >= 4: language_code = line[3].strip() if language_code != '': issue = issue.filter( series__language__code__iexact=language_code) if issue.count() == 0: not_found += '","'.join(line) + '\n' else: issues = issues | issue if 'import_my_issues_base_only' in request.POST: issues = issues.filter(variant_of=None) issues = issues.distinct() tmpfile.close() os.remove(tmpfile_name) cancel = HttpResponseRedirect(urlresolvers.reverse('collections_list')) return select_issues_from_preselection(request, issues, cancel, not_found=not_found)
def check_item_is_not_in_collection(request, item, collection): if item in collection.items.all(): raise ErrorWithMessage("This item already does belong to given " "collection.")
def check_item_is_in_collection(request, item, collection): if item not in collection.items.all(): raise ErrorWithMessage("This item doesn't belong to given collection.")