def add_snippet(request): """ Allows a user to add a Snippet to the database. Context:: form The form to add the Snippet. Template:: cab/add_snippet_form.html """ original_id = request.GET.get('oid', None) if request.method == 'POST': form = forms.AddSnippetForm(request.POST) if form.is_valid(): new_snippet = Snippet(title=form.cleaned_data['title'], description=form.cleaned_data['description'], code=form.cleaned_data['code'], tag_list=form.cleaned_data['tag_list'], language_id=form.cleaned_data['language'], author=request.user) if original_id: new_snippet.original_id = original_id new_snippet.save() return HttpResponseRedirect('/kgp/cab/') else: form = forms.AddSnippetForm() return render_to_response('cab/add_snippet_form.html', { 'form': form }, context_instance=RequestContext(request))
def edit_snippet(request, snippet_id=None, template_name='cab/edit_snippet.html'): if not request.user.is_active: return HttpResponseForbidden() if snippet_id: snippet = get_object_or_404(Snippet, pk=snippet_id) if request.user.id != snippet.author.id: return HttpResponseForbidden() else: template_name = 'cab/add_snippet.html' snippet = Snippet(author=request.user, language=Language.objects.get(name='Python')) if request.method == 'POST': form = SnippetForm(instance=snippet, data=request.POST) if form.is_valid(): snippet = form.save() messages.info(request, 'Your snippet has been saved') return redirect(snippet) else: form = SnippetForm(instance=snippet) return render(request, template_name, {'form': form})
def edit_snippet(request, snippet_id=None, template_name='cab/edit_snippet.html'): if snippet_id: snippet = get_object_or_404(Snippet, pk=snippet_id) if request.user.id != snippet.author.id: return HttpResponseForbidden() else: template_name = 'cab/add_snippet.html' snippet = Snippet(author=request.user, language=Language.objects.get(name='Python')) if request.method == 'POST': form = SnippetForm(instance=snippet, data=request.POST) if form.is_valid(): snippet = form.save() return HttpResponseRedirect(snippet.get_absolute_url()) else: form = SnippetForm(instance=snippet) return render_to_response(template_name, {'form': form}, context_instance=RequestContext(request))
def handle(self, *args, **options): # # neccessary: #Site(domain="pygameweb.no-ip.org", name="pygame web").save() #Site(domain="127.0.0.1:8000", name="localhost").save() testusers = [] for i in xrange(30): u, c = User.objects.get_or_create(username="******"%i) u.save() testusers.append(u) for obj in Project.objects.all(): obj.delete() tags = ("test %s"%words(15)).split() pnames = [] for i in xrange(20): admins = [random.choice(testusers) for i in xrange(random.randint(2, 4))] members = [random.choice(testusers) for i in xrange(random.randint(2, 6))] while True: pname = words(2) if pname not in pnames: pnames.append(pname) break p = Project(name=pname, markup="rst", description="\n\n".join(paragraphs(4)), summary=paragraph(), homepage_link = "http://example.com", creator=admins[0], last_editor=admins[1], tags=",".join([random.choice(tags) for i in xrange(random.randint(3, 10))]),) p.save() p.admins = admins p.members = members p.save() # s = Screenshot(name="test", project=p, # uploaded_by=User.objects.all()[0], # description="testimage", # image="screenshots/logo.png") # s.save() k = 1 for j in xrange(random.choice((0,0,0,1,1,1,1,2,2,2,3,3,4))): r = Release(project=p, version="%i.%i %s"%(k, random.randint(0,9), words(1)), markup="rst", summary="\n\n\n".join(paragraphs(2, False)), changelog=paragraph(), notes_url = "http://example.org", download_url = "http://example.org", creator=members[0], last_editor=members[1]) r.save() for obj in News.objects.all(): obj.delete() news_admins = User.objects.order_by("?")[:4] for i in xrange(20): n = News(title=words(random.randint(2,5)), slug=slugify("News Test %i"%i), markup="rst", creator=random.choice(news_admins), last_editor=User.objects.all()[0], content=""" Hello, .. sourcecode:: python while True: print "Hello World!" *this site is* `pony powered <http://www.djangopony.com/>`_ %s""" % "\n\n".join(paragraphs(3)) # creator=User.objects.all()[0], # last_editor=User.objects.all()[0] ) n.save() py, c = Language.objects.get_or_create(name="Python", language_code="py", file_extension="py", mime_type="application/x-python") py.save() for i in xrange(10): s = Snippet(title=words(random.randint(3, 8)), language=py, author=random.choice(testusers), tags=" ".join([random.choice(tags) for i in xrange(random.randint(2, 8))]), description="".join(paragraphs(2)), code=SAMPLE_CODE, markup="rst" ) s.save() for y in xrange(3): b = Bookmark(user=random.choice(testusers), snippet=s) b.save()