def _create_default_topic(user): """ Create a default topic for the given user if one doesn't already exist. """ try: existing_topic = Topic.objects.get(user=user, default=True) except Topic.DoesNotExist: existing_topic = None if existing_topic == None: topic = Topic() topic.user = user topic.active = True topic.default = True topic.num_views = 0 topic.hide_username = False topic.hidden_url = hiddenURLs.generate(user) # URL for user, not topic. topic.created_at = datetime.datetime.utcnow() topic.updated_at = datetime.datetime.utcnow() topic.save()
def create(session, topic_name=None): """ Create a new topic for the currently logged-in user. """ raise UnauthorizedException() # Disable for now. if session == None: raise InvalidParametersException() sessionHandler.validate(session) user = sessionHandler.get_user(session) if topic_name == "": topic_name = None if topic_name == None: # Create a new random name for this topic. while True: topic_name = utils.random_letters_and_digits(min_length=4, max_length=6) if not Topic.objects.filter(user=user,name=topic_name).exists(): break _check_topic_name(topic_name) try: existing_topic = Topic.objects.get(user=user, name=topic_name) except Topic.DoesNotExist: existing_topic = None if existing_topic != None: raise DuplicateTopicNameException() topic = Topic() topic.user = user topic.name = topic_name topic.active = True topic.num_views = 0 topic.hide_username = False topic.hidden_url = hiddenURLs.generate(user) # URL for user, not topic. topic.save() return topic.to_dict()
def handle(self, *args, **kwargs): """ Run our custom management command. """ for topic in Topic.objects.all(): topic.hidden_url = hiddenURLs.generate(topic.user) topic.save()