Ejemplo n.º 1
0
def join_comptoir(request, cid):

    context = RequestContext(request)
    context['registerForm'] = RegisterForm()
    context['version'] = VERSION
    context['now'] = datetime.datetime.now()
    template_name = "chat/see_comptoir.html"

    comptoir = get_object_or_404(Comptoir, id=cid)
        
    if comptoir is None:
        return redirect("home")

    if cid not in request.session.keys():
        request.session[cid] = False
    
    if comptoir.public:
        request.session[cid] = True
    
    context["title"] = comptoir.title
    context["description"] = comptoir.description
    context["id"] = comptoir.id
    context['public'] = comptoir.public
    context["request"] = request
    context["title_is_ciphered"] = comptoir.title_is_ciphered

    count = Message.objects.filter(comptoir=comptoir).count()
    msgs = Message.objects.filter(comptoir=comptoir).order_by('date')[max(0, count - 150):]
    context["msgs"] = msgs 
    context["nb_msg"] = count
    context["nb_auth"] = len(list(set([msg.owner.username for msg in msgs])))

    if len(context["msgs"]) > 0:
        context["senti"] = context["msgs"][len(context["msgs"])-1].id
    else:
        context["senti"] = 0
    
    user = request.user
    if not user.is_anonymous() and user.is_authenticated():
        try:
            lv = user.chatuser.last_visits.get(comptoir=comptoir)
            lv.date = datetime.datetime.utcnow().replace(tzinfo=utc)
            lv.save()
        except ObjectDoesNotExist:
            lv = LastVisit()
            lv.comptoir = comptoir
            lv.save()
            user.chatuser.last_visits.add(lv)
            messages.info(request, "Hello, stranger.")
            
    for c in request.user.comptoirs:
        if c[0] == comptoir:
            new_c = (c[0], 0)
            request.user.comptoirs.remove(c)
            request.user.comptoirs.append(new_c)
            break

    return render(request, template_name, context)
Ejemplo n.º 2
0
def create_comptoir(request):
    global comptoir_created

    context = RequestContext(request)
    context['registerForm'] = RegisterForm()
    context['version'] = VERSION
    form = ComptoirForm(request.POST or None)

    if form.is_valid():
        data = form.cleaned_data

        if request.user.is_authenticated and str(request.user) != "AnonymousUser":
            user = request.user
        else:
            user = None

        if not data['public'] and (data['key_hash'] is None or data['key_hash'] == ""):
            form._errors['key_hash'] = ErrorList("This field is requested to create a private comptoir.")
            messages.warning(request, "Comptoir not created: no hash given.")
            return index(request)
        if data['public']:
            key = "ff" * 32
            sha = hashlib.sha3_512()
            sha.update(key)
            data['key_hash'] = sha.hexdigest()
        new_comptoir = Comptoir(owner=user,
                                title=data['title'],
                                description=data['description'],
                                public=not data['public'],
                                key_hash=data['key_hash'],
                                title_is_ciphered=True)

        new_comptoir.save()

        lv = LastVisit(comptoir=new_comptoir)
        lv.save()
        request.user.chatuser.last_visits.add(lv)
        request.user.comptoirs.append((new_comptoir, 0))

        messages.success(request,
                         "The comptoir has been created successfully. You can access your comptoirs by clicking on your nickname.")

        # Global variable set to True to distinguish between form with error
        # (that will be filled by index) and form treated that as to be cleaned up
        comptoir_created = True

        return HttpResponse("cid_" + str(new_comptoir.id))
        # return redirect("join_comptoir", new_comptoir.id)

    else:
        messages.warning(request, "Comptoir not created: some errors in the form.")

        return index(request)