Esempio n. 1
0
def select(request, pk):
    user = tools.getCurrentUser(request)
    res = {}
    if user:
        file = AI.objects.filter(id=pk)
        if len(file) == 0:
            res['message'] = 'File does not exist.'
            res['result'] = False
        elif not file[0].team:
            res['message'] = 'The file does not belong to a team.'
            res['result'] = False
        elif file[0].team != user.team:
            res['message'] = 'You can only select file of your own team.'
            res['result'] = False
        else:
            team = user.team
            team_files = AI.objects.filter(team=team)
            for f in team_files:
                f.selected = False
                f.save()
            file = file[0]
            file.selected = True
            file.save()
            res['message'] = 'success'
            res['result'] = True
    else:
        res['message'] = 'Please login first.'
        res['result'] = False
    return HttpResponse(json.dumps(res), content_type='application/json')
Esempio n. 2
0
def update(request):
    user = tools.getCurrentUser(request)
    if (user == None):
        return HttpResponse(json.dumps({'message': 'Please log in.'}),
                            content_type='application/json')

    changes = {}
    return HttpResponse(json.dumps(changes), content_type='application/json')
Esempio n. 3
0
def manage(request):
    user = tools.getCurrentUser(request)
    result = {}
    if (user and user.isCaptain):
        team = user.team
        if (request.POST.get('name')):
            if len(request.POST['name']) > 30:
                return HttpResponse(json.dumps({
                    'message': 'The name is too long',
                    'result': False
                }),
                                    content_type='application/json')
            team.name = request.POST['name']
        if (request.POST.get('introduction')):
            if len(request.POST['introduction']) > 250:
                return HttpResponse(json.dumps({
                    'message': 'The introduction is too long',
                    'result': False
                }),
                                    content_type='application/json')
            team.introduction = request.POST['introduction']
        team.save()
        result['message'] = 'Update successfully'

        if (request.POST.get('accept')):
            result['message'] = 'Accept successfully'
            user = User.objects.get(id=request.POST['accept'])
            if (user and user.team == team):
                user.isMember = True
                user.save()

        if (request.POST.get('dismiss')):
            result['message'] = 'Dismiss successfully'
            user = User.objects.get(id=request.POST['dismiss'])
            if (user and user.team == team):
                user.isMember = False
                user.save()
        result['result'] = True

        if (request.POST.get('disband')):
            result['result'], result['message'] = tools.disbandTeam(team)

    elif (user and user.isMember == False and request.POST.get('name')):
        result['result'], result['message'] = tools.createTeam(
            user.id, request.POST.get('name'),
            request.POST.get('introduction'))
    else:
        result['message'] = 'Please log in.'
        result['result'] = False

    return HttpResponse(json.dumps(result), content_type='application/json')
Esempio n. 4
0
def detail(request, teamID=-1):
    user = tools.getCurrentUser(request)
    if (teamID == -1):
        if (user == None or user.isMember == False):
            return HttpResponse(json.dumps({}),
                                content_type='application/json')
        else:
            team = user.team
    else:
        try:
            team = Team.objects.get(id=teamID)
        except:
            team = None

    if (team == None):
        return HttpResponse(json.dumps({
            'message': 'Team does\'t exist.',
            'result': False
        }),
                            content_type='application/json')

    members = User.objects.filter(team=team, isMember=True)
    result = {
        'id': team.id,
        'name': team.name,
        'introduction': team.introduction
    }
    memberList = []
    for member in members:
        info = json.loads(member.information)
        info = {'id': info['id'], 'username': info['username']}
        if member.isCaptain:
            captain = info
        else:
            memberList.append(info)
    result['members'] = memberList
    result['captain'] = captain

    if (user and captain and user.id == captain['id']):
        candidates = User.objects.filter(team=team, isMember=False)
        result['candidates'] = [{
            'id':
            user.id,
            'username':
            json.loads(user.information)['username']
        } for user in candidates]
    result['result'] = True

    return HttpResponse(json.dumps(result), content_type='application/json')
Esempio n. 5
0
def apply(request, teamID):
    user = tools.getCurrentUser(request)
    team = Team.objects.get(id=teamID)
    result = {}
    if (team == None):
        result['message'] = 'Team does\'t exist.'
        result['result'] = False
    if (user == None):
        result['message'] = 'Please log in.'
        result['result'] = False
    elif (user.isMember):
        result['message'] = 'Already in a team now.'
        result['result'] = False
    else:
        user.team = team
        result['message'] = 'Apply successfully.'
        result['result'] = True
        user.save()
    return HttpResponse(json.dumps(result), content_type='application/json')
Esempio n. 6
0
def upload(request):
    user = tools.getCurrentUser(request)
    res = {}
    if user != None and request.method == 'POST' and request.POST.get(
            'filename') and type(
                request.POST.get('description')) != type(None):
        #limit the size and type of file to be uploaded
        myfile = request.FILES['file']
        if myfile:
            if myfile.size >= 1048576:
                res['error'] = True
                res['message'] = 'Size of file should be less than 1MB.'
            elif myfile.name.endswith(SUFFIX) == False:
                res['error'] = True
                res['message'] = 'Only {0} file will be accepted.'.format(
                    SUFFIX)
            else:
                fileupload = AI()
                fileupload.filename = request.POST['filename']
                fileupload.user = user
                fileupload.team = user.team
                fileupload.description = request.POST['description']
                fileupload.file = myfile
                fileupload.save()
                print('Code uploaded. author={0}, name={1}'.format(
                    user.id, fileupload.filename))

                res['error'] = False
                res['message'] = 'You have successfully uploaded the code.'
        else:
            res['error'] = True
            res['message'] = 'File does not exist.'
    elif user == None:
        res['error'] = True
        res['message'] = 'Please log in.'
    else:
        res['error'] = True
        res['message'] = 'Error'

    return HttpResponse(json.dumps(res), content_type='application/json')
Esempio n. 7
0
def filedownload(request, pk):
    def file_iterator(file_name, chunk_size=2048):
        with open(file_name, 'rb') as f:
            while True:
                c = f.read(chunk_size)
                if c:
                    yield c
                else:
                    break

    user = tools.getCurrentUser(request)
    if user:
        file = AI.objects.filter(id=pk)
        if len(file) > 0 and ((not file[0].team and file[0].user == user) or
                              (file[0].team and file[0].team == user.team)):
            response = StreamingHttpResponse(
                file_iterator(get_file_path(file[0])))
            response['Content-Type'] = 'application/octet-stream'
            response[
                'Content-Disposition'] = 'attachment;filename="{0}"'.format(
                    file[0].origin_name)
    return response
Esempio n. 8
0
def delete(request, pk):
    user = tools.getCurrentUser(request)
    res = {}
    if user:
        file = AI.objects.filter(id=pk)
        if len(file) == 0:
            res['message'] = 'File does not exist.'
            res['result'] = False
        elif file[0].user != user:
            res['message'] = 'You can only delete your own file.'
            res['result'] = False
        else:
            file = file[0]
            if os.path.exists(get_file_path(file)):
                os.remove(get_file_path(file))
            file.delete()
            res['message'] = 'success'
            res['result'] = True
    else:
        res['message'] = 'Please login first.'
        res['result'] = False
    return HttpResponse(json.dumps(res), content_type='application/json')
Esempio n. 9
0
def list(request):
    user = tools.getCurrentUser(request)
    res = {}
    if user:
        ai_list = AI.objects.filter(user=user)
        data = []
        for ai in ai_list:
            #for attr in dir(ai.user):
            #    print(attr+":"+str(getattr(ai.user,attr)))
            data.append({
                'username': json.loads(ai.user.information)["username"],
                'filename': ai.filename,
                'description': ai.description,
                'upload time': ai.timestamp,
                'ai id': ai.id
            })
        res['data'] = data
        res['result'] = True
    else:
        res['data'] = []
        res['result'] = False
    return HttpResponse(json.dumps(res, cls=DateEncoder),
                        content_type='application/json')
Esempio n. 10
0
def quit(request):
    user = tools.getCurrentUser(request)
    result = {}
    if (user == None):
        result['message'] = 'Please log in.'
        result['result'] = False
    elif (user.team == None):
        result['message'] = 'Not in a team now.'
        result['result'] = False
    elif (user.isCaptain == True):
        result['message'] = 'Captain can\'t quit.'
        result['result'] = False
    else:
        user.team = None
        if (user.isMember == True):
            user.isMember = False
            result['message'] = 'Quit successfully.'
            result['result'] = True
        else:
            result['message'] = 'Cancel successfully.'
            result['result'] = True
        user.save()
    return HttpResponse(json.dumps(result), content_type='application/json')