Exemplo n.º 1
0
 def inject_forums(self):
     logging.info('Injecting forums...')
     with open('utils/dbinjector/forums.csv', newline='') as csv_file:
         csv_reader = csv.reader(csv_file, delimiter=',')
         for row in csv_reader:
             forum = Forum()
             forum.name = row[0]
             forum.save()
Exemplo n.º 2
0
def post(request):
    if request.method == "POST":
        question = request.POST.get("question", "")
        description = request.POST.get("description", "")
        author = request.user.username
        tags = request.POST.get("tags", "")
        newQues = Forum(question=question,
                        description=description,
                        author=author,
                        tags=tags)
        newQues.save()
        return HttpResponseRedirect('/forums/')
    else:
        raise Http404
Exemplo n.º 3
0
def create_forum():
    request_data = request.json
    fields_missing = require_values(request_data, ["title", "forumType", "parent"])
    if len(fields_missing):
        return response.errors({"fields_missing": fields_missing})
    invalid_values = {}
    if request_data["forumType"] not in Forum.ForumTypes.values:
        invalid_values[
            "forum_type"
        ] = f"forumTypes must be in [{Forum.ForumTypes.values}])"
    if not int(request_data["parent"]):
        invalid_values["parent"] = f"parent must be an integer"
    try:
        parent: Forum = get_objects_by_id(
            request_data["parent"], Forum, CacheKeys.FORUM_DETAILS.value
        )
    except Forum.DoesNotExist:
        invalid_values["parent"] = f"parent \"{request_data['parent']}\" does not exist"

    game = None
    if "game_id" in request_data:
        game: Game = get_objects_by_id(
            request_data["game_id"], Game, CacheKeys.GAME_DETAILS.value
        )
        if not game:
            invalid_values[
                "game_id"
            ] = f"game_id \"{request_data['game_id']}\" does not exist"
    if invalid_values:
        return response.errors({"invalid_values": invalid_values})

    forum_values = {
        "title": request_data["title"],
        "forumType": request_data["forumType"],
        "parent": parent,
    }
    if "description" in request_data:
        forum_values["description"] = request_data["description"]
    if game:
        forum_values["game"] = game

    forum = Forum(**forum_values)
    forum.save()
    forum.generate_heritage()
    forum.save()
    return response.success({"forum": {"id": forum.id, "title": forum.title}})
Exemplo n.º 4
0
def test_thread_model():
    forum = Forum(title='Test Forum', description='This is a Test Forum')
    forum.save()
    user = get_user_model().objects.create_user(
        username='******',
        email='*****@*****.**',
        password='******',
    )
    thread = Thread(title='A new thread',
                    text='The text of the thread',
                    forum=forum,
                    user=user)
    thread.save()
    assert thread.title == 'A new thread'
    assert thread.text == 'The text of the thread'
    assert thread.forum == forum
    assert thread.user == user
    assert thread.added
    assert thread.edited
    assert str(
        thread) == f'Thread: {thread.title} - (started by {thread.user})'
Exemplo n.º 5
0
def test_forum_model(add_forum):
    forum = Forum(title='Test Forum', description='This is a Test Forum')
    forum.save()
    assert forum.title == 'Test Forum'
    assert forum.description == 'This is a Test Forum'
    assert str(forum) == f'Forum: {forum.title}'
Exemplo n.º 6
0
        system_data["basics"] = None
    system = System(**{
        k: v
        for k, v in system_data.items() if k not in ["genres", "publisher"]
    })
    system.save()
    if system_data["publisher"]:
        publisher, _ = Publisher.objects.get_or_create(
            name=system_data["publisher"]["name"],
            defaults={"website": system_data["publisher"]["site"]},
        )
        system.publisher = publisher
    if system_data["genres"]:
        for genre_data in system_data["genres"]:
            genre, _ = Genre.objects.get_or_create(genre=genre_data)
            system.genres.add(genre)
    print(f"Created system: {system.name}")
print("\n")

with open("data/forums.json") as f:
    forums_data = json.load(f)

forums = {}
for forum_data in forums_data:
    if forum_data["parent"] is not None:
        forum_data["parent"] = forums[forum_data["parent"]]
    forum = Forum(**forum_data, createdAt=datetime.now())
    forum.save()
    forums[forum.id] = forum
print("Forums created\n")