def upload(request): if request.method == 'POST': # If the form has been submitted... form = UploadForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data # ... url = form.cleaned_data['url'] tags = form.cleaned_data['tags'].split() comment = form.cleaned_data['comment'] collection = Post.objects.filter(user=request.user, gif_url=url) if collection.exists(): message = 'You have already posted this gif.' return HttpResponse(message) else: p = Post(user=request.user, gif_url=url, comment=comment) p.save() for tag in tags: p.tags.create(tag=tag) return HttpResponseRedirect(reverse('user', args=[request.user.username])) else: form = UploadForm() # An unbound form return render(request, 'upload_form.html', { 'form': form, })
def run_bots(): count_added = 0 count_duplicates = 0 b = Bot.objects.order_by('last_run_at')[0] links = b.parse_url() for l in reversed(links): p = Post(user=b.user, gif_url=l) try: p.save() count_added += 1 except IntegrityError: connection._rollback() count_duplicates += 1 b.last_run_at = datetime.datetime.now() b.save() return "added: {0}, duplicates: {1}".format(count_added, count_duplicates)
def update(request): b = Bot.objects.order_by('last_run_at')[0] if datetime.timedelta(hours=settings.OLDEST_LAST_RUN_DELTA) < datetime.datetime.now()-b.last_run_at: count_added = 0 count_duplicates = 0 links = b.parse_url() for l in reversed(links): p = Post(user=b.user, gif_url=l) try: p.save() count_added += 1 except IntegrityError: connection._rollback() count_duplicates += 1 b.last_run_at = datetime.datetime.now() b.save() response = "added: {0}, duplicates: {1}".format(count_added, count_duplicates) else: response = "oldest bot updated less that {0} hours ago".format(settings.OLDEST_LAST_RUN_DELTA) return HttpResponse(response)