def my_opportunity_edit(request, slug): opportunity = get_object_or_404(UmmeliOpportunity, slug=slug, owner=request.user) if request.method == 'POST': form = OpportunityEditForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] description = form.cleaned_data['description'] opportunity.title = title opportunity.description = description opportunity.province = [form.cleaned_data['province'], ] opportunity.save() messages.success(request, 'Your opportunity has been updated') return redirect(reverse('my_community')) else: if opportunity.province.exists(): init_pk = opportunity.province.latest('pk').pk else: init_pk = 0 initial = {'title': opportunity.title, 'description': opportunity.description, 'province': init_pk, } form = OpportunityEditForm(initial=initial) print initial return render(request, 'opportunities/opportunity_edit.html', {'form': form})
def opportunity_create(request, slug=None): if request.method == "POST": form = OpportunityEditForm(request.POST) if form.is_valid(): title = form.cleaned_data["title"] description = form.cleaned_data["description"] # check if the user hasn't placed the exact same job article # with the exact same information in the last 5 minutes to prevent # duplicates delta = datetime.now() - timedelta(minutes=5) duplicate = UmmeliOpportunity.permitted.filter( owner=request.user, created__gte=delta, title=title, description=description ).exists() daily_limit_exceeded = ( UmmeliOpportunity.objects.filter(owner=request.user, created__gte=datetime.now().date()).count() >= settings.COMMUNITY_BOARD_POST_LIMIT ) if daily_limit_exceeded: msg = "You can only post to the community board %s times a day" messages.error(request, msg % settings.COMMUNITY_BOARD_POST_LIMIT) return redirect(reverse("index")) if not duplicate and not daily_limit_exceeded: model = form.get_model() opportunity = model(title=title, description=description) opportunity.owner = request.user opportunity.state = "published" opportunity.is_community = True opportunity.publish_on = datetime.now() opportunity.save() opportunity.sites.add(site) opportunity.province.add(form.cleaned_data["province"]) messages.success(request, "Your opportunity has been added") return redirect(reverse("my_community_opportunities")) else: form = OpportunityEditForm() return render(request, "opportunities/opportunity_create.html", {"form": form})