def post_ad(request): files = {'images': [], 'video': []} if request.method == "POST": from_class = Ad.get_form_class(request.POST.get('category'), request.POST.get('sub_category'), request.POST.get('offering') == "True") form = from_class(request.POST, anonym=(not request.user.is_authenticated())) if form.is_valid(): obj = form.save(commit=False) # returns unsaved instance if request.user.is_authenticated(): obj.user = request.user if not obj.district: obj.town = Town.objects.get(pk=request.session.get('TOWN_SELECTED', 1)) obj.save() obj.blocked = WordAnalyser.block_object(obj, Ad.fields_for_analyse) if obj.blocked is not False: obj.save() else: try: obj.user = get_user_model().objects.get(email=form.cleaned_data['email']) except get_user_model().DoesNotExist: obj.user = get_user_model().objects.create_user(form.cleaned_data['email'], None, is_active=False) finally: obj.save_as_disabled(form.cleaned_data['email']) tasks.find_similar(obj.id) Ad.save_files(request.POST, obj) if obj.disabled: return redirect('activate_ad', ad_id=obj.id) else: messages.success(request, _('Your add was successfully added.')) return redirect(obj.get_absolute_url()) else: for img_id in request.POST.getlist('images[]'): file_obj = ImageAttachment.objects.get(pk=img_id) if file_obj: files['images'].append(file_obj) for vid_id in request.POST.getlist('video[]'): file_obj = VideoAttachment.objects.get(pk=vid_id) if file_obj: files['video'].append(file_obj) else: from_class = Ad.get_form_class(offering=True) form = from_class(anonym=(not request.user.is_authenticated())) return render(request, 'ads/post_ad.html', {'form': form, 'files': files, 'images_limit': settings.UPLOAD_IMAGES_LIMIT - len(files['images']), 'video_limit': settings.UPLOAD_VIDEO_LIMIT - len(files['video'])})
def edit_ad(request, ad_id=None): instance = get_object_or_404(Ad, id=ad_id, user_id=request.user.id) if request.method == "POST": cache_key = 'detail_%s' % ad_id cache.delete(cache_key) offering = request.POST.get('offering') if offering: offering = True if offering == 'True' else False from_class = Ad.get_form_class(request.POST.get('category'), request.POST.get('sub_category'), (offering if offering is not None else instance.offering)) form = from_class(request.POST, instance=instance) if form.is_valid(): obj = form.save(commit=False) obj.blocked = WordAnalyser.block_object(obj, Ad.fields_for_analyse) obj.save() tasks.find_similar(obj.id) Ad.save_files(request.POST, obj) messages.success(request, _('Your add was successfully updated.')) return redirect(obj.get_absolute_url()) else: from_class = Ad.get_form_class(instance.category, instance.sub_category if hasattr(instance, 'sub_category') else None, instance.offering) form = from_class(instance=instance) files = {'images': instance.imageattachment_set.all(), 'video': instance.videoattachment_set.all()} return render(request, 'ads/post_ad.html', {'form': form, 'files': files, 'images_limit': settings.UPLOAD_IMAGES_LIMIT - len(files['images']), 'video_limit': settings.UPLOAD_VIDEO_LIMIT - len(files['video'])})