Beispiel #1
0
def login(request):
    """
    desc: Validate login credentials
    params: str username, str password
    return: bool success
    """
    response = {
        'success': False,
        'username': '',
    }

    # Validate input
    if valid_request(request, need_auth=False):
        username = request.POST['username'].lower()
        password = request.POST['password']
        if username and password:
            user = auth.authenticate(username=username, password=password)
            
            # Login is successful
            if user is not None and user.is_active:
                auth.login(request, user)
                response['username'] = user.username
                response['success'] = True

    # Return results
    return JsonResponse(response)
Beispiel #2
0
def get_nav(request):
    """
    desc: Get a list navigation menus
    params: void
    return: array({str name, str url}) items
    """
    response = {
        'success': False,
        'items': [],
        'data': {
            # Index number of the items to indicate the login button.
            'login': -1,
            'logout': -1,
        },
    }
    items = []
    data = {}
    
    if valid_request(request, method='GET'):
        name = '{0} {1}'.format(request.user.first_name, request.user.last_name)
        if name == ' ':
            name = request.user.username
            
        items.append({'name': 'Index', 'url': reverse('index')})
        items.append({'name': 'Home', 'url': reverse('tag', args=[1])})
        items.append({'name': 'Logout', 'url': ''})
        items.append({'name': name, 'url': ''})
        data['logout'] = 'Logout'
        
        response['success'] = True
    
    else:
        items.append({'name': 'Home', 'url': reverse('index')})
        items.append({'name': 'Login', 'url': ''})
        data['login'] = '******'
        
        response['success'] = True
    
    response['items'] = items
    response['data'] = data
    
    # Return results
    return JsonResponse(response)
Beispiel #3
0
def register(request):
    """
    desc: Register a new user
    params: str username, str password, str passconf, str email
    return: bool success
    """
    response = {
        'success': False,
        'username': '',
    }

    # Validate input
    if valid_request(request, need_auth=False):
            
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        email = request.POST['email'].lower()
        username = request.POST['username'].lower()
        password = request.POST['password']
        if first_name and last_name and email and username and password:
            users = User.objects.filter(
                Q(username=username) | (
                    Q(first_name=first_name) &
                    Q(last_name=last_name)
                )
            ).count()
            if users == 0:
                user = User.objects.create_user(username, email, password)
                user.first_name = first_name
                user.last_name = last_name
                user.save()
                auth.authenticate(username=username, password=password)
                response['username'] = user.username
                response['success'] = True
                
    
    # Return results
    return JsonResponse(response)
Beispiel #4
0
def create_tag(request):
    """
    desc: Create a new tag
    params: str tag_name, array(int tag_id) sub_tags,
    return: bool success, str tag_name, id tag_id
    """
    response = {
        'success': False,
        'tag_name': '',
        'tag_id': -1
    }
    
    # Validate input
    if valid_request(request):
        tag_name = request.POST['tag_name']
        parent_id = request.POST['parent_id']
        post_date = timezone.now()
        
        if tag_name and parent_id:
            # Create new tag and update parent tag with new tag.
            parent_tag = Tag.objects.filter(id=parent_id)
            new_tag = Tag(
                name=tag_name
            )
            new_tag.save()
            
            parent_tag.tags.add(new_tag)
            parent_tag.save()
            
            # Set response
            response['tag_name'] = new_tag.name
            response['tag_id'] = new_tag.id
            
            # Everything was successful!
            response['success'] = True
    
    # Return results
    return JsonResponse(response)
Beispiel #5
0
def create_topic(request):
    """
    desc: Create a new topic
    params: str topic_title, post_text, int tag_id
    return: bool success, int topic_id, str topic_title, str topic_author
    """
    response = {
        'success': False,
        'topic_id': -1,
        'topic_title': '',
        'topic_author': '',
        'topic_date': '',
    }

    # Validate input
    if valid_request(request):
        topic_title = request.POST['topic_title']
        tag_id = request.POST['tag_id']
        post_text = request.POST['post_text']
        date = timezone.now()
        if topic_title and tag_id and post_text:
            tag = Tag.objects.get(pk=tag_id)
            new_topic = Topic(
                title=topic_title,
                author=request.user,
                post_date=date,
                last_post=date
            )
            new_topic.save()
            
            tag_topic = TagTopic.objects.create(
                tag=tag,
                topic=new_topic,
                date_added=date
            )
            tag_topic.save()
            
            new_post = Post(
                text=post_text,
                author=request.user,
                post_date=date
            )
            new_post.save()
            
            topic_post = TopicPost.objects.create(
                topic=new_topic,
                post=new_post,
                date_added=date
            )
            
            # Set the response
            response['topic_id'] = new_topic.id
            response['topic_title'] = new_topic.title
            response['topic_author'] = new_topic.author.username
            response['topic_date'] = date
            
            # Everything was successful!
            response['success'] = True

    # Return results
    return JsonResponse(response)