Ejemplo n.º 1
0
def CreateGallery():
    g = Gallery(
        createTime=datetime.now(),
        token=GetNewId(),
    )
    g.save(force_insert=True)
    return g
Ejemplo n.º 2
0
def StoreImage(image, field, sizeLimit=6 * 2**20):
    imageFileExt = os.path.splitext(image.name)[1]
    if imageFileExt not in [".jpg", ".jpeg", ".gif", ".png"]:
        raise TsExc("image_bad_format")
    if image.size > sizeLimit:
        raise TsExc("image_big_size")
    filePath = GetNewId() + imageFileExt
    field.save(filePath, image)
    return filePath
Ejemplo n.º 3
0
def BuyOfferAddView(request):
    params = request.REQUEST
    act = params.get("act", "")
    owner = GetCurrentUser(request)
    if act == "add":
        try:
            CheckPost(request)
            CheckAuth(request)

            fr, to, title, costFrom, costTo = ExtractBuyFields(params)

            gallery = Gallery.objects.get(token=params.get("gallery", ""))
            buy = BuyOffer(
                title=title,
                ititle=title.lower(),
                content=params.get("content", ""),
                costFrom=costFrom,
                costTo=costTo,
                guarant=params.get("guarant", False),
                fr=fr,
                frCity=params.get("frCity", ""),
                ifrCity=params.get("frCity", "").lower(),
                to=to,
                toCity=params.get("toCity", ""),
                itoCity=params.get("toCity", "").lower(),
                gallery=gallery,
                owner=owner,
                createTime=datetime.now(),
            )
            buy.save()
            VerifyPhotos(params.get("token", ""))
            # backref = params.get("backref", "/offer/buy/list#buy{}".format(buy.id))
            if SaleExtractRecommend(buy, owner, limit=1):
                backref = "/offer/buy/recommend/{}?first_time=true".format(
                    buy.id)
            else:
                backref = "/offer/buy/list#buy{}".format(buy.id)
            return redirect(backref)
        except BuyEditErr as e:
            raise RedirectExc("/offer/buy/add?err={}".format(e.status))
    else:
        gallery = CreateGallery()
        token = GetNewId()
        return RenderToResponse(
            "offer/buy/add.html", request, {
                "url": "/offer/buy/add/",
                "gallery": gallery,
                "token": token,
                "countries": GetCountries(),
                "err": GetBuyEditMsg(params.get("err", ""))
            })
Ejemplo n.º 4
0
def BuyEditView(request, id):
    params = request.REQUEST
    act = params.get("act", "")
    buy = BuyOffer.objects.get(id=id)
    user = GetCurrentUser(request)
    if buy.owner != user:
        return redirect("/")
    if act == "edit":
        try:
            CheckPost(request)
            fr, to, title, costFrom, costTo = ExtractBuyFields(params)
            buy.title = title
            buy.content = params.get("content", "")
            buy.costFrom = costFrom
            buy.costTo = costTo
            buy.fr = fr
            buy.frCity = params.get("frCity", "")
            buy.ifrCity = params.get("frCity", "").lower()
            buy.to = to
            buy.toCity = params.get("toCity", "")
            buy.itoCity = params.get("toCity", "").lower()
            buy.guarant = params.get("guarant", False)
            buy.save()
            VerifyPhotos(params.get("token", ""))

            backref = params.get("backref",
                                 "/offer/buy/list/#buy{}".format(buy.id))
            return redirect(backref)
        except BuyEditErr as e:
            raise RedirectExc("/offer/buy/edit/{}?err={}".format(
                buy.id, e.status))
    elif act == "makeHead":
        pic = Photo.objects.get(id=params.get("picId"))
        pic.gallery.head = pic
        pic.gallery.save()
        backref = params.get("backref", "/offer/buy/edit/{}".format(buy.id))
        return redirect(backref)
    elif act == "erasePic":
        pic = Photo.objects.get(id=params.get("picId"))
        buy.gallery.er(pic)
        backref = params.get("backref", "/offer/buy/edit/{}".format(buy.id))
        return redirect(backref)
    return RenderToResponse(
        "offer/buy/edit.html", request, {
            "url": "/offer/buy/edit/{}/".format(buy.id),
            "buy": buy,
            "err": GetBuyEditMsg(params.get("err", "")),
            "token": GetNewId(),
            "countries": GetCountries(),
        })
Ejemplo n.º 5
0
def CreateGalleryPhoto(gallery, token=None):
    if not token:
        token = GetNewId()
    if gallery.photos.filter(verified=True).count() \
        + gallery.photos.filter(verified=False, token=token).count() >= 10:
        raise TsExc("image_limit_exceeded")
    photo = Photo(
        gallery=gallery,
        token=token,
        verified=False,
    )
    photo.save(force_insert=True)
    if not gallery.head:
        gallery.head = photo
        gallery.save()
    return photo
Ejemplo n.º 6
0
def UserRecoverView(request):
    params = request.REQUEST
    msg = ""
    msgErr = ""
    if request.method == "POST":
        email = params.get("email", "")
        user = User.objects.filter(username=email).all()
        if not user:
            msgErr = "user_does_not_exist"
        else:
            msg = "recover_ok"
            user = user[0]
            newPassword = GetNewId()[0:8]
            user.set_password(newPassword)
            user.save()
            SendRecoverMail(user, newPassword)
    return RenderToResponse("user/recover.html", request, {
        "msg": GetUserRecoverMsg(msg),
        "msgErr": GetUserRecoverMsg(msgErr),
    })
Ejemplo n.º 7
0
#!/usr/bin/env python
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tripnsale.settings")
from gallery.models import *
from util.utils import GetNewId
import django

if __name__ == "__main__":
    django.setup()
    galleries = Gallery.objects.all()
    for g in galleries:
        if not g.token:
            g.token = GetNewId()
            g.save()
Ejemplo n.º 8
0
def AuthView(request):
    params = request.REQUEST
    act = params.get("act", "")
    if act == "login":
        CheckPost(request)
        djUser = authenticate(
            username=params.get("email", "").lower(),
            password=params.get("password", ""),
        )
        user = GetUserByDjUser(djUser)
        if user:
            if not user.activated:
                raise RedirectExc("/user/auth?msgLogin=user_is_not_activated")
            user.remoteAddr = request.META["REMOTE_ADDR"]
            user.save()
            login(request, djUser)
        else:
            raise RedirectExc("/user/auth/?msgLogin=wrong_login_or_password")
        backref = params.get("next", "/")
        if not backref:
            backref = "/"
        return redirect(backref)
    elif act == "reg":
        try:
            CheckPost(request)
            if not params.get("firstName", "").strip() or not params.get("lastName", "").strip():
                raise RequiredFieldsMissingErr

            if params["password"] != params["password2"]:
                raise PasswordsAreNotEqualErr
            if not re.compile("^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+$").match(params["email"]):
                raise EmailIsInvalidErr
            if not (3 <= len(params["password"]) <= 20):
                raise BadPasswordLengthErr
            if not params.get("offert", 0):
                raise OfferNotAgreedErr
            country = Country.objects.get(name=params.get("country", 0))

            if User.objects.filter(username=params["email"]).count():
                raise DuplicateEmailErr

            user = User.objects.create_user(
                params.get("email", "").lower(),
                params.get("email", "").lower(),
                params.get("password", ""),
                first_name=params.get("firstName", ""),
                last_name=params.get("lastName", ""),
                country=country,
                city=params.get("city", ""),
                remoteAddr=request.META["REMOTE_ADDR"],
                regRemoteAddr=request.META["REMOTE_ADDR"],
                activateCode=GetNewId(),
                activated=not settings.ENABLE_ACTIVATION,
            )
            user.save()

            if settings.ENABLE_ACTIVATION:
                SendActivateMail(user)
                return RenderToResponse("user/auth_success.html", request, {
                            "email": params.get("email", ""),
                        })
            else:
                user = authenticate(
                    username=user.username,
                    password=params.get("password", ""),
                )
                login(request, user)
                backref = params.get("next", "/user/profile/?firsttime=1")
                return redirect(backref)
        except RegErr as e:
            raise RedirectExc("/user/auth/?msgReg={}".format(e.status))
    return RenderToResponse("user/auth.html", request, {
        "countries": GetCountries(),
        "msgLogin": GetRegMsg(params.get("msgLogin", "")),
        "msgReg": GetRegMsg(params.get("msgReg", "")),
        "next": params.get("next", ""),
    })