def fork_snippet(snippet_uuid): to_fork = get_snippet_by_uuid(snippet_uuid) if to_fork.is_private() and to_fork.user != current_user: raise Unauthorized("This snippet is private.") new_snippet = Snippet(to_fork.body) new_snippet.title = to_fork.title new_snippet.language = to_fork.language new_snippet.parent_fork = to_fork new_snippet.user = current_user db.session.add(new_snippet) db.session.commit() return redirect( url_for('snippets.show_snippet', snippet_uuid=new_snippet.get_uuid()))
def index(): """Route allows users to create a new snippet""" snippet_form = SnippetForm() populate_choice_field(snippet_form) if snippet_form.validate_on_submit(): s = Snippet(snippet_form.snippet.data) if snippet_form.title.data: s.title = snippet_form.title.data s.language = Language.query.get(snippet_form.language.data) if not current_user.is_anonymous(): s.user = current_user s.private = snippet_form.private.data db.session.add(s) db.session.commit() return redirect( url_for('snippets.show_snippet', snippet_uuid=s.get_uuid())) if current_user.is_authenticated() and current_user.default_language: snippet_form.language.default = current_user.default_language_id snippet_form.process() return render_template('snippets/compose.html', form=snippet_form)
import json import sys import os pwd = os.path.dirname(os.path.realpath(__file__)) sys.path.append(pwd + "../") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MockServer.settings") import django django.setup() from app.models import Snippet p = os.path.join(os.getcwd(), 'mock.json') f = open(p, 'r').read().decode('utf-8') for [k, v] in dict.items(json.loads(f)): try: snippet = Snippet.objects.get(url=k) except Snippet.DoesNotExist: snippet = Snippet(url=k) for [k2, v2] in dict.items(v): if (k2 == 'data'): snippet.code = json.dumps(v2) if (k2 == 'msg'): snippet.title = v2 if (k2 == 'sleep'): snippet.sleep = v2 snippet.save()