Beispiel #1
0
def newThread(request):
    langCode = request.build_absolute_uri().split("/")[-4]
    if not request.session.has_key('username'):
        return redirect('/' + langCode + '/login')
    if request.method == 'POST':
        thread_form = ThreadForm(request.POST)
        post_form = PostForm(request.POST)
        if thread_form.is_valid() and post_form.is_valid():
            thread = Thread()
            thread.title = thread_form.cleaned_data["title"]
            thread.description = thread_form.cleaned_data["description"]
            thread.save()
            post = Post()
            post.content = post_form.cleaned_data["content"]
            post.date = timezone.now()
            post.user = User.objects.get(username=request.session["username"])
            post.positionInThread = 0
            post.thread = thread
            post.save()
            thread.originalPost = post
            thread.save()
            return redirect("/" + langCode + "/forum/thread/" + str(thread.pk))
    else:
        # GET
        return render(request, 'forum/newthread.html', {
            "thread_form": ThreadForm(),
            "post_form": PostForm()
        })
			firstname = user.split(" ")[0]
			lastname = user.split(" ")[1]
		except:
			firstname = "Unknown"
			lastname = "User"
		u = User.objects.create_user(first_name = firstname, last_name = lastname, username = user, password="******", email="*****@*****.**")
		u.save()

with open('commotion-discuss.txt_sorted.csv', 'rb') as csvfile:
	forum_data_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
		# Add threads
	threads = []
	for index, row in enumerate(forum_data_reader):
		if index >0:
			if row[2] not in threads:
				threads.append(row[2])
				creator = User.objects.filter(username = row[5]).first()
				forum = Forum.objects.filter(title=row[1]).first()
				title = row[2]
				t = Thread(title =title, creator = creator, forum = forum)
				t.save()

# Add posts
with open('commotion-discuss.txt_sorted.csv', 'rb') as csvfile:
	forum_data_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
	for index, row in enumerate(forum_data_reader):
		if index >0:
			creator = User.objects.filter(username = row[5]).first()
			thread = Thread.objects.filter(title = row[2]).first()
			p = Post(creator=creator, thread=thread, title="", body=row[6])
			p.save()