<commit_before># -*- coding: utf-8 -*-

import time
import hashlib
import requests
import sys
import json
import re

from optparse import make_option

from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand, CommandError
from django.utils.translation import ugettext as _

from moocng import mongodb
from moocng.badges.models import Badge
from moocng.courses.models import Course
from moocng.courses.utils import get_groups_by_course



class Command(BaseCommand):

    help = ("Creates forum topics for each group in a course that has no forum topic yet.")

    option_list = BaseCommand.option_list + (
        make_option('-c', '--courses',
                    action='store',
                    dest='course_pk',
                    default="",
                    help='Course pk'),
    )

    def error(self, message):
        self.stderr.write("%s\n" % message.encode("ascii", "replace"))

    def message(self, message):
        self.stdout.write("%s\n" % message.encode("ascii", "replace"))

    def handle(self, *args, **options):

        if not options["course_pk"]:
            raise CommandError("-c course_pk is required")

        groups = list(get_groups_by_course(options["course_pk"]))
        print "Groups: " + str(len(groups))
        for group in groups:
            if "forum_slug" in group and group["forum_slug"] is not None:
                print "\nGroup " + str(group["name"]) + " already has forum topic"
            else:
                print "\nGroup " + str(group["name"]) + " has NO forum topic"

                course = Course.objects.filter(id=int(options["course_pk"]))[:1].get()
                split_result = re.split(r'([0-9]+)', course.forum_slug)
                cid = split_result[1]

                content = _(u"This is the topic for ") + group["name"] + _(u" where you can comment and help other team members")
                data = {
                    "uid": 1,
                    "title": group["name"],
                    "content": content,
                    "cid": cid
                }
                timestamp = int(round(time.time() * 1000))
                authhash = hashlib.md5(settings.FORUM_API_SECRET + str(timestamp)).hexdigest()
                headers = {
                    "Content-Type": "application/json",
                    "auth-hash": authhash,
                    "auth-timestamp": timestamp
                }
                
                if settings.FEATURE_FORUM:
                    try:
                        r = requests.post(settings.FORUM_URL + "/api2/topics", data=json.dumps(data), headers=headers)<commit_msg>Fix group creation<commit_after># -*- coding: utf-8 -*-

import time
import hashlib
import requests
import sys
import json
import re

from optparse import make_option

from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand, CommandError
from django.utils.translation import ugettext as _

from moocng import mongodb
from moocng.badges.models import Badge
from moocng.courses.models import Course
from moocng.courses.utils import get_groups_by_course



class Command(BaseCommand):

    help = ("Creates forum topics for each group in a course that has no forum topic yet.")

    option_list = BaseCommand.option_list + (
        make_option('-c', '--courses',
                    action='store',
                    dest='course_pk',
                    default="",
                    help='Course pk'),
    )

    def error(self, message):
        self.stderr.write("%s\n" % message.encode("ascii", "replace"))

    def message(self, message):
        self.stdout.write("%s\n" % message.encode("ascii", "replace"))

    def handle(self, *args, **options):

        if not options["course_pk"]:
            raise CommandError("-c course_pk is required")

        groups = list(get_groups_by_course(options["course_pk"]))
        print "Groups: " + str(len(groups))
        for group in groups:
            if "forum_slug" in group and group["forum_slug"] is not None:
                print "\nGroup " + str(group["name"]) + " already has forum topic"
            else:
                print "\nGroup " + str(group["name"]) + " has NO forum topic"

                course = Course.objects.filter(id=int(options["course_pk"]))[:1].get()
                split_result = re.split(r'([0-9]+)', course.forum_slug)
                cid = split_result[1]
                print split_result
                print cid

                content = _(u"This is the topic for ") + group["name"] + _(u" where you can comment and help other team members")
                data = {
                    "uid": 1,
                    "title": group["name"],
                    "content": content,
                    "cid": cid
                }
                timestamp = int(round(time.time() * 1000))
                authhash = hashlib.md5(settings.FORUM_API_SECRET + str(timestamp)).hexdigest()
                headers = {
                    "Content-Type": "application/json",
                    "auth-hash": authhash,
                    "auth-timestamp": timestamp
                }
                
                if settings.FEATURE_FORUM:
                    try:
                        r = requests.post(settings.FORUM_URL + "/api2/topics", data=json.dumps(data), headers=headers)
{'fraction_non_alphanumeric': 0.11520058888479941, 'fraction_numerical': 0.004416635995583364, 'mean_word_length': 1.9330814894765245, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '413938', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1438, 'n_words': 387}