Ejemplo n.º 1
0
def modify(request):
    if request.method =='POST':
        token = request.POST.get('token')
        name = request.POST.get('name')
        user = userToken.getUser(token)
        if len(Route.objects.filter(name=name)):
            route = Route.objects.get(name=name)
            if route.createuser == user:
                data = request.POST.get('data')
                type = request.POST.get('type')
                route.data = data if data else route.data
                route.type = type if type else route.type
                route.save()
                return HttpResponse(json.dumps({
                    'action':'modifyRoute',
                    'result':'succeed',
                }))
            else:
                return HttpResponse(json.dumps({
                    'action':'modifyRoute',
                    'result':'error',
                    'errorResult':'userLimit',
                }))
        else:
            return HttpResponse(json.dumps({
                'action':'modifyRoute',
                'result':'error',
                'errorResult':'nameNotExist',
            }))
Ejemplo n.º 2
0
def savepassage(request):
    if request.method == 'POST':
        token = request.POST.get('token')
        user = userToken.getUser(token)
        if user:
            newPassage = Passage()
            newPassage.m_user = user
            newPassage.title = request.POST.get('title')
            newPassage.body = request.POST.get('body')
            newPassage.save()
            return HttpResponse(
                json.dumps({
                    'action': 'savepassage',
                    'result': 'succeed',
                    'id': newPassage.id,
                }))
        else:
            return HttpResponse(
                json.dumps({
                    'action': 'savepassage',
                    'result': 'error',
                    'errorResult': 'TokenDoNotMatch',
                }))
    else:
        return HttpResponse('''Please use POST to visit. ''')
Ejemplo n.º 3
0
def create(request):
    if request.method =='POST':
        token = request.POST.get('token')
        if userToken.isCar(token):
            user = userToken.getUser(token)
            name = request.POST.get('name')
            type = request.POST.get('type')
            data = request.POST.get('data')
            if len(Route.objects.filter(name=name)):
                return HttpResponse(json.dumps({
                    'action':'createRoute',
                    'result':'error',
                    'errorResult':'nameExist',
                }))
            newRoute = Route()
            newRoute.name = name
            newRoute.createuser = user
            newRoute.type = type
            newRoute.data = data
            newRoute.save()
            addtoDicts(RouteTypeDicts, type, name)
            RouteNames.append(name)
            return HttpResponse(json.dumps({
                'action':'createRoute',
                'result':'succeed',
                'savename':'name',
            }))
        else:
            return HttpResponse(json.dumps({
                'action':'createRoute',
                'result':'error',
                'errorResult':'userLimit',
            }))
Ejemplo n.º 4
0
def delete(request):
    if request.method =='POST':
        token = request.POST.get('token')
        name = request.POST.get('name')
        user = userToken.getUser(token)
        if len(Route.objects.filter(name=name)):
            route = Route.objects.get(name=name)
            if route.createuser == user:
                route.delete()
                return HttpResponse(json.dumps({
                    'action':'deleteRoute',
                    'result':'succeed',
                }))
            else:
                return HttpResponse(json.dumps({
                    'action':'deleteRoute',
                    'result':'error',
                    'errorResult':'userLimit',
                }))
        else:
            return HttpResponse(json.dumps({
                'action':'deleteRoute',
                'result':'error',
                'errorResult':'nameNotExist',
            }))
Ejemplo n.º 5
0
def send(request):
    if request.method == 'POST':
        token = request.POST.get('token')
        user = userToken.getUser(token)
        if user:
            data = request.POST.get('data')
            action = request.POST.get('action')
            to = request.POST.get('toname')
            if action == 'send':
                tmpMessages.addmessage(to, user.username, data)
                return HttpResponse(
                    json.dumps({
                        'action': 'sendmassage',
                        'result': 'succeed',
                    }))
            elif action == 'get':
                getfrom = request.POST.get('froms')
                r = tmpMessages.getmeaages(user.username, getfrom)
                return HttpResponse(
                    json.dumps({
                        'action': 'getmessage',
                        'result': 'succeed',
                        'data': r,
                    }))
            elif action == 'clean':
                tmpMessages.cleanmessages(user.username)
                return HttpResponse(
                    json.dumps({
                        'action': 'cleanmessage',
                        'result': 'succeed',
                    }))
            elif action == 'list':
                r = tmpMessages.listsender(user.username)
                return HttpResponse(
                    json.dumps({
                        'action': 'cleanmessage',
                        'result': 'succeed',
                        'data': r,
                    }))
            else:
                return HttpResponse(
                    json.dumps({
                        'action': 'message',
                        'result': 'error',
                        'errorResult': 'actionNotExist',
                    }))
        else:
            return HttpResponse(
                json.dumps({
                    'action': 'message',
                    'result': 'error',
                    'errorResult': 'TokenDoNotMatch',
                }))
    else:
        return HttpResponse('''Please use POST to visit. ''')
Ejemplo n.º 6
0
def changepassage(request):
    if request.method == 'POST':
        token = request.POST.get('token')
        user = userToken.getUser(token)
        if user:
            passageid = request.POST.get('id')
            passage = Passage.objects.filter(id=passageid)
            if passage:  # passage exists
                passage = passage[0]
                if passage.m_user == user:
                    passage.title = request.POST.get('title')
                    passage.body = request.POST.get('body')
                    passage.save()
                    return HttpResponse(
                        json.dumps({
                            'action': 'changepassage',
                            'result': 'succeed',
                            'id': passageid,
                        }))
                else:  # passage is not this user post
                    return HttpResponse(
                        json.dumps({
                            'action': 'changepassage',
                            'result': 'error',
                            'errorResult': 'PermissionDenied',
                            'id': passageid,
                        }))
            else:  # passage doesn't exists
                return HttpResponse(
                    json.dumps({
                        'action': 'changepassage',
                        'result': 'error',
                        'errorResult': 'PassageDoNotExists',
                        'id': passageid,
                    }))
        else:  # user don't login
            return HttpResponse(
                json.dumps({
                    'action': 'savepassage',
                    'result': 'error',
                    'errorResult': 'TokenDoNotMatch',
                }))
    else:
        return HttpResponse('''Please use POST to visit. ''')
Ejemplo n.º 7
0
def modify(request):
    if request.method == 'POST':
        if request.POST.get('token'):
            user = userToken.getUser(request.POST.get('token'))
            if not user:
                return HttpResponse(
                    json.dumps({
                        'action': 'modify',
                        'result': 'error',
                        'errorResult': 'userDoNotExist',
                    }))
            modifyData = request.POST.get('modifydata')
            try:
                modifyData = json.loads(modifyData)
            except Exception, e:
                return HttpResponse(
                    json.dumps({
                        'action': 'modify',
                        'result': 'error',
                        'errorResult': e,
                    }))
            if modifyData.get('college'):
                user.college = modifyData.get('college')
            if modifyData.get('studentid'):
                user.studentid = modifyData.get('studentid')
            if modifyData.get('truename'):
                user.truename = modifyData.get('truename')
            user.save()
            return HttpResponse(
                json.dumps({
                    'action': 'modify',
                    'result': 'succeed',
                }))
        else:
            return HttpResponse(
                json.dumps({
                    'action': 'modify',
                    'result': 'error',
                    'errorResult': 'tokenDoNotExist',
                }))
Ejemplo n.º 8
0
def modify(request):
    if request.method=='POST':
        if request.POST.get('token'):
            user = userToken.getUser(request.POST.get('token'))
            if not user:
                return HttpResponse(json.dumps({
                    'action':'modify',
                    'result':'error',
                    'errorResult':'userDoNotExist',
                }))                
            modifyData = request.POST.get('modifydata')
            try:
                if modifyData:
                    modifyData = json.loads(modifyData)
                else:
                    return HttpResponse(json.dumps({
                        'action':'modify',
                        'result':'error',
                        'errorResult':'modifyDataDoNotExists',
                    }))                     
            except Exception as e:
                return HttpResponse(json.dumps({
                    'action':'modify',
                    'result':'error',
                    'errorResult':e,
                }))     
            for dataname in modifyData:
                if user.__dict__.get(dataname):
                    user.__dict__[dataname] = modifyData[dataname]                                                               
            user.save()
            return HttpResponse(json.dumps({
                'action':'modify',
                'result':'succeed',
            }))
        else:
            return HttpResponse(json.dumps({
                'action':'modify',
                'result':'error',
                'errorResult':'tokenDoNotExist',
            }))
Ejemplo n.º 9
0
def getpositions(request):
    if request.method=='POST':
        token = request.POST.get('token')
        getuser = request.POST.get('getuser')
        user = userToken.getUser(token)
        if not user:
            return HttpResponse(json.dumps({
                'action':'getposition',
                'result':'error',
                'errorResult':'UserDoNotExist',
            })) 
        if not getuser:
            getuser = user.username
        if user.username == getuser:# User it self
            positions = []
            tag = request.POST.get('tag')
            if tag:
                posData = user.SavePosition.filter(tag=tag)
            else:
                posData = user.SavePosition.all()            
            for pos in posData:
                positions.append({
                        'latitude':pos.latitude,
                        'longitude':pos.longitude,
                        'tag':pos.tag,
                        'time':str(pos.time),
                    })
            return HttpResponse(json.dumps({
                'action':'getposition',
                'result':'succeed',
                'positions':positions,
            }))   
        else:
            return HttpResponse(json.dumps({
                'action':'getposition',
                'result':'error',
                'errorResult':'CanNotGetOthersData',
            }))                  
Ejemplo n.º 10
0
def modifypassword(request):
    if request.method == 'POST':
        if request.POST.get('token'):
            token = request.POST.get('token')
            user = userToken.getUser(request.POST.get('token'))
            if not user:
                return HttpResponse(
                    json.dumps({
                        'action': 'modifypassword',
                        'result': 'error',
                        'errorResult': 'userTokenError',
                    }))
            oldpassword = request.POST.get('password')
            if user.pwd != encodePwd.encode(str(oldpassword)):
                return HttpResponse(
                    json.dumps({
                        'action': 'modifypassword',
                        'result': 'error',
                        'errorResult': 'passwordError',
                    }))
            newpassword = request.POST.get('newpassword')
            user.pwd = encodePwd.encode(newpassword)
            user.save()
            userToken.dropToken(token)
            return HttpResponse(
                json.dumps({
                    'action': 'modifypassword',
                    'username': user.username,
                    'result': 'succeed',
                }))
        else:
            return HttpResponse(
                json.dumps({
                    'action': 'modifypassword',
                    'result': 'error',
                    'errorResult': 'tokenDoNotExist',
                }))
Ejemplo n.º 11
0
def saveposition(request):
    if request.method=='POST':
        token = request.POST.get('token')
        user = userToken.getUser(token)
        if not user:
            return HttpResponse(json.dumps({
                'action':'saveposition',
                'result':'error',
                'errorResult':'UserDoNotExist',
            }))      
        else:
            latitude = request.POST.get('latitude')
            longitude = request.POST.get('longitude')
            tag = request.POST.get('tag')            
            position = SavePosition()
            position.user = user
            position.latitude = latitude
            position.longitude = longitude
            position.tag = tag
            position.save()
            return HttpResponse(json.dumps({
                'action':'saveposition',
                'result':'succeed',
            }))              
Ejemplo n.º 12
0
def getcallcarpeoples(request):
    if request.method=='POST':
        token = request.POST.get('token')
        user = userToken.getUser(token)
        route = request.POST.get('route')
        if route not in RouteNames:
            return HttpResponse(json.dumps({
                'action':'send',
                'result':'error',
                'position':'routeNotExist',
            }))
        if user:
            positions = peoplePositions.getCallCarPositons(route) 
            return HttpResponse(json.dumps({
                'action':'getcallcarpeoples',
                'result':'succeed',
                'positions':positions,
            }))            
        else:
            return HttpResponse(json.dumps({
                'action':'getcallcarpeoples',
                'result':'error',
                'errorResult':'UserDoNotExist',
            }))       
Ejemplo n.º 13
0
def getdata(request):
    if request.method == 'POST':
        getdatausername = request.POST.get('getdatausername')
        token = request.POST.get('token')
        user = userToken.getUser(token)
        if user:
            if user.username == getdatausername:
                return HttpResponse(
                    json.dumps({
                        'action': 'getuserdata',
                        'result': 'succeed',
                        'data': {
                            'college': user.college,
                            'studentid': user.studentid,
                            'truename': user.truename,
                            'iscar': user.iscar,
                        },
                    }))
        # not user himself
        user = m_User.objects.filter(username=getdatausername)
        if user:
            user = user[0]
            return HttpResponse(
                json.dumps({
                    'action': 'getuserdata',
                    'result': 'succeed',
                    'data': {
                        'college': user.college,
                        'truename': user.truename,
                        'iscar': user.iscar,
                    },
                }))
        else:
            return HttpResponse(
                json.dumps({
                    'action': 'getuserdata',
                    'result': 'error',
                    'errorResult': 'usernameDoNotExist',
                }))


#
# def index(request):
#     projects=Project.objects.order_by('startdate').reverse()[:10]
#     tutors=Tutor.objects.order_by('?').reverse()[:10]
#     context={
#         'projects':projects,
#         'tutors':tutors,
#     }
#     a={
#         'edde':'dfbgd',
#         'vdv':['dfhr','gedgfs'],
#     }
#     json.dump(a)
#     return render(request,'main/index.html',context)

# def signin(request,before='index'):
#     if request.method=='POST':
#         sf = SigninForm(request.POST)
#         if sf.is_valid():
#             username = sf.cleaned_data['username']
#             password = sf.cleaned_data['password']
#             user = authenticate(username=username, password=password)
#             if user is not None and user.is_active:
#                 login(request, user)
#                 return render(request,'main/signin.html',{'result':'succeed','before':before})
#         #form doesn't true
#             else:
#                 return render(request, 'main/signin.html', {'result':'error','form':sf})
#         else:
#             return render(request,'main/signin.html',{'result':'error'})
#             # Return an error message.
#     else:
#         print 'signin2'
#         sf = SigninForm()
#         return render(request,'main/signin.html',{'form':sf,'before':before})

# def signout(request,before='index'):
#     logout(request)
#     return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
# def signup2(request):
#     if request.method=='POST':
#         suf = SigninForm(request.POST)
#         print request.POST
#         username = request.POST['username']
#         password = request.POST['password']
#         if not len(User.objects.filter(username=username)):
#             if request.POST['password']==request.POST['passwordagain']:
#                 user=User()
#                 user.username=username
#                 user.set_password(password)
#                 user.save()
#                 student=Student()
#                 student.username=user
#                 student.sid=request.POST['sid']
#                 student.truename=request.POST['truename']
#                 student.birthday=timezone.now()
#                 student.save()
#                 user = authenticate(username=username, password=password)
#                 if user is not None and user.is_active:
#                     login(request, user)
#                     return render(request,'main/signin.html',{'result':'succeed'})
#                 else:
#                     pass #something wrong
#             else:
#                  return render(request,'main/signup2.html',{'form':suf,'error':'passwordagain'})
#         else:
#             return render(request,'main/signup2.html',{'form':suf,'error':'usernamerepeat'})
#     else:
#         suf=SignupForm()
#         return render(request,'main/signup2.html',{'form':suf})

# def signup(request):
#     if request.method=='POST':
#         suf=SignupForm()
#         if suf.is_valid():
#             pass
#         else:
#             return render(request,'main/signup.html',{'error':'1','form':suf})
#     else:
#         suf=SignupForm()
#         return render(request,'main/signup.html',{'form':suf})

# def modifyaccount(request):
#     if request.user.is_authenticated():
#         if request.method=='POST':
#             f=ModifyAccountForm(request.POST,instance=Student.objects.get(username=request.user))
#             if f.is_valid():
#                 f.save()
#                 return render(request,'main/modifyaccount.html',{'success':'1','form':f})
#             else:
#                 return render(request,'main/modifyaccount.html',{'error':'1','form':f})
#         else:
#             student=Student.objects.get(username=request.user)
#             projects=student.project_set.all()
#             f=ModifyAccountForm(instance=Student.objects.get(username=request.user))
#             return render(request,'main/modifyaccount.html',{'projects':projects,'form':f})
#     else:
#         return render(request,'main/signup2.html',{'form':suf})

# def showtutors(request,page):
#     pass
# def showtutor(request,id):
#     try:
#         student=Tutor.objects.get(id=id)
#     except:
#         return render(request,'main/tutor.html',{'error':'none'})
#     f=ShowTutorForm(instance=student)
#     return render(request,'main/tutor.html',{'form':f})

# def showprojects(request,page):
#     page=int(page)
#     projects=Project.objects.order_by('startdate').reverse()[1*page-1:30*page-1]
#     return render(request,'main/projects.html',{'page':page,'projects':projects})
# def showproject(request,id):
#     p=Project.objects.get(id=id)
#     students=p.students.all()
#     tutors=p.tutors.all()
#     allstudents=p.students.all()
#     for s in allstudents:#应该可以优化一下
#         if request.user==s.username:
#             return render(request,'main/project.html',{'project':p,'students':students,'tutors':tutors,'edit':'1'})
#     return render(request,'main/project.html',{'project':p,'students':students,'tutors':tutors})

# def createproject(request):
#     if request.user.is_authenticated():
#         if request.method=='POST':
#             f=CreateProjectForm(request.POST)
#             if f.is_valid():
#                 stu=Student.objects.get(username=request.user)
#                 project=Project()
#                 project.name=f.cleaned_data['name']
#                 allprojects=stu.project_set.all()
#                 for p in allprojects:
#                     if p.name==project.name:
#                         return render(request,'main/createproject.html',{'error':'repeat','form':f})
#                 project.status=f.cleaned_data['status']
#                 project.introduction=f.cleaned_data['introduction']
#                 project.startdate=timezone.now()
#                 project.save()
#                 project.students.add(stu)
#                 project.save()
#                 return showproject(request,project.id)      #why this doesn't work!!!!

#             else:
#                 return render(request,'main/createproject.html',{'error':'1','form':f})
#         else:
#             f=CreateProjectForm()
#             return render(request,'main/createproject.html',{'form':f})
#     else:
#         return signin(request)

# def showstudent(request,id):
#     try:
#         student=Student.objects.get(id=id)
#     except:
#         return render(request,'main/student.html',{'error':'none'})
#     f=ShowStudentForm(instance=student)
#     return render(request,'main/student.html',{'form':f})

# def invitetutor(request):
#     pass
# def invitestudent(request):
#     pass