def getAskableCurrentTags(request): if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) asked = objJson['asked'] user = User.objects.filter(username=objJson['login'])[0] profile = Profile.objects.filter(id_user=user)[0] listtags = ListTags.objects.filter(id_profile=profile) res = {} if len(listtags) > 0: for lt in listtags: tag = lt.id_tag category = tag.id_category theme = category.id_theme if (theme.theme in asked and category.category in asked[theme.theme] and tag.tag in asked[theme.theme][category.category]): continue if not theme.theme in res: res[theme.theme] = {} if not category.category in res[theme.theme]: res[theme.theme][category.category] = [] res[theme.theme][category.category].append(tag.tag) return myDumpJson(res) else: return HttpResponse(content="Not a good request", status=400)
def getMyFriends(request): """ Récupère les amis d'un utilisateur """ if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) user = User.objects.get(username=objJson['login']) profile = Profile.objects.get(id_user=user) res = [] friends = Friendship.objects.filter(profile1=profile) for friend in friends: p = Profile.objects.filter(pseudo=friend.profile2.pseudo) if p.exists(): p = p[0] res.append(ProfileDTO(p, p.pourcentage).toJson()) friends = Friendship.objects.filter(profile2=profile) for friend in friends: p = Profile.objects.filter(pseudo=friend.profile1.pseudo) if p.exists(): p = p[0] res.append(ProfileDTO(p, p.pourcentage).toJson()) return myDumpJson(res) else: return HttpResponse(content="Not a POST request", status=400)
def getHistoriqueCheckin(request, user): user = User.objects.filter(username=user)[0] checkins = Checkin.objects.filter(id_user=user) res = [] for c in checkins: hc = HistoriqueCheckinDTO(c) res.append(hc.toJson()) return myDumpJson(res)
def getPossibleChallengers(request): logger = logging.getLogger(__name__) if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) user = User.objects.filter(username=objJson['login'])[0] res = findValidCheckins(user, getLastCheckin(user)) return myDumpJson(res) else: return HttpResponse(content="Not a good request", status=400)
def getGameQuestions(request): logger = logging.getLogger(__name__) if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) user = User.objects.filter(username=objJson['login'])[0] prof = Profile.objects.filter(id_user=user)[0] profchall = Profile.objects.filter(pseudo=objJson['pseudo-challenger'])[0] game = Game.objects.filter(profile1=prof, profile2=profchall) if len(game) == 0: game = Game.objects.filter(profile1=profchall, profile2=prof) if len(game) == 0: game = Game.objects.create_game(profile1=prof, profile2=profchall, startDate=datetime.now()) else: game = game[0] questions = Question.objects.filter(id_game=game) tab = [] for question in questions: if question.prof_ask == prof: lt = ListTags.objects.filter(id_profile=profchall, id_tag=question.id_tag) q = QuestionDTO(question, len(lt) == 1, True) else : lt = ListTags.objects.filter(id_profile=prof, id_tag=question.id_tag) q = QuestionDTO(question, len(lt) == 1, False) tab.append(q.toJson()) res = {} res['questions'] = tab res['turn'] = game.turn1 if game.profile1 == prof else game.turn2 res['startDate'] = game.startDate.strftime('%d/%m/%Y %H:%M:%S') if len(questions) > 0 : res['lastDate'] = questions[len(questions) - 1].askDate.strftime('%d/%m/%Y %H:%M:%S') else: res['lastDate'] = ' - ' f1 = Friendship.objects.filter(profile1=prof, profile2=profchall) f2 = Friendship.objects.filter(profile1=profchall, profile2=prof) res['friend'] = False res['canBeFriends'] = False if f1.exists() or f2.exists(): res['friend'] = True else: num = 0 challNum = 0 for q in res['questions']: if q['answer']: if q['profile'] == profchall: challNum += 1 if q['profile'] == prof: num += 1 res['canBeFriends'] = num >= 3 and challNum >= 3 return myDumpJson(res, status=200) else: return HttpResponse(content="Not a good request", status=400)
def dispatchDataBase(request, username): """ Récupère toute la base de données """ if request.method == 'GET': user = User.objects.filter(username=username) profile = Profile.objects.filter(id_user=user[0]) res = getDataBase(profile[0]) return myDumpJson(res) else: return HttpResponse(content="Not a good request", status=400)
def getProfile(request): """ Récupère le profil d'un utilisateur """ if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) user = User.objects.filter(username=objJson['login']) prof = Profile.objects.filter(id_user=user)[0] p = ProfileDTO(prof, prof.pourcentage) return myDumpJson(p.toJson()) else: return HttpResponse(content="Not a POST request", status=400)
def getAllThemes(request): """ Récupère tous les thèmes de l'application """ if request.method == 'GET': allThemes = Theme.objects.all() res = [] for theme in allThemes: res.append(theme.theme) return myDumpJson(res) else: return HttpResponse(content="Not a good request", status=400)
def getTagsLetter(request, theme, category): """ Récupère tous les tags d'une catégorie d'un thème, en fonction d'une recherche """ if request.method == 'POST': letter = request.POST.get('letter') name = request.POST.get('login') user = User.objects.filter(username=name) them = Theme.objects.filter(theme=urllib.parse.unquote(theme)) cat = Category.objects.filter(id_theme=them, category=urllib.parse.unquote(category)) return myDumpJson(getTags(category=cat, profile=profile, letter=letter)) else: return HttpResponse(content="Not a good request", status=400)
def getAllTags(request, theme, category): """ Récupère tous les tags d'une catégorie d'un thème """ if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) name = objJson['login'] user = User.objects.filter(username=name) profile = Profile.objects.filter(id_user=user)[0] them = Theme.objects.filter(theme=urllib.parse.unquote(theme)) cat = Category.objects.filter(id_theme=them, category=urllib.parse.unquote(category)) return myDumpJson(getTags(category=cat, profile=profile)) else: return HttpResponse(content="Not a good request", status=400)
def getCategories(request, theme): """ Récupère toutes les catégories d'un thème """ if request.method == 'GET': them = Theme.objects.filter(theme=urllib.parse.unquote(theme)) allCategories = Category.objects.filter(id_theme=them) res = [] for cat in allCategories: c = CategoryDTO(cat) res.append(c.toJson()) return myDumpJson(res) else: return HttpResponse(content="Not a good request", status=400)
def getDemandesChallenges(request): logger = logging.getLogger(__name__) if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) user = User.objects.filter(username=objJson['login'])[0] profile2 = Profile.objects.filter(id_user=user)[0] games = Game.objects.filter(profile2=profile2, startDate=None) res = [] for g in games: profile1 = g.profile1 compatibility = defineCompatibility(profile2, profile1) if compatibility[0]: dc = ProfileDTO(profile1, compatibility[1]) res.append(dc.toJson()) return myDumpJson(res, status=200) else: return HttpResponse(content="Not a good request", status=400)
def getDemandesChallenges(request): logger = logging.getLogger(__name__) if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) user = User.objects.filter(username=objJson['login'])[0] profile2 = Profile.objects.filter(id_user=user)[0] games = Game.objects.filter(profile2=profile2, startDate=None) res = [] for g in games: profile1 = g.profile1 compatibility = defineCompatibility(profile2, profile1) if compatibility[0] : dc = ProfileDTO(profile1, compatibility[1]) res.append(dc.toJson()) return myDumpJson(res, status=200) else: return HttpResponse(content="Not a good request", status=400)
def getCurrentGames(request): logger = logging.getLogger(__name__) if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) user = User.objects.filter(username=objJson['login'])[0] prof = Profile.objects.filter(id_user=user)[0] games = Game.objects.filter(profile2=prof).exclude(startDate=None) res = [] for g in games: cg = CurrentGamesDTO(g.profile1, g.turn2) res.append(cg.toJson()) games = Game.objects.filter(profile1=prof).exclude(startDate=None) for g in games: cg = CurrentGamesDTO(g.profile2, g.turn1) res.append(cg.toJson()) return myDumpJson(res, status=200) else: return HttpResponse(content="Not a good request", status=400)
def logUser(request): """ Connecte un utilisateur """ if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) login = objJson['login'] password = objJson['password'] user = authenticate(username=login, password=password) if user is not None: if user.is_active: logger.debug(str(user) + " authenticated !") prof = Profile.objects.filter(id_user=user)[0] p = ProfileDTO(prof, prof.pourcentage) return myDumpJson(p.toJson()) else: logger.error(str(user) + " is not active !") return HttpResponse(status=401) else: logger.error("Can't authenticate " + str(user) + "!") return HttpResponse(status=400) else: return HttpResponse(content="Not a POST request", status=400)
def getGameQuestions(request): logger = logging.getLogger(__name__) if request.method == 'POST': objJson = json.loads(request.body.decode('utf-8')) user = User.objects.filter(username=objJson['login'])[0] prof = Profile.objects.filter(id_user=user)[0] profchall = Profile.objects.filter( pseudo=objJson['pseudo-challenger'])[0] game = Game.objects.filter(profile1=prof, profile2=profchall) if len(game) == 0: game = Game.objects.filter(profile1=profchall, profile2=prof) if len(game) == 0: game = Game.objects.create_game(profile1=prof, profile2=profchall, startDate=datetime.now()) else: game = game[0] questions = Question.objects.filter(id_game=game) tab = [] for question in questions: if question.prof_ask == prof: lt = ListTags.objects.filter(id_profile=profchall, id_tag=question.id_tag) q = QuestionDTO(question, len(lt) == 1, True) else: lt = ListTags.objects.filter(id_profile=prof, id_tag=question.id_tag) q = QuestionDTO(question, len(lt) == 1, False) tab.append(q.toJson()) res = {} res['questions'] = tab res['turn'] = game.turn1 if game.profile1 == prof else game.turn2 res['startDate'] = game.startDate.strftime('%d/%m/%Y %H:%M:%S') if len(questions) > 0: res['lastDate'] = questions[len(questions) - 1].askDate.strftime( '%d/%m/%Y %H:%M:%S') else: res['lastDate'] = ' - ' f1 = Friendship.objects.filter(profile1=prof, profile2=profchall) f2 = Friendship.objects.filter(profile1=profchall, profile2=prof) res['friend'] = False res['canBeFriends'] = False if f1.exists() or f2.exists(): res['friend'] = True else: num = 0 challNum = 0 for q in res['questions']: if q['answer']: if q['profile'] == profchall: challNum += 1 if q['profile'] == prof: num += 1 res['canBeFriends'] = num >= 3 and challNum >= 3 return myDumpJson(res, status=200) else: return HttpResponse(content="Not a good request", status=400)
def test(request): didi = Profile.objects.filter(pseudo="Le didi")[0] zozo = Profile.objects.filter(pseudo="Zozo")[0] game = Game.objects.filter(profile1=zozo, profile2=didi)[0] questions = Question.objects.filter(id_game=game).delete() return myDumpJson(Question.objects.filter(id_game=game))